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

  1. '********************************************************************
  2. '*
  3. '* File:           CompSys.vbs 
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  List the properties of a computer system.
  8. '*
  9. '*  CompSys.vbs [/S <server>] [/U <username>] [/W <password>] 
  10. '*              [/O <outputfile>]
  11. '*
  12. '* Copyright (C) 1999 Microsoft Corporation
  13. '*
  14. '********************************************************************
  15.  
  16.  
  17. OPTION EXPLICIT
  18.  
  19.     'Define constants
  20.     CONST CONST_ERROR                   = 0
  21.     CONST CONST_WSCRIPT                 = 1
  22.     CONST CONST_CSCRIPT                 = 2
  23.     CONST CONST_SHOW_USAGE              = 3
  24.     CONST CONST_PROCEED                 = 4
  25.  
  26.     'Declare variables
  27.     Dim intOpMode, i
  28.     Dim strServer, strUserName, strPassword, strOutputFile
  29.  
  30.     'Make sure the host is csript, if not then abort
  31.     VerifyHostIsCscript()
  32.  
  33.     'Parse the command line
  34.     intOpMode = intParseCmdLine(strServer     ,  _
  35.                                 strUserName   ,  _
  36.                                 strPassword   ,  _
  37.                                 strOutputFile    )
  38.  
  39.  
  40.     Select Case intOpMode
  41.  
  42.         Case CONST_SHOW_USAGE
  43.             Call ShowUsage()
  44.  
  45.         Case CONST_PROCEED                 
  46.             Call LstCompSys(strServer    , _
  47.                            strOutputFile , _
  48.                            strUserName   , _
  49.                            strPassword     )
  50.  
  51.         Case CONST_ERROR
  52.             'Do Nothing
  53.  
  54.         Case Else                    'Default -- should never happen
  55.             Call Wscript.Echo("Error occurred in passing parameters.")
  56.  
  57.     End Select
  58.  
  59. '********************************************************************
  60. '* End of Script
  61. '********************************************************************
  62.  
  63. '********************************************************************
  64. '*
  65. '* Sub LstCompSys()
  66. '*
  67. '* Purpose: Outputs Information on CDRomDrives.
  68. '*
  69. '* Input:   strServer           a machine name
  70. '*          strOutputFile       an output file name
  71. '*          strUserName         the current user's name
  72. '*          strPassword         the current user's password
  73. '*
  74. '* Output:  Results are either printed on screen or saved in strOutputFile.
  75. '*
  76. '********************************************************************
  77.  
  78. Private Sub LstCompSys(strServer, strOutputFile, strUserName, strPassword)
  79.  
  80.     ON ERROR RESUME NEXT
  81.  
  82.     Dim objFileSystem, objOutputFile, objService, objCompSet, objComp
  83.     Dim strLine, strWbemClass
  84.  
  85.     strWBEMClass = "Win32_ComputerSystem"
  86.  
  87.     'Open a text file for output if the file is requested
  88.     If Not IsEmpty(strOutputFile) Then
  89.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  90.             Call Wscript.Echo ("Could not open an output file.")
  91.             Exit Sub
  92.         End If
  93.     End If
  94.  
  95.     'Establish a connection with the server.
  96.     If blnConnect("root\cimv2" , _
  97.                    strUserName , _
  98.                    strPassword , _
  99.                    strServer   , _
  100.                    objService  ) Then
  101.         Call Wscript.Echo("")
  102.         Call Wscript.Echo("Please check the server name, " _
  103.                         & "credentials and WBEM Core.")
  104.         Exit Sub
  105.     End If
  106.  
  107.     'Get the set
  108.     Set objCompSet = objService.InstancesOf(strWBEMClass)
  109.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  110.         Exit Sub
  111.     End If
  112.  
  113.     If objCompSet.Count = 0 Then
  114.         Call WriteLine("No computer system information is available." _
  115.                      , objOutputFile)    
  116.         Exit Sub
  117.     End If
  118.  
  119.     Call WriteLine("Computer system information for Machine " & strServer, _
  120.                     objOutputFile)
  121.     Call WriteLine("", objOutputFile)
  122.  
  123.     For Each objComp In objCompSet
  124.         Call WriteLine("Name                         = " _
  125.                      & objComp.Name, objOutputFile)
  126.         Call WriteLine("Domain                       = " _
  127.                      & objComp.Domain, objOutputFile)
  128.         Call WriteLine("Infrared Supported           = " _
  129.                      & objComp.InfraredSupported, objOutputFile)
  130.         Call WriteLine("Network Server Mode Enabled  = " _
  131.                      & objComp.NetworkServerModeEnabled, objOutputFile)
  132.         Call WriteLine("System Type                  = " _
  133.                      & objComp.SystemType, objOutputFile)
  134.         Call WriteLine("Primary Owner Name           = " _
  135.                      & objComp.PrimaryOwnerName, objOutputFile)
  136.         Call WriteLine("", objOutputFile)
  137.     Next
  138.  
  139.     If IsObject(objOutputFile) Then
  140.         objOutputFile.Close
  141.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  142.     End If
  143.  
  144. End Sub
  145.  
  146. '********************************************************************
  147. '*
  148. '* Function intParseCmdLine()
  149. '*
  150. '* Purpose: Parses the command line.
  151. '* Input:   
  152. '*
  153. '* Output:  strServer         a remote server ("" = local server")
  154. '*          strUserName       the current user's name
  155. '*          strPassword       the current user's password
  156. '*          strOutputFile     an output file name
  157. '*
  158. '********************************************************************
  159. Private Function intParseCmdLine( ByRef strServer,        _
  160.                                   ByRef strUserName,      _
  161.                                   ByRef strPassword,      _
  162.                                   ByRef strOutputFile     )
  163.  
  164.  
  165.     ON ERROR RESUME NEXT
  166.  
  167.     Dim strFlag
  168.     Dim intState, intArgIter
  169.     Dim objFileSystem
  170.  
  171.     If Wscript.Arguments.Count > 0 Then
  172.         strFlag = Wscript.arguments.Item(0)
  173.     End If
  174.  
  175.     If IsEmpty(strFlag) Then                'No arguments have been received
  176.         intParseCmdLine = CONST_PROCEED
  177.         Exit Function
  178.     End If
  179.  
  180.     'Check if the user is asking for help or is just confused
  181.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  182.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  183.         OR (strFlag="h") Then
  184.         intParseCmdLine = CONST_SHOW_USAGE
  185.         Exit Function
  186.     End If
  187.  
  188.     'Retrieve the command line and set appropriate variables
  189.      intArgIter = 0
  190.     Do While intArgIter <= Wscript.arguments.Count - 1
  191.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  192.   
  193.             Case "/s"
  194.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  195.                     intParseCmdLine = CONST_ERROR
  196.                     Exit Function
  197.                 End If
  198.                 intArgIter = intArgIter + 1
  199.  
  200.             Case "/o"
  201.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  202.                     intParseCmdLine = CONST_ERROR
  203.                     Exit Function
  204.                 End If
  205.                 intArgIter = intArgIter + 1
  206.  
  207.             Case "/u"
  208.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  209.                     intParseCmdLine = CONST_ERROR
  210.                     Exit Function
  211.                 End If
  212.                 intArgIter = intArgIter + 1
  213.  
  214.             Case "/w"
  215.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  216.                     intParseCmdLine = CONST_ERROR
  217.                     Exit Function
  218.                 End If
  219.                 intArgIter = intArgIter + 1
  220.  
  221.             Case Else 'We shouldn't get here
  222.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  223.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  224.                    & "Please check the input and try again," & vbCRLF _
  225.                    & "or invoke with '/?' for help with the syntax.")
  226.                 Wscript.Quit
  227.  
  228.         End Select
  229.  
  230.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  231.  
  232.     If IsEmpty(intParseCmdLine) Then _
  233.         intParseCmdLine = CONST_PROCEED
  234.  
  235. End Function
  236.  
  237. '********************************************************************
  238. '*
  239. '* Sub ShowUsage()
  240. '*
  241. '* Purpose: Shows the correct usage to the user.
  242. '*
  243. '* Input:   None
  244. '*
  245. '* Output:  Help messages are displayed on screen.
  246. '*
  247. '********************************************************************
  248. Private Sub ShowUsage()
  249.  
  250.     Wscript.Echo ""
  251.     Wscript.Echo "List the properties of a computer system."
  252.     Wscript.Echo ""
  253.     Wscript.Echo "SYNTAX:"
  254.     Wscript.Echo "  CompSys.vbs [/S <server>] [/U <username>]" _
  255.                & " [/W <password>]"
  256.     Wscript.Echo "  [/O <outputfile>]"
  257.     Wscript.Echo ""
  258.     Wscript.Echo "PARAMETER SPECIFIERS:"
  259.     Wscript.Echo "   server        A machine name."
  260.     Wscript.Echo "   username      The current user's name."
  261.     Wscript.Echo "   password      Password of the current user."
  262.     Wscript.Echo "   outputfile    The output file name."
  263.     Wscript.Echo ""
  264.     Wscript.Echo "EXAMPLE:"
  265.     Wscript.Echo "1. cscript CompSys.vbs"
  266.     Wscript.Echo "   List the properties of a computer system for the " _
  267.                & "current machine."
  268.     Wscript.Echo "2. cscript CompSys.vbs /S MyMachine2"
  269.     Wscript.Echo "   List the properties of a computer system 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 CompSys.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.              "    ""CompSys.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.