home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / ras.cab / irqres.vbs < prev    next >
Text File  |  1999-11-04  |  21KB  |  629 lines

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