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

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