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

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