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

  1. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. '  File:           DeviceMem.VBS
  3. '  Created:        December 1998
  4. '  Version:        1.0
  5. '  Main Function: Outputs Information on Disk Drives.
  6. '
  7. '  Drives.vbs  [/S <server>] [/O <outputfile>] [/U <username>] [/W <password>] 
  8. '  Copyright (C) 1998 Microsoft Corporation
  9. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  10. CONST THISFILE                      = "DeviceMem.VBS"
  11.  
  12.  
  13. 'Define constants
  14. CONST CONST_ERROR                   = 0
  15. CONST CONST_WSCRIPT                 = 1
  16. CONST CONST_CSCRIPT                 = 2
  17. CONST CONST_SHOW_USAGE              = 3
  18. CONST CONST_PROCEED                 = 4
  19. CONST CONST_LIST                    = "LIST"
  20. CONST CONST_DEFAULTTASK             = "LIST"
  21.  
  22. 'Generic variables for remote automation
  23. Dim intOpMode,    strTaskCommand,   strArgArray(),   i 'generic loop counter
  24. Dim strServer,    strUserName,      strPassword,     objService
  25. Dim blnReboot,    blnForce,         blnVerbose
  26.  
  27.  
  28. 'Get the command line arguments
  29. ReDim strArgArray (Max (0, Wscript.arguments.count - 1))
  30.  
  31. For i = 0 to Wscript.arguments.count - 1
  32.     strArgArray(i) = Wscript.arguments.Item(i)
  33. Next
  34.  
  35. call VerifyCScript()
  36.  
  37. 'Parse the command line
  38. intOpMode = intParseCmdLine(  strArgArray,      _
  39.                               strTaskCommand,   _ 
  40.                               strServer,        _
  41.                               strUserName,      _
  42.                               strPassword,      _
  43.                               strOutPutFile    )
  44.  
  45. Do
  46.   Select Case intOpMode
  47.     Case CONST_PROCEED    Exit Do
  48.     Case CONST_SHOW_USAGE Call ShowUsage()
  49.     Case Else             Call Print("Error occurred in passing parameters.")
  50.   End Select
  51.   Wscript.Quit
  52. Loop
  53.  
  54. 'Establish a connection with the server
  55. Call blnConnect (objService, strServer, "root/cimv2", strUserName, strPassword)
  56.  
  57. if not isobject (objService) then 
  58.   call Print("Please check the server name, credentials and WBEM Core.")
  59.   Wscript.Quit
  60. end if
  61.  
  62. If IsEmpty(strServer) Then
  63.   Dim objWshNet
  64.   Set objWshNet = CreateObject("Wscript.Network")
  65.   strServer = objWshNet.ComputerName
  66. End If
  67.  
  68. Select Case strTaskCommand
  69.  
  70.     Case CONST_LIST
  71.         call List (objService)
  72.  
  73.     Case Else
  74.         call ShowUsage ()
  75.  
  76. End Select
  77.  
  78. 'END of main routine
  79.  
  80.  
  81. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  82. '  Function List ()
  83. '  Purpose: List instances of the class Win32_DeviceMemoryAddress
  84. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  85. Private Sub List (objService)
  86.  
  87.   ON ERROR RESUME NEXT
  88.  
  89.   Dim objFileSystem, objOutFile
  90.   Dim strClass, objSet, obj, objInst, strLine
  91.  
  92.   If NOT IsEmpty(strOutputFile) Then
  93.     'Create a file object
  94.     set objFileSystem = CreateObject("Scripting.FileSystemObject")
  95.     If ErrorOccurred("opening a filesystem object.") Then Exit Sub
  96.     'Open the file for output
  97.     set objOutFile = objFileSystem.OpenTextFile(strOutputFile, 8, True)
  98.     If ErrorOccurred("opening file " + strOutputFile) Then Exit Sub
  99.   End If
  100.  
  101.   'Get the first instance
  102.   strClass = "Win32_DeviceMemoryAddress"
  103.   Set objSet = objService.InstancesOf(strClass)
  104.   If ErrorOccurred ("obtaining the "& strClass) Then Exit Sub
  105.  
  106.   WriteLine "Instances of "& strClass& " on "& strServer, objOutFile
  107.   Dim intW(5)
  108.   intW(0) = 23
  109.   intW(1) = 11
  110.   intW(2) = 12
  111.   intW(3) = 14
  112.   intW(4) = 14
  113.   strLine = Empty
  114.   strLine = strLine + strPackString ("Range"       ,intW(0),1,1)
  115.   strLine = strLine + strPackString ("Starting"    ,intW(1),1,1)
  116.   strLine = strLine + strPackString ("Ending"      ,intW(2),1,1)
  117.   strLine = strLine + strPackString ("MemoryType"  ,intW(3),1,1)
  118.   strLine = strLine + strPackString ("Status"      ,intW(4),1,1)
  119.   WriteLine " ", objOutFile
  120.   WriteLine strLine, objOutFile
  121.   WriteLine Replace (Space(73), " ", "-"), objOutFile
  122.  
  123.   For Each obj In objSet
  124.     strLine = Empty
  125.     strLine = strLine + strPackString (obj.Caption        ,intW(0),1,1) 
  126.     strLine = strLine + strPackString (obj.StartingAddress,intW(1),1,1)
  127.     strLine = strLine + strPackString (obj.EndingAddress  ,intW(2),1,1)
  128.     strLine = strLine + strPackString (obj.MemoryType     ,intW(3),1,1)
  129.     strLine = strLine + strPackString (obj.Status         ,intW(4),1,1)
  130.     WriteLine strLine, objOutFile
  131.   Next
  132.  
  133.   If NOT IsEmpty(objOutFile) Then
  134.     objOutFile.Close
  135.     Wscript.Echo "Results are saved in file " & strOutputFile & "."
  136.   End If
  137.  
  138. End Sub
  139.  
  140.  
  141. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  142. '  Function blnConnect()
  143. '  Purpose: Connects to machine strServer.
  144. '  Input:   strServer       a machine name
  145. '           strNameSpace    a namespace
  146. '           strUserName     name of the current user
  147. '           strPassword     password of the current user
  148. '  Output:  objService is returned  as a service object.
  149. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  150. Private Function blnConnect (objService, strServer, strNameSpace, strUserName, strPassword)
  151.  
  152.     ON ERROR RESUME NEXT
  153.     Dim objLocator
  154.  
  155.     blnConnect = True 'Return an error if we exit before reaching the end.
  156.  
  157.     'Create Locator object to connect to remote CIM object manager
  158.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  159.         if ErrorOccurred("occurred creating a locator object.") _
  160.         Then Exit Function
  161.  
  162.     'Connect to the namespace which is either local or remote
  163.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  164.                                                strUserName, strPassword)
  165.          if ErrorOccurred("occurred connecting to server \\" & strServer & "." ) _
  166.         then Exit Function
  167.  
  168.         ObjService.Security_.impersonationlevel = 3
  169.          if ErrorOccurred("occurred setting security level on \\" & strServer & "." ) _
  170.         then Exit Function
  171.  
  172.     blnConnect = False     'There is no error.
  173.  
  174. End Function
  175.  
  176. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  177. '  Function ErrorOccurred()
  178. '  Purpose: Reports error with a string saying what the error occurred in.
  179. '  Input:   strIn        string saying what the error occurred in.
  180. '  Output:                displayed on screen 
  181. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  182. Private Function ErrorOccurred (strIn)
  183.     If Err.Number then
  184.         call Print( "Error 0x" & CStr(Hex(Err.Number)) & " occurred " & strIn)
  185.         If Err.Description <> "" Then
  186.             call Print( "Error description: " & Err.Description)
  187.         End If
  188.         Err.Clear
  189.         ErrorOccurred = true
  190.     Else
  191.         ErrorOccurred = false
  192.     End If
  193. End Function
  194.  
  195. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  196. '  Sub Print()
  197. '  Purpose: Prints a message on screen.
  198. '  Input:   strMessage      the string to print
  199. '  Output:  strMessage is printed on screen.
  200. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  201. Sub Print (ByRef strMessage)
  202.  
  203.     Wscript.Echo  strMessage
  204.  
  205. End Sub
  206.  
  207. '********************************************************************
  208. '*
  209. '* Function strPackString()
  210. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  211. '* Input:   strString   a string
  212. '*          intWidth   the intended length of the string
  213. '*          blnAfter    specifies whether to add spaces after or before the string
  214. '*          blnTruncate specifies whether to truncate the string or not if
  215. '*                      the string length is longer than intWidth
  216. '* Output:  strPackString is returned as the packed string.
  217. '*
  218. '********************************************************************
  219. Private Function strPackString(strString, ByVal intWidth, blnAfter, blnTruncate)
  220.  
  221.     'ON ERROR RESUME NEXT
  222.  
  223.     intWidth = CInt(intWidth)
  224.     blnAfter = CBool(blnAfter)
  225.     blnTruncate = CBool(blnTruncate)
  226.     If Err.Number Then
  227.         Print "Argument type is incorrect!"
  228.         Err.Clear
  229.         Wscript.Quit
  230.     End If
  231.  
  232.     If IsNull(strString) Then
  233.         strPackString = "null" & Space(intWidth-4)
  234.         Exit Function
  235.     End If
  236.  
  237.     strString = CStr(strString)
  238.     If Err.Number Then
  239.         Print "Argument type is incorrect!"
  240.         Err.Clear
  241.         Wscript.Quit
  242.     End If
  243.  
  244.     If intWidth > Len(strString) Then
  245.         If blnAfter Then
  246.             strPackString = strString & Space(intWidth-Len(strString))
  247.         Else
  248.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  249.         End If
  250.     Else
  251.         If blnTruncate Then
  252.             strPackString = Left(strString, intWidth-1) & " "
  253.         Else
  254.             strPackString = strString & " "
  255.         End If
  256.     End If
  257.  
  258. End Function
  259.  
  260. '********************************************************************
  261. '*
  262. '* Sub WriteLine()
  263. '* Purpose: Writes a text line either to a file or on screen.
  264. '* Input:   strMessage  the string to print
  265. '*          objFile     an output file object
  266. '* Output:  strMessage is either displayed on screen or written to a file.
  267. '*
  268. '********************************************************************
  269. Sub WriteLine(ByRef strMessage, ByRef objFile)
  270.  
  271.     If IsObject(objFile) then        'objFile should be a file object
  272.         objFile.WriteLine strMessage
  273.     Else
  274.         Wscript.Echo  strMessage
  275.     End If
  276.  
  277. End Sub
  278.  
  279. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  280. '  Sub Max()
  281. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  282. Private Function Max (intA, intB) 
  283.     Max = intA
  284.     if intA < intB then Max = intB
  285. End Function
  286.  
  287.  
  288. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  289. '
  290. '  Sub VerifyCScript ()
  291. '
  292. '  Purpose:  Verify that the script is called with CScript, and 
  293. '            take appropriate action (quit with GUI usage message)
  294. '
  295. '  Suggested Use: add this line at beginning of main module:
  296. '            call VerifyCScript()
  297. '
  298. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  299. Sub VerifyCScript ()
  300.  
  301.   Select Case intChkProgram()
  302.     Case CONST_CSCRIPT 
  303.       'Do Nothing
  304.     Case CONST_WSCRIPT
  305.       WScript.Echo "Please run this script using CScript." & vbCRLF & _
  306.         "This can be achieved by" & vbCRLF & _
  307.         "1. Using ""CScript "&THISFILE&" arguments"" for Windows 95/98 or" & vbCRLF & _
  308.         "2. Changing the default Windows Scripting Host setting to CScript" & vbCRLF & _
  309.         "    using ""CScript //H:CScript //S"" and running the script using" & vbCRLF & _
  310.         "    """&THISFILE&" arguments"" for Windows NT."
  311.       WScript.Quit
  312.     Case Else
  313.       WScript.Quit
  314.   End Select
  315.  
  316. End Sub
  317.  
  318. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  319. '
  320. '  Function intChkProgram()
  321. '
  322. '  Purpose: Determines which program is used to run this script.
  323. '  Input:   None
  324. '
  325. '  Output:  intChkProgram is set to one of CONST_ERROR, CONST_WSCRIPT,
  326. '           and CONST_CSCRIPT.
  327. '
  328. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  329. Private Function intChkProgram()
  330.  
  331.     ON ERROR RESUME NEXT
  332.  
  333.     Dim strFullName, strCommand, i, j
  334.  
  335.     intChkProgram =  CONST_ERROR
  336.  
  337.     'strFullName should be something like C:\WINDOWS\COMMAND\CSCRIPT.EXE
  338.     strFullName = WScript.FullName
  339.     If ErrorOccurred("occurred.") Then Exit Function
  340.  
  341.     i = InStr(1, strFullName, ".exe", 1)
  342.     If i = 0 Then Exit Function
  343.  
  344.     j = InStrRev(strFullName, "\", i, 1)
  345.     If j = 0 Then Exit Function
  346.  
  347.     strCommand = Mid(strFullName, j+1, i-j-1)
  348.     Select Case LCase(strCommand)
  349.         Case "cscript"
  350.             intChkProgram = CONST_CSCRIPT
  351.         Case "wscript"
  352.             intChkProgram = CONST_WSCRIPT
  353.         Case Else       'should never happen
  354.             call Print( "An unexpected program is used to run this script.")
  355.             call Print( "Only CScript.Exe or WScript.Exe can be used to run this script.")
  356.     End Select
  357.  
  358. End Function
  359.  
  360.  
  361. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  362. '  Function GetArg()
  363. '
  364. '  Purpose: Helper to intParseCmdLine()
  365. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  366. Private Function GetArg (strArgArray, intArgIter, strVar, StrVarName) 
  367.  
  368.   GetArg = True 'return error if we don't make it to end function
  369.  
  370.   intArgIter = intArgIter + 1
  371.   If intArgIter > UBound(strArgArray) Then Exit Function
  372.  
  373.   strVar = strArgArray (intArgIter)
  374.   If Err.Number Then
  375.     call Print( "Invalid " & StrVarName & ".")
  376.     call Print( "Please check the input and try again.")
  377.   Exit Function
  378.   End If
  379.  
  380.   If InStr(strVar, "/") Then
  381.     call Print( "Invalid " & StrVarName)
  382.     call Print( "Please check the input and try again.")
  383.     Wscript.Quit
  384.   End If
  385.  
  386.   GetArg = False 'success
  387.  
  388. End Function
  389.  
  390.  
  391. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  392. '  Function ShowUsage ()
  393. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  394. Sub ShowUsage ()
  395.  
  396.   Wscript.Echo ""
  397.   Wscript.Echo "Outputs Information on Device Memory Address Ranges."
  398.   Wscript.Echo ""
  399.   Wscript.Echo "SYNTAX:"
  400.   Wscript.Echo "  DeviceMem.vbs  [/S <server>] [/O <outputfile>] [/U <username>]"
  401.   Wscript.Echo "  [/W <password>]"
  402.   Wscript.Echo ""
  403.   Wscript.Echo "PARAMATER SPECIFIERS:"
  404.   Wscript.Echo "   server        A machine name."
  405.   Wscript.Echo "   outputfile    The output file name."
  406.   Wscript.Echo "   username      The current user's name."
  407.   Wscript.Echo "   password      Password of the current user."
  408.   Wscript.Echo ""
  409.   Wscript.Echo "EXAMPLE:"
  410.   Wscript.Echo "1. cscript DeviceMem.vbs"
  411.   Wscript.Echo "   Get the device memory address ranges for the current machine."
  412.   Wscript.Echo "2. cscript DeviceMem.vbs /S MyMachine2"
  413.   Wscript.Echo "   Get the device memory address ranges  for the machine MyMachine2."
  414.  
  415. End Sub
  416.  
  417. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  418. '  Function intParseCmdLine()
  419. '  Purpose: Parses the command line.
  420. '  Input:   strArgArray  an array containing input from the command line
  421. '  Output:  strTaskCommand    one of help, ...
  422. '           strServer         a machine name
  423. '           strUserName       the current user's name
  424. '           strPassword       the current user's password
  425. '           strOutPutFile     an output file
  426. '           intParseCmdLine   is set to one of CONST_ERROR, 
  427. '                             CONST_SHOW_USAGE, or CONST_PROCEED.
  428. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  429. Private Function intParseCmdLine( strArgArray,      _
  430.                                   strTaskCommand,   _ 
  431.                                   strServer,        _
  432.                                   strUserName,      _
  433.                                   strPassword,      _
  434.                                   strOutPutFile    )
  435.  
  436.   Dim strFlag
  437.   Dim intArgIter
  438.   Dim objFileSystem
  439.  
  440.   strFlag = strArgArray(0)
  441.   If strFlag = "" Then                 'No arguments have been received
  442.     strTaskCommand = CONST_DEFAULTTASK 'Default Task
  443.     intParseCmdLine = CONST_PROCEED
  444.     Exit Function
  445.   End If
  446.  
  447.   'Check if the user is asking for help or is just confused
  448.   intParseCmdLine = CONST_SHOW_USAGE
  449.   If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  450.    OR (strFlag= "\?") OR (strFlag="/?") OR (strFlag="?") OR (strFlag="h") _
  451.   Then Exit Function
  452.  
  453.   'Retrieve the command line and set appropriate variables
  454.   Dim intArgMax
  455.   intArgMax = UBound(strArgArray)
  456.  
  457.   'Return an error if we don't survive the next round
  458.   intParseCmdLine = CONST_ERROR 
  459.  
  460.   intArgIter = 0
  461.   Do While intArgIter <= UBound(strArgArray)
  462.     Select Case LCase(strArgArray(intArgIter))
  463.  
  464.       Case "/s"
  465.         If GetArg (strArgArray, intArgIter, strServer, "server name") _
  466.         Then Exit Function
  467.  
  468.       Case "/u" 
  469.         If GetArg (strArgArray, intArgIter, strUserName, "user name") _
  470.         Then Exit Function
  471.  
  472.       Case "/w"
  473.         If GetArg (strArgArray, intArgIter, strPassword, "password") _
  474.         Then Exit Function
  475.  
  476.       Case "/o"
  477.         If GetArg (strArgArray, intArgIter, strOutPutFile, "output filename") _
  478.         Then Exit Function
  479.  
  480.       Case Else 'We shouldn't get here
  481.         call Print( "Invalid or misplaced parameter: " _
  482.                   & strArgArray(intArgIter) & vbCRLF _
  483.                   & "Please check the input and try again," & vbCRLF _
  484.                   & "or invoke with '/?' for help with the syntax." )
  485.         Wscript.Quit
  486.  
  487.     End Select
  488.  
  489.     intArgIter = intArgIter + 1     'default advance
  490.  
  491.   Loop '** intArgIter <= UBound(strArgArray) **
  492.  
  493.   'we survived the last round; return default command if not set
  494.   If IsEmpty(strTaskCommand) _
  495.     Then strTaskCommand = CONST_DEFAULTTASK 'Default Task
  496.  
  497.   intParseCmdLine = CONST_PROCEED
  498.  
  499. End Function
  500.