home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / ras.cab / parallelport.vbs < prev    next >
Text File  |  1999-11-04  |  22KB  |  630 lines

  1. '********************************************************************
  2. '*
  3. '* File:           ParallelPort.vbs
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Get the parallel port information for a machine.
  8. '*
  9. '*  ParallelPort.vbs      [/S <server>] [/U <username>] [/W <password>] 
  10. '*                        [/O <outputfile>]
  11. '*
  12. '* Copyright (C) 1999 Microsoft Corporation
  13. '*
  14. '********************************************************************
  15.  
  16. OPTION EXPLICIT
  17.  
  18.     'Define constants
  19.     CONST CONST_ERROR                   = 0
  20.     CONST CONST_WSCRIPT                 = 1
  21.     CONST CONST_CSCRIPT                 = 2
  22.     CONST CONST_SHOW_USAGE              = 3
  23.     CONST CONST_PROCEED                 = 4
  24.  
  25.     'Declare variables
  26.     Dim intOpMode, i
  27.     Dim strServer, strUserName, strPassword, strOutputFile
  28.  
  29.     'Make sure the host is csript, if not then abort
  30.     VerifyHostIsCscript()
  31.  
  32.     'Parse the command line
  33.     intOpMode = intParseCmdLine(strServer     ,  _
  34.                                 strUserName   ,  _
  35.                                 strPassword   ,  _
  36.                                 strOutputFile    )
  37.  
  38.  
  39.     Select Case intOpMode
  40.  
  41.         Case CONST_SHOW_USAGE
  42.             Call ShowUsage()
  43.  
  44.         Case CONST_PROCEED                 
  45.             Call GetPPortInfo(strServer     , _
  46.                               strOutputFile , _
  47.                               strUserName   , _
  48.                               strPassword     )
  49.  
  50.         Case CONST_ERROR
  51.             'Do Nothing
  52.  
  53.         Case Else                    'Default -- should never happen
  54.             Call Wscript.Echo("Error occurred in passing parameters.")
  55.  
  56.     End Select
  57.  
  58. '********************************************************************
  59. '* End of Script
  60. '********************************************************************
  61.  
  62. '********************************************************************
  63. '*
  64. '* Sub GetPPortInfo()
  65. '*
  66. '* Purpose: Get the parallel port information for a machine.
  67. '*
  68. '* Input:   strServer           a machine name
  69. '*          strOutputFile       an output file name
  70. '*          strUserName         the current user's name
  71. '*          strPassword         the current user's password
  72. '*
  73. '* Output:  Results are either printed on screen or saved in strOutputFile.
  74. '*
  75. '********************************************************************
  76. Private Sub GetPPortInfo(strServer, strOutputFile, strUserName, strPassword)
  77.  
  78.     ON ERROR RESUME NEXT
  79.  
  80.     Dim objFileSystem, objOutputFile, objService, objPPortSet, objPPort
  81.     Dim strWBEMClass
  82.  
  83.     strWBEMClass = "Win32_ParallelPort"
  84.  
  85.     'Open a text file for output if the file is requested
  86.     If Not IsEmpty(strOutputFile) Then
  87.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  88.             Call Wscript.Echo ("Could not open an output file.")
  89.             Exit Sub
  90.         End If
  91.     End If
  92.  
  93.     'Establish a connection with the server.
  94.     If blnConnect("root\cimv2" , _
  95.                    strUserName , _
  96.                    strPassword , _
  97.                    strServer   , _
  98.                    objService  ) Then
  99.         Call Wscript.Echo("")
  100.         Call Wscript.Echo("Please check the server name, " _
  101.                         & "credentials and WBEM Core.")
  102.         Exit Sub
  103.     End If
  104.  
  105.     'Get the set
  106.     Set objPPortSet = objService.InstancesOf(strWBEMClass)
  107.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  108.         Exit Sub
  109.     End If
  110.  
  111.     If objPPortSet.Count = 0 Then
  112.         Call WriteLine("No parallel port information is available.", _
  113.                         objOutputFile)    
  114.         Exit Sub
  115.     End If
  116.  
  117.     Call WriteLine("Parallel port information for Machine " & _
  118.                     strServer, objOutputFile)
  119.     Call WriteLine("", objOutputFile)
  120.  
  121.     For Each objPPort In objPPortSet
  122.         Call WriteLine("Name = " & objPPort.Name, objOutputFile)
  123.         Call WriteLine("PNP Device ID = " & objPPort.PNPDeviceID, objOutputFile)
  124.         Call WriteLine("Device ID = " & objPPort.DeviceID, objOutputFile)
  125.         Call WriteLine("", objOutputFile)
  126.     Next
  127.  
  128.     If IsObject(objOutputFile) Then
  129.         objOutputFile.Close
  130.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  131.     End If
  132.  
  133. End Sub
  134.  
  135. '********************************************************************
  136. '*
  137. '* Function intParseCmdLine()
  138. '*
  139. '* Purpose: Parses the command line.
  140. '* Input:   
  141. '*
  142. '* Output:  strServer         a remote server ("" = local server")
  143. '*          strUserName       the current user's name
  144. '*          strPassword       the current user's password
  145. '*          strOutputFile     an output file name
  146. '*
  147. '********************************************************************
  148. Private Function intParseCmdLine( ByRef strServer,        _
  149.                                   ByRef strUserName,      _
  150.                                   ByRef strPassword,      _
  151.                                   ByRef strOutputFile     )
  152.  
  153.  
  154.     ON ERROR RESUME NEXT
  155.  
  156.     Dim strFlag
  157.     Dim intState, intArgIter
  158.     Dim objFileSystem
  159.  
  160.     If Wscript.Arguments.Count > 0 Then
  161.         strFlag = Wscript.arguments.Item(0)
  162.     End If
  163.  
  164.     If IsEmpty(strFlag) Then                'No arguments have been received
  165.         intParseCmdLine = CONST_PROCEED
  166.         Exit Function
  167.     End If
  168.  
  169.     'Check if the user is asking for help or is just confused
  170.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  171.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  172.         OR (strFlag="h") Then
  173.         intParseCmdLine = CONST_SHOW_USAGE
  174.         Exit Function
  175.     End If
  176.  
  177.     'Retrieve the command line and set appropriate variables
  178.      intArgIter = 0
  179.     Do While intArgIter <= Wscript.arguments.Count - 1
  180.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  181.   
  182.             Case "/s"
  183.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  184.                     intParseCmdLine = CONST_ERROR
  185.                     Exit Function
  186.                 End If
  187.                 intArgIter = intArgIter + 1
  188.  
  189.             Case "/o"
  190.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  191.                     intParseCmdLine = CONST_ERROR
  192.                     Exit Function
  193.                 End If
  194.                 intArgIter = intArgIter + 1
  195.  
  196.             Case "/u"
  197.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  198.                     intParseCmdLine = CONST_ERROR
  199.                     Exit Function
  200.                 End If
  201.                 intArgIter = intArgIter + 1
  202.  
  203.             Case "/w"
  204.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  205.                     intParseCmdLine = CONST_ERROR
  206.                     Exit Function
  207.                 End If
  208.                 intArgIter = intArgIter + 1
  209.  
  210.             Case Else 'We shouldn't get here
  211.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  212.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  213.                    & "Please check the input and try again," & vbCRLF _
  214.                    & "or invoke with '/?' for help with the syntax.")
  215.                 Wscript.Quit
  216.  
  217.         End Select
  218.  
  219.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  220.  
  221.     If IsEmpty(intParseCmdLine) Then _
  222.         intParseCmdLine = CONST_PROCEED
  223.  
  224. End Function
  225.  
  226. '********************************************************************
  227. '*
  228. '* Sub ShowUsage()
  229. '*
  230. '* Purpose: Shows the correct usage to the user.
  231. '*
  232. '* Input:   None
  233. '*
  234. '* Output:  Help messages are displayed on screen.
  235. '*
  236. '********************************************************************
  237. Private Sub ShowUsage()
  238.  
  239.     Wscript.Echo ""
  240.     Wscript.Echo "Get the parallel port information for a machine."
  241.     Wscript.Echo ""
  242.     Wscript.Echo "SYNTAX:"
  243.     Wscript.Echo "  ParallelPort.vbs [/S <server>] [/U <username>]" _
  244.                 &" [/W <password>]"
  245.     Wscript.Echo "  [/O <outputfile>]"
  246.     Wscript.Echo ""
  247.     Wscript.Echo "PARAMETER SPECIFIERS:"
  248.     Wscript.Echo "   server        A machine name."
  249.     Wscript.Echo "   username      The current user's name."
  250.     Wscript.Echo "   password      Password of the current user."
  251.     Wscript.Echo "   outputfile    The output file name."
  252.     Wscript.Echo ""
  253.     Wscript.Echo "EXAMPLE:"
  254.     Wscript.Echo "1. cscript ParallelPort.vbs"
  255.     Wscript.Echo "   Get the parallel port information for the current machine."
  256.     Wscript.Echo "2. cscript ParallelPort.vbs /S MyMachine2"
  257.     Wscript.Echo "   Get the parallel port information for " _
  258.                & "the machine MyMachine2."
  259.  
  260. End Sub
  261.  
  262. '********************************************************************
  263. '* General Routines
  264. '********************************************************************
  265.  
  266. '********************************************************************
  267. '*
  268. '* Function strPackString()
  269. '*
  270. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  271. '*
  272. '* Input:   strString   a string
  273. '*          intWidth    the intended length of the string
  274. '*          blnAfter    Should spaces be added after the string?
  275. '*          blnTruncate specifies whether to truncate the string or not if
  276. '*                      the string length is longer than intWidth
  277. '*
  278. '* Output:  strPackString is returned as the packed string.
  279. '*
  280. '********************************************************************
  281. Private Function strPackString( ByVal strString, _
  282.                                 ByVal intWidth,  _
  283.                                 ByVal blnAfter,  _
  284.                                 ByVal blnTruncate)
  285.  
  286.     ON ERROR RESUME NEXT
  287.  
  288.     intWidth      = CInt(intWidth)
  289.     blnAfter      = CBool(blnAfter)
  290.     blnTruncate   = CBool(blnTruncate)
  291.  
  292.     If Err.Number Then
  293.         Call Wscript.Echo ("Argument type is incorrect!")
  294.         Err.Clear
  295.         Wscript.Quit
  296.     End If
  297.  
  298.     If IsNull(strString) Then
  299.         strPackString = "null" & Space(intWidth-4)
  300.         Exit Function
  301.     End If
  302.  
  303.     strString = CStr(strString)
  304.     If Err.Number Then
  305.         Call Wscript.Echo ("Argument type is incorrect!")
  306.         Err.Clear
  307.         Wscript.Quit
  308.     End If
  309.  
  310.     If intWidth > Len(strString) Then
  311.         If blnAfter Then
  312.             strPackString = strString & Space(intWidth-Len(strString))
  313.         Else
  314.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  315.         End If
  316.     Else
  317.         If blnTruncate Then
  318.             strPackString = Left(strString, intWidth-1) & " "
  319.         Else
  320.             strPackString = strString & " "
  321.         End If
  322.     End If
  323.  
  324. End Function
  325.  
  326. '********************************************************************
  327. '* 
  328. '*  Function blnGetArg()
  329. '*
  330. '*  Purpose: Helper to intParseCmdLine()
  331. '* 
  332. '*  Usage:
  333. '*
  334. '*     Case "/s" 
  335. '*       blnGetArg ("server name", strServer, intArgIter)
  336. '*
  337. '********************************************************************
  338. Private Function blnGetArg ( ByVal StrVarName,   _
  339.                              ByRef strVar,       _
  340.                              ByRef intArgIter) 
  341.  
  342.     blnGetArg = False 'failure, changed to True upon successful completion
  343.  
  344.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  345.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  346.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  347.                 strVar = Right(Wscript.Arguments(intArgIter), _
  348.                          Len(Wscript.Arguments(intArgIter)) - 3)
  349.                 blnGetArg = True
  350.                 Exit Function
  351.             Else
  352.                 intArgIter = intArgIter + 1
  353.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  354.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  355.                     Call Wscript.Echo( "Please check the input and try again.")
  356.                     Exit Function
  357.                 End If
  358.  
  359.                 strVar = Wscript.Arguments.Item(intArgIter)
  360.                 If Err.Number Then
  361.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  362.                     Call Wscript.Echo( "Please check the input and try again.")
  363.                     Exit Function
  364.                 End If
  365.  
  366.                 If InStr(strVar, "/") Then
  367.                     Call Wscript.Echo( "Invalid " & StrVarName)
  368.                     Call Wscript.Echo( "Please check the input and try again.")
  369.                     Exit Function
  370.                 End If
  371.  
  372.                 blnGetArg = True 'success
  373.             End If
  374.         Else
  375.             strVar = Right(Wscript.Arguments(intArgIter), _
  376.                      Len(Wscript.Arguments(intArgIter)) - 2)
  377.             blnGetArg = True 'success
  378.             Exit Function
  379.         End If
  380.     Else
  381.         intArgIter = intArgIter + 1
  382.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  383.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  384.             Call Wscript.Echo( "Please check the input and try again.")
  385.             Exit Function
  386.         End If
  387.  
  388.         strVar = Wscript.Arguments.Item(intArgIter)
  389.         If Err.Number Then
  390.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  391.             Call Wscript.Echo( "Please check the input and try again.")
  392.             Exit Function
  393.         End If
  394.  
  395.         If InStr(strVar, "/") Then
  396.             Call Wscript.Echo( "Invalid " & StrVarName)
  397.             Call Wscript.Echo( "Please check the input and try again.")
  398.             Exit Function
  399.         End If
  400.         blnGetArg = True 'success
  401.     End If
  402. End Function
  403.  
  404. '********************************************************************
  405. '*
  406. '* Function blnConnect()
  407. '*
  408. '* Purpose: Connects to machine strServer.
  409. '*
  410. '* Input:   strServer       a machine name
  411. '*          strNameSpace    a namespace
  412. '*          strUserName     name of the current user
  413. '*          strPassword     password of the current user
  414. '*
  415. '* Output:  objService is returned  as a service object.
  416. '*          strServer is set to local host if left unspecified
  417. '*
  418. '********************************************************************
  419. Private Function blnConnect(ByVal strNameSpace, _
  420.                             ByVal strUserName,  _
  421.                             ByVal strPassword,  _
  422.                             ByRef strServer,    _
  423.                             ByRef objService)
  424.  
  425.     ON ERROR RESUME NEXT
  426.  
  427.     Dim objLocator, objWshNet
  428.  
  429.     blnConnect = False     'There is no error.
  430.  
  431.     'Create Locator object to connect to remote CIM object manager
  432.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  433.     If Err.Number then
  434.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  435.                            " occurred in creating a locator object." )
  436.         If Err.Description <> "" Then
  437.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  438.         End If
  439.         Err.Clear
  440.         blnConnect = True     'An error occurred
  441.         Exit Function
  442.     End If
  443.  
  444.     'Connect to the namespace which is either local or remote
  445.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  446.        strUserName, strPassword)
  447.     ObjService.Security_.impersonationlevel = 3
  448.     If Err.Number then
  449.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  450.                            " occurred in connecting to server " _
  451.            & strServer & ".")
  452.         If Err.Description <> "" Then
  453.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  454.         End If
  455.         Err.Clear
  456.         blnConnect = True     'An error occurred
  457.     End If
  458.  
  459.     'Get the current server's name if left unspecified
  460.     If IsEmpty(strServer) Then
  461.         Set objWshNet = CreateObject("Wscript.Network")
  462.     strServer     = objWshNet.ComputerName
  463.     End If
  464.  
  465. End Function
  466.  
  467. '********************************************************************
  468. '*
  469. '* Sub      VerifyHostIsCscript()
  470. '*
  471. '* Purpose: Determines which program is used to run this script.
  472. '*
  473. '* Input:   None
  474. '*
  475. '* Output:  If host is not cscript, then an error message is printed 
  476. '*          and the script is aborted.
  477. '*
  478. '********************************************************************
  479. Sub VerifyHostIsCscript()
  480.  
  481.     ON ERROR RESUME NEXT
  482.  
  483.     Dim strFullName, strCommand, i, j, intStatus
  484.  
  485.     strFullName = WScript.FullName
  486.  
  487.     If Err.Number then
  488.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  489.         If Err.Description <> "" Then
  490.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  491.         End If
  492.         intStatus =  CONST_ERROR
  493.     End If
  494.  
  495.     i = InStr(1, strFullName, ".exe", 1)
  496.     If i = 0 Then
  497.         intStatus =  CONST_ERROR
  498.     Else
  499.         j = InStrRev(strFullName, "\", i, 1)
  500.         If j = 0 Then
  501.             intStatus =  CONST_ERROR
  502.         Else
  503.             strCommand = Mid(strFullName, j+1, i-j-1)
  504.             Select Case LCase(strCommand)
  505.                 Case "cscript"
  506.                     intStatus = CONST_CSCRIPT
  507.                 Case "wscript"
  508.                     intStatus = CONST_WSCRIPT
  509.                 Case Else       'should never happen
  510.                     Call Wscript.Echo( "An unexpected program was used to " _
  511.                                        & "run this script." )
  512.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  513.                                        & "be used to run this script." )
  514.                     intStatus = CONST_ERROR
  515.                 End Select
  516.         End If
  517.     End If
  518.  
  519.     If intStatus <> CONST_CSCRIPT Then
  520.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  521.              "This can be achieved by" & vbCRLF & _
  522.              "1. Using ""CScript Parallelport.vbs arguments"" for Windows 95/98 or" _
  523.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  524.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  525.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  526.              "    ""Parallelport.vbs arguments"" for Windows NT/2000." )
  527.         WScript.Quit
  528.     End If
  529.  
  530. End Sub
  531.  
  532. '********************************************************************
  533. '*
  534. '* Sub WriteLine()
  535. '* Purpose: Writes a text line either to a file or on screen.
  536. '* Input:   strMessage  the string to print
  537. '*          objFile     an output file object
  538. '* Output:  strMessage is either displayed on screen or written to a file.
  539. '*
  540. '********************************************************************
  541. Sub WriteLine(ByVal strMessage, ByVal objFile)
  542.  
  543.     On Error Resume Next
  544.     If IsObject(objFile) then        'objFile should be a file object
  545.         objFile.WriteLine strMessage
  546.     Else
  547.         Call Wscript.Echo( strMessage )
  548.     End If
  549.  
  550. End Sub
  551.  
  552. '********************************************************************
  553. '* 
  554. '* Function blnErrorOccurred()
  555. '*
  556. '* Purpose: Reports error with a string saying what the error occurred in.
  557. '*
  558. '* Input:   strIn        string saying what the error occurred in.
  559. '*
  560. '* Output:  displayed on screen 
  561. '* 
  562. '********************************************************************
  563. Private Function blnErrorOccurred (ByVal strIn)
  564.  
  565.     If Err.Number Then
  566.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  567.         If Err.Description <> "" Then
  568.             Call Wscript.Echo( "Error description: " & Err.Description)
  569.         End If
  570.         Err.Clear
  571.         blnErrorOccurred = True
  572.     Else
  573.         blnErrorOccurred = False
  574.     End If
  575.  
  576. End Function
  577.  
  578. '********************************************************************
  579. '* 
  580. '* Function blnOpenFile
  581. '*
  582. '* Purpose: Opens a file.
  583. '*
  584. '* Input:   strFileName        A string with the name of the file.
  585. '*
  586. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  587. '*            Nothing upon Failure.
  588. '* 
  589. '********************************************************************
  590. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  591.  
  592.     ON ERROR RESUME NEXT
  593.  
  594.     Dim objFileSystem
  595.  
  596.     Set objFileSystem = Nothing
  597.  
  598.     If IsEmpty(strFileName) OR strFileName = "" Then
  599.         blnOpenFile = False
  600.         Set objOpenFile = Nothing
  601.         Exit Function
  602.     End If
  603.  
  604.     'Create a file object
  605.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  606.     If blnErrorOccurred("Could not create filesystem object.") Then
  607.         blnOpenFile = False
  608.         Set objOpenFile = Nothing
  609.         Exit Function
  610.     End If
  611.  
  612.     'Open the file for output
  613.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  614.     If blnErrorOccurred("Could not open") Then
  615.         blnOpenFile = False
  616.         Set objOpenFile = Nothing
  617.         Exit Function
  618.     End If
  619.     blnOpenFile = True
  620.  
  621. End Function
  622.  
  623. '********************************************************************
  624. '*                                                                  *
  625. '*                           End of File                            *
  626. '*                                                                  *
  627. '********************************************************************
  628.  
  629.  
  630.