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

  1. '********************************************************************
  2. '*
  3. '* File:           PageFile.vbs
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Controls Pagefiles on a machine.
  8. '*
  9. '*  1.  PageFile.vbs /L [/S <server>] [/U <username>]
  10. '*                  [/W <password>] [/O <outputfile>]
  11. '*  
  12. '*  2.  PageFile.vbs /R /M <maxsize> /Z <minsize> /P <pagefile> [/B] [/F]
  13. '*                  [/S <server>] [/U <username>] [/W <password>]
  14. '*                  [/O <outputfile>]
  15. '*
  16. '* Copyright (C) 1998 Microsoft Corporation
  17. '*
  18. '********************************************************************
  19. OPTION EXPLICIT
  20.  
  21.     'Define constants
  22.     CONST CONST_REBOOT_FORCE            = 6
  23.     CONST CONST_REBOOT_NOFORCE          = 2
  24.     CONST CONST_ERROR                   = 0
  25.     CONST CONST_WSCRIPT                 = 1
  26.     CONST CONST_CSCRIPT                 = 2
  27.     CONST CONST_SHOW_USAGE              = 3
  28.     CONST CONST_PROCEED                 = 4
  29.     CONST CONST_LIST                    = "LIST"
  30.     CONST CONST_RESIZE                  = "RESIZE"
  31.  
  32.     'Declare variables
  33.     Dim intOpMode, i
  34.     Dim strServer, strUserName, strPassword, strOutputFile
  35.     Dim strOldName
  36.     Dim intMin, intMax
  37.     Dim blnReboot, blnForce
  38.  
  39.     'Make sure the host is csript, if not then abort
  40.     VerifyHostIsCscript()
  41.  
  42.     'Parse the command line
  43.     intOpMode = intParseCmdLine(strServer      ,  _
  44.                                 strUserName    ,  _
  45.                                 strPassword    ,  _
  46.                                 strOutputFile  ,  _
  47.                                 intMin         ,  _
  48.                                 intMax         ,  _
  49.                                 strOldName     ,  _
  50.                                 blnReboot      ,  _
  51.                                 blnForce          )
  52.  
  53.     Select Case intOpMode
  54.  
  55.         Case CONST_SHOW_USAGE
  56.             Call ShowUsage()
  57.  
  58.         Case CONST_LIST                 
  59.             Call ListPageFiles(strServer     , _
  60.                                strOutputFile , _
  61.                                strUserName   , _
  62.                                strPassword     )
  63.  
  64.         Case CONST_RESIZE
  65.             Call ResizePageFile(strServer     , _
  66.                                 strOutputFile , _
  67.                                 strUserName   , _
  68.                                 strPassword   , _
  69.                                 strOldName    , _
  70.                                 intMax        , _
  71.                                 intMin        , _
  72.                                 blnReboot     , _
  73.                                 blnForce        )
  74.  
  75.         Case CONST_ERROR
  76.             'Do Nothing
  77.  
  78.         Case Else                    'Default -- should never happen
  79.             Call Wscript.Echo("Error occurred in passing parameters.")
  80.  
  81.     End Select
  82.  
  83. '********************************************************************
  84. '*
  85. '* Sub ListPageFiles()
  86. '*
  87. '* Purpose: Lists data on pagefiles.
  88. '*
  89. '* Input:   strServer           a machine name
  90. '*          strOutputFile       an output file name
  91. '*          strUserName         the current user's name
  92. '*          strPassword         the current user's password
  93. '*
  94. '* Output:  Results are either printed on screen or saved in strOutputFile.
  95. '*
  96. '********************************************************************
  97. Private Sub ListPageFiles(strServer, strOutputFile, strUserName, strPassword)
  98.  
  99.     ON ERROR RESUME NEXT
  100.  
  101.     Dim objFileSystem, objOutputFile, objService, WbemObjectSet, objInst
  102.  
  103.     'Open a text file for output if the file is requested
  104.     If Not IsEmpty(strOutputFile) Then
  105.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  106.             Call Wscript.Echo ("Could not open an output file.")
  107.             Exit Sub
  108.         End If
  109.     End If
  110.  
  111.     'Establish a connection with the server.
  112.     If blnConnect("root\cimv2" , _
  113.                    strUserName , _
  114.                    strPassword , _
  115.                    strServer   , _
  116.                    objService  ) Then
  117.         Call Wscript.Echo("")
  118.         Call Wscript.Echo("Please check the server name, " _
  119.                         & "credentials and WBEM Core.")
  120.         Exit Sub
  121.     End If
  122.  
  123.     Set WbemObjectSet = objService.InstancesOf("Win32_PageFile")
  124.     If blnErrorOccurred(" attempting to get Pagefile Instances.") then
  125.             Exit Sub
  126.         End If
  127.  
  128.     i = 0
  129.     WScript.Echo WbemObjectSet.Count & " Pagefile(s) detected:"
  130.     For Each objInst In WbemObjectSet
  131.         i = i + 1
  132.         Call WriteLine ("", objOutputFile)
  133.         Call WriteLine ("File " & Cstr(i) & " = " & objInst.Name, _
  134.                                 objOutputFile)
  135.         Call WriteLine ("Initial Size: " & objInst.InitialSize & _
  136.                                 "M", objOutputFile)
  137.         Call WriteLine ("Maximum Size: " & objInst.MaximumSize & _
  138.                                 "M", objOutputFile)
  139.         Call WriteLine ("Free Space: " & objInst.FreeSpace & "M", _
  140.                                  objOutputFile)
  141.         Call WriteLine ("Installed " & strFormatMOFTime _
  142.                                (objInst.InstallDate), objOutputFile)
  143.         Call WriteLine ("Status: " & objInst.Status, objOutputFile)
  144.     Next 
  145.  
  146.     If IsObject(objOutputFile) Then
  147.         objOutputFile.Close
  148.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  149.     End If
  150.  
  151. End Sub
  152.  
  153. '********************************************************************
  154. '*
  155. '* Sub ResizePageFile()
  156. '*
  157. '* Purpose: Lists data on pagefiles.
  158. '*
  159. '* Input:   strServer           a machine name
  160. '*          strOutputFile       an output file name
  161. '*          strUserName         the current user's name
  162. '*          strPassword         the current user's password
  163. '*          strOldName          the name of the target pagefile
  164. '*          intMax              the maximum size
  165. '*          intMin              the minimum size
  166. '*          blnReboot           reboot after operation
  167. '*          blnForce            force reboot
  168. '*
  169. '* Output:  Results are either printed on screen or saved in strOutputFile.
  170. '*
  171. '********************************************************************
  172. Private Sub ResizePageFile(strServer     , _
  173.                            strOutputFile , _
  174.                            strUserName   , _
  175.                            strPassword   , _
  176.                            strOldName    , _
  177.                            intMax        , _
  178.                            intMin        , _
  179.                            blnReboot     , _
  180.                            blnForce        )
  181.  
  182.     ON ERROR RESUME NEXT
  183.  
  184.     Dim objFileSystem, objOutputFile, objService, obj, objPutResult
  185.     Dim strQuery
  186.     
  187.     'Open a text file for output if the file is requested
  188.     If Not IsEmpty(strOutputFile) Then
  189.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  190.             Call Wscript.Echo ("Could not open an output file.")
  191.             Exit Sub
  192.         End If
  193.     End If
  194.  
  195.     'Establish a connection with the server.
  196.     If blnConnect("root\cimv2" , _
  197.                    strUserName , _
  198.                    strPassword , _
  199.                    strServer   , _
  200.                    objService  ) Then
  201.         Call Wscript.Echo("")
  202.         Call Wscript.Echo("Please check the server name, " _
  203.                         & "credentials and WBEM Core.")
  204.         Exit Sub
  205.     End If
  206.  
  207.     'Ensure query key is nonempty
  208.     If isEmpty(strOldName) then 
  209.         Wscript.echo "The pagefile must have a name to be resized."
  210.         Wscript.quit
  211.     end if
  212.     If isEmpty (intMin) then 
  213.         Wscript.echo "Pagefile resize requested but new size not specified."
  214.         Wscript.quit
  215.     End if
  216.     'Check that sizes are reasonable?
  217.  
  218.     'Grab PageFile Instance by its filename
  219.     strQuery = "Win32_PageFile=""" & strDoubleBackSlashes _
  220.                    (strOldName) & """"
  221.     set obj = objService.Get (strQuery)
  222.     If blnErrorOccurred(" getting an instance of the PageFile " _
  223.            & strOldName) then Exit Sub
  224.  
  225.     'Check that sizes are different
  226.     If obj.InitialSize = intMin and obj.MaximumSize = intMax then
  227.         call Print ("Resize already matches existing pagefile.")
  228.         Wscript.quit
  229.     End If
  230.  
  231.     obj.InitialSize = intMin
  232.     If not isEmpty (intMax) then obj.MaximumSize = intMax
  233.  
  234.     set objPutResult = obj.Put_(1)
  235.     If blnErrorOccurred(" trying to resize the PageFile") then Exit Sub
  236.     Call WriteLine ("Resize of " & strOldName & " to " _
  237.                 & CStr(intMin) & "-" & CStr(intMax) & "M", objOutputFile)
  238.  
  239.     'Alert user of need to reboot before settings will take effect
  240.     If blnReboot then
  241.         Call Reboot (objService, strServer, blnForce, objOutputFile)
  242.     Else
  243.         Call Print ("The changes will not take effect until server restarts.")
  244.     End If
  245.  
  246.     If IsObject(objOutputFile) Then
  247.         objOutputFile.Close
  248.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  249.     End If
  250.  
  251. End Sub
  252.  
  253. '********************************************************************
  254. '*
  255. '* Sub Reboot()
  256. '*
  257. '* Purpose: Lists data on pagefiles.
  258. '*
  259. '* Input:   objService           the WBEM object
  260. '*          strServer            the target machine
  261. '*          blnForce             Force a reboot
  262. '*
  263. '* Output:  Results are either printed on screen or saved in strOutputFile.
  264. '*
  265. '********************************************************************
  266. Sub Reboot (objService, strServer, blnForce, objOutputFile)
  267.  
  268.     Dim objEnumerator, objInstance
  269.     Dim strQuery, strMessage
  270.     Dim intStatus
  271.  
  272.     strQuery = "Select * From Win32_OperatingSystem"
  273.  
  274.     Set objEnumerator = objService.ExecQuery (strQuery,,0)
  275.     If blnErrorOccurred(" during the query.") Then Exit Sub
  276.  
  277.     For Each objInstance in objEnumerator
  278.  
  279.         If objInstance is nothing Then
  280.             Exit Sub
  281.         Else
  282.             If blnForce Then
  283.                 intStatus = objInstance.Win32ShutDown (CONST_REBOOT_FORCE)
  284.             Else
  285.                 intStatus = objInstance.Win32ShutDown (CONST_REBOOT_NOFORCE)
  286.             End If
  287.             If blnErrorOccurred(".") Then
  288.                 strMessage = "Failed to reboot machine " & strServer & "."
  289.             Else
  290.                 strMessage = "Rebooting machine " & strServer & "..."
  291.             End If
  292.             Call WriteLine( strMessage, objOutputFile)
  293.         End If
  294.     Next
  295.  
  296.     If blnErrorOccurred(" trying to reboot server " & strServer) Then
  297.         Exit Sub
  298.     End If
  299.  
  300. End Sub
  301.  
  302. '********************************************************************
  303. '*
  304. '* Function intParseCmdLine()
  305. '*
  306. '* Purpose: Parses the command line.
  307. '* Input:   
  308. '*
  309. '* Output:  strServer         a remote server ("" = local server")
  310. '*          strUserName       the current user's name
  311. '*          strPassword       the current user's password
  312. '*          strOutputFile     an output file name
  313. '*
  314. '********************************************************************
  315. Private Function intParseCmdLine(strServer      ,  _
  316.                                  strUserName    ,  _
  317.                                  strPassword    ,  _
  318.                                  strOutputFile  ,  _
  319.                                  intMin         ,  _
  320.                                  intMax         ,  _
  321.                                  strOldName     ,  _
  322.                                  blnReboot      ,  _
  323.                                  blnForce          )
  324.  
  325.     ON ERROR RESUME NEXT
  326.  
  327.     Dim strFlag
  328.     Dim intState, intArgIter
  329.     Dim objFileSystem
  330.  
  331.     If Wscript.Arguments.Count > 0 Then
  332.         strFlag = Wscript.arguments.Item(0)
  333.     End If
  334.  
  335.     If IsEmpty(strFlag) Then                'No arguments have been received
  336.         intParseCmdLine = CONST_LIST
  337.         Exit Function
  338.     End If
  339.  
  340.     'Check if the user is asking for help or is just confused
  341.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  342.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  343.         OR (strFlag="h") Then
  344.         intParseCmdLine = CONST_SHOW_USAGE
  345.         Exit Function
  346.     End If
  347.  
  348.     'Retrieve the command line and set appropriate variables
  349.      intArgIter = 0
  350.     Do While intArgIter <= Wscript.arguments.Count - 1
  351.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  352.   
  353.             Case "/s"
  354.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  355.                     intParseCmdLine = CONST_ERROR
  356.                     Exit Function
  357.                 End If
  358.                 intArgIter = intArgIter + 1
  359.  
  360.             Case "/o"
  361.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  362.                     intParseCmdLine = CONST_ERROR
  363.                     Exit Function
  364.                 End If
  365.                 intArgIter = intArgIter + 1
  366.  
  367.             Case "/u"
  368.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  369.                     intParseCmdLine = CONST_ERROR
  370.                     Exit Function
  371.                 End If
  372.                 intArgIter = intArgIter + 1
  373.  
  374.             Case "/w"
  375.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  376.                     intParseCmdLine = CONST_ERROR
  377.                     Exit Function
  378.                 End If
  379.                 intArgIter = intArgIter + 1
  380.  
  381.             Case "/l"
  382.                 intParseCmdLine = CONST_LIST
  383.                 intArgIter = intArgIter + 1
  384.  
  385.             Case "/r"
  386.                 intParseCmdLine = CONST_RESIZE
  387.                 intArgIter = intArgIter + 1
  388.  
  389.             Case "/p"
  390.                 If Not blnGetArg("Page File Name", strOldName, intArgIter) Then
  391.                     intParseCmdLine = CONST_ERROR
  392.                     Exit Function
  393.                 End If
  394.                 intArgIter = intArgIter + 1
  395.  
  396.             Case "/m"
  397.                 If Not blnGetArg("Max", intMax, intArgIter) Then
  398.                     intParseCmdLine = CONST_ERROR
  399.                     Exit Function
  400.                 End If
  401.                 intArgIter = intArgIter + 1
  402.  
  403.             Case "/z"
  404.                 If Not blnGetArg("Min", intMin, intArgIter) Then
  405.                     intParseCmdLine = CONST_ERROR
  406.                     Exit Function
  407.                 End If
  408.                 intArgIter = intArgIter + 1
  409.  
  410.             Case "/b"
  411.                 blnReboot = True
  412.                 intArgIter = intArgIter + 1
  413.  
  414.             Case "/f"
  415.                 blnforce = True
  416.                 intArgIter = intArgIter + 1
  417.  
  418.             Case Else 'We shouldn't get here
  419.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  420.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  421.                    & "Please check the input and try again," & vbCRLF _
  422.                    & "or invoke with '/?' for help with the syntax.")
  423.                 Wscript.Quit
  424.  
  425.         End Select
  426.  
  427.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  428.  
  429.     If IsEmpty(intParseCmdLine) Then 
  430.         intParseCmdLine = CONST_LIST
  431.     End If
  432.     If intParseCmdLine = CONST_RESIZE then
  433.         If IsEmpty(intMax) then            
  434.             intParseCmdLine = CONST_ERROR
  435.             Wscript.Echo ("Missing required argurments.")
  436.             Exit Function
  437.         ElseIf IsEmpty(intMin) then
  438.             intParseCmdLine = CONST_ERROR
  439.             Wscript.Echo ("Missing required argurments.")
  440.             Exit Function
  441.         End If
  442.         if intMin > intMax then
  443.             Wscript.Echo ("The minimum pagefile size must be equal to or less than the maximum")
  444.             Wscript.echo ("pagefile size.")
  445.             intParseCmdLine = CONST_ERROR
  446.             Exit Function
  447.         End if
  448.     End If
  449.     
  450. End Function
  451.  
  452. '********************************************************************
  453. '*
  454. '* Sub ShowUsage()
  455. '*
  456. '* Purpose: Shows the correct usage to the user.
  457. '*
  458. '* Input:   None
  459. '*
  460. '* Output:  Help messages are displayed on screen.
  461. '*
  462. '********************************************************************
  463. Private Sub ShowUsage()
  464.  
  465.     Wscript.Echo ""
  466.     Wscript.Echo "Controls Pagefiles on a machine."
  467.     Wscript.Echo ""
  468.     Wscript.Echo "SYNTAX:"
  469.     Wscript.Echo "1.  PageFile.vbs /L [/S <server>] [/U <username>]"
  470.     Wscript.Echo "                [/W <password>] [/O <outputfile>]"
  471.     Wscript.Echo ""
  472.     Wscript.Echo "2.  PageFile.vbs /R /M <maxsize> /Z <minsize> /P " _
  473.                & "<pagefile> [/B] [/F]"
  474.     Wscript.Echo "                [/S <server>] [/U <username>] [/W <password>]"
  475.     Wscript.Echo "                [/O <outputfile>]"
  476.     Wscript.Echo ""
  477.     Wscript.Echo "PARAMETER SPECIFIERS:"
  478.     Wscript.Echo "   /L            List page file data."
  479.     Wscript.Echo "   /R            Change the pafe file size."
  480.     Wscript.Echo "   /B            Reboot."
  481.     Wscript.Echo "   /F            Force Reboot."
  482.     Wscript.Echo "   pagefile      The target pagefile name."
  483.     Wscript.Echo "   maxsize       The largest size the pagefile can grow."
  484.     Wscript.Echo "   minsize       The minimum size of the pagefile."
  485.     Wscript.Echo "   server        A machine name."
  486.     Wscript.Echo "   username      The current user's name."
  487.     Wscript.Echo "   password      Password of the current user."
  488.     Wscript.Echo "   outputfile    The output file name."
  489.     Wscript.Echo ""
  490.     Wscript.Echo "EXAMPLE:"
  491.     Wscript.Echo "1. cscript PageFile.vbs /L"
  492.     Wscript.Echo "   Get the pagefile data for the current machine."
  493.     Wscript.Echo "2. cscript PageFile.vbs /R /M 150 /Z 100 /p " _
  494.                & "c:\pagefile.sys /S MyMachine2"
  495.     Wscript.Echo "   Change the pagefile size on machine MyMachine2."
  496.  
  497. End Sub
  498.  
  499. '********************************************************************
  500. '* General Routines
  501. '********************************************************************
  502.  
  503. '********************************************************************
  504. '*  Function: strDoubleBackSlashes (strIn)
  505. '*
  506. '*  Purpose:  expand path string to use double node-delimiters;
  507. '*            doubles ALL backslashes.
  508. '*
  509. '*  Input:    strIn    path to file or directory
  510. '*  Output:            WMI query-food
  511. '*
  512. '*  eg:      c:\pagefile.sys     becomes   c:\\pagefile.sys
  513. '*  but:     \\server\share\     becomes   \\\\server\\share\\
  514. '*
  515. '********************************************************************
  516. Private Function strDoubleBackSlashes (strIn)
  517.     Dim i, str, strC
  518.     str = ""
  519.     for i = 1 to len (strIn)
  520.         strC = Mid (strIn, i, 1)
  521.         str = str & strC
  522.         if strC = "\" then str = str & strC
  523.     next
  524.     strDoubleBackSlashes = str
  525. End Function
  526.  
  527. '********************************************************************
  528. '*
  529. '* Function strFormatMOFTime(strDate)
  530. '*
  531. '* Purpose: Formats the date in WBEM to a readable Date
  532. '*
  533. '* Input:   blnB    A WBEM Date
  534. '*
  535. '* Output:  a string 
  536. '*
  537. '********************************************************************
  538.  
  539. Private Function strFormatMOFTime(strDate)
  540.     Dim str
  541.     str = Mid(strDate,1,4) & "-" _
  542.            & Mid(strDate,5,2) & "-" _
  543.            & Mid(strDate,7,2) & ", " _
  544.            & Mid(strDate,9,2) & ":" _
  545.            & Mid(strDate,11,2) & ":" _
  546.            & Mid(strDate,13,2)
  547.     strFormatMOFTime = str
  548. End Function
  549.  
  550. '********************************************************************
  551. '*
  552. '* Function strPackString()
  553. '*
  554. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  555. '*
  556. '* Input:   strString   a string
  557. '*          intWidth    the intended length of the string
  558. '*          blnAfter    Should spaces be added after the string?
  559. '*          blnTruncate specifies whether to truncate the string or not if
  560. '*                      the string length is longer than intWidth
  561. '*
  562. '* Output:  strPackString is returned as the packed string.
  563. '*
  564. '********************************************************************
  565. Private Function strPackString( ByVal strString, _
  566.                                 ByVal intWidth,  _
  567.                                 ByVal blnAfter,  _
  568.                                 ByVal blnTruncate)
  569.  
  570.     ON ERROR RESUME NEXT
  571.  
  572.     intWidth      = CInt(intWidth)
  573.     blnAfter      = CBool(blnAfter)
  574.     blnTruncate   = CBool(blnTruncate)
  575.  
  576.     If Err.Number Then
  577.         Call Wscript.Echo ("Argument type is incorrect!")
  578.         Err.Clear
  579.         Wscript.Quit
  580.     End If
  581.  
  582.     If IsNull(strString) Then
  583.         strPackString = "null" & Space(intWidth-4)
  584.         Exit Function
  585.     End If
  586.  
  587.     strString = CStr(strString)
  588.     If Err.Number Then
  589.         Call Wscript.Echo ("Argument type is incorrect!")
  590.         Err.Clear
  591.         Wscript.Quit
  592.     End If
  593.  
  594.     If intWidth > Len(strString) Then
  595.         If blnAfter Then
  596.             strPackString = strString & Space(intWidth-Len(strString))
  597.         Else
  598.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  599.         End If
  600.     Else
  601.         If blnTruncate Then
  602.             strPackString = Left(strString, intWidth-1) & " "
  603.         Else
  604.             strPackString = strString & " "
  605.         End If
  606.     End If
  607.  
  608. End Function
  609.  
  610. '********************************************************************
  611. '* 
  612. '*  Function blnGetArg()
  613. '*
  614. '*  Purpose: Helper to intParseCmdLine()
  615. '* 
  616. '*  Usage:
  617. '*
  618. '*     Case "/s" 
  619. '*       blnGetArg ("server name", strServer, intArgIter)
  620. '*
  621. '********************************************************************
  622. Private Function blnGetArg ( ByVal StrVarName,   _
  623.                              ByRef strVar,       _
  624.                              ByRef intArgIter) 
  625.  
  626.     blnGetArg = False 'failure, changed to True upon successful completion
  627.  
  628.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  629.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  630.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  631.                 strVar = Right(Wscript.Arguments(intArgIter), _
  632.                          Len(Wscript.Arguments(intArgIter)) - 3)
  633.                 blnGetArg = True
  634.                 Exit Function
  635.             Else
  636.                 intArgIter = intArgIter + 1
  637.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  638.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  639.                     Call Wscript.Echo( "Please check the input and try again.")
  640.                     Exit Function
  641.                 End If
  642.  
  643.                 strVar = Wscript.Arguments.Item(intArgIter)
  644.                 If Err.Number Then
  645.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  646.                     Call Wscript.Echo( "Please check the input and try again.")
  647.                     Exit Function
  648.                 End If
  649.  
  650.                 If InStr(strVar, "/") Then
  651.                     Call Wscript.Echo( "Invalid " & StrVarName)
  652.                     Call Wscript.Echo( "Please check the input and try again.")
  653.                     Exit Function
  654.                 End If
  655.  
  656.                 blnGetArg = True 'success
  657.             End If
  658.         Else
  659.             strVar = Right(Wscript.Arguments(intArgIter), _
  660.                      Len(Wscript.Arguments(intArgIter)) - 2)
  661.             blnGetArg = True 'success
  662.             Exit Function
  663.         End If
  664.     Else
  665.         intArgIter = intArgIter + 1
  666.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  667.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  668.             Call Wscript.Echo( "Please check the input and try again.")
  669.             Exit Function
  670.         End If
  671.  
  672.         strVar = Wscript.Arguments.Item(intArgIter)
  673.         If Err.Number Then
  674.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  675.             Call Wscript.Echo( "Please check the input and try again.")
  676.             Exit Function
  677.         End If
  678.  
  679.         If InStr(strVar, "/") Then
  680.             Call Wscript.Echo( "Invalid " & StrVarName)
  681.             Call Wscript.Echo( "Please check the input and try again.")
  682.             Exit Function
  683.         End If
  684.         blnGetArg = True 'success
  685.     End If
  686. End Function
  687.  
  688. '********************************************************************
  689. '*
  690. '* Function blnConnect()
  691. '*
  692. '* Purpose: Connects to machine strServer.
  693. '*
  694. '* Input:   strServer       a machine name
  695. '*          strNameSpace    a namespace
  696. '*          strUserName     name of the current user
  697. '*          strPassword     password of the current user
  698. '*
  699. '* Output:  objService is returned  as a service object.
  700. '*          strServer is set to local host if left unspecified
  701. '*
  702. '********************************************************************
  703. Private Function blnConnect(ByVal strNameSpace, _
  704.                             ByVal strUserName,  _
  705.                             ByVal strPassword,  _
  706.                             ByRef strServer,    _
  707.                             ByRef objService)
  708.  
  709.     ON ERROR RESUME NEXT
  710.  
  711.     Dim objLocator, objWshNet
  712.  
  713.     blnConnect = False     'There is no error.
  714.  
  715.     'Create Locator object to connect to remote CIM object manager
  716.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  717.     If Err.Number then
  718.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  719.                            " occurred in creating a locator object." )
  720.         If Err.Description <> "" Then
  721.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  722.         End If
  723.         Err.Clear
  724.         blnConnect = True     'An error occurred
  725.         Exit Function
  726.     End If
  727.  
  728.     'Connect to the namespace which is either local or remote
  729.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  730.        strUserName, strPassword)
  731.     ObjService.Security_.impersonationlevel = 3
  732.     If Err.Number then
  733.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  734.                            " occurred in connecting to server " _
  735.            & strServer & ".")
  736.         If Err.Description <> "" Then
  737.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  738.         End If
  739.         Err.Clear
  740.         blnConnect = True     'An error occurred
  741.     End If
  742.  
  743.     'Get the current server's name if left unspecified
  744.     If IsEmpty(strServer) Then
  745.         Set objWshNet = CreateObject("Wscript.Network")
  746.     strServer     = objWshNet.ComputerName
  747.     End If
  748.  
  749. End Function
  750.  
  751. '********************************************************************
  752. '*
  753. '* Sub      VerifyHostIsCscript()
  754. '*
  755. '* Purpose: Determines which program is used to run this script.
  756. '*
  757. '* Input:   None
  758. '*
  759. '* Output:  If host is not cscript, then an error message is printed 
  760. '*          and the script is aborted.
  761. '*
  762. '********************************************************************
  763. Sub VerifyHostIsCscript()
  764.  
  765.     ON ERROR RESUME NEXT
  766.  
  767.     Dim strFullName, strCommand, i, j, intStatus
  768.  
  769.     strFullName = WScript.FullName
  770.  
  771.     If Err.Number then
  772.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  773.         If Err.Description <> "" Then
  774.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  775.         End If
  776.         intStatus =  CONST_ERROR
  777.     End If
  778.  
  779.     i = InStr(1, strFullName, ".exe", 1)
  780.     If i = 0 Then
  781.         intStatus =  CONST_ERROR
  782.     Else
  783.         j = InStrRev(strFullName, "\", i, 1)
  784.         If j = 0 Then
  785.             intStatus =  CONST_ERROR
  786.         Else
  787.             strCommand = Mid(strFullName, j+1, i-j-1)
  788.             Select Case LCase(strCommand)
  789.                 Case "cscript"
  790.                     intStatus = CONST_CSCRIPT
  791.                 Case "wscript"
  792.                     intStatus = CONST_WSCRIPT
  793.                 Case Else       'should never happen
  794.                     Call Wscript.Echo( "An unexpected program was used to " _
  795.                                        & "run this script." )
  796.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  797.                                        & "be used to run this script." )
  798.                     intStatus = CONST_ERROR
  799.                 End Select
  800.         End If
  801.     End If
  802.  
  803.     If intStatus <> CONST_CSCRIPT Then
  804.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  805.              "This can be achieved by" & vbCRLF & _
  806.              "1. Using ""CScript Pagefile.vbs arguments"" for Windows 95/98 or" _
  807.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  808.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  809.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  810.              "    ""Pagefile.vbs arguments"" for Windows NT/2000." )
  811.         WScript.Quit
  812.     End If
  813.  
  814. End Sub
  815.  
  816. '********************************************************************
  817. '*
  818. '* Sub WriteLine()
  819. '* Purpose: Writes a text line either to a file or on screen.
  820. '* Input:   strMessage  the string to print
  821. '*          objFile     an output file object
  822. '* Output:  strMessage is either displayed on screen or written to a file.
  823. '*
  824. '********************************************************************
  825. Sub WriteLine(ByVal strMessage, ByVal objFile)
  826.  
  827.     On Error Resume Next
  828.     If IsObject(objFile) then        'objFile should be a file object
  829.         objFile.WriteLine strMessage
  830.     Else
  831.         Call Wscript.Echo( strMessage )
  832.     End If
  833.  
  834. End Sub
  835.  
  836. '********************************************************************
  837. '* 
  838. '* Function blnErrorOccurred()
  839. '*
  840. '* Purpose: Reports error with a string saying what the error occurred in.
  841. '*
  842. '* Input:   strIn        string saying what the error occurred in.
  843. '*
  844. '* Output:  displayed on screen 
  845. '* 
  846. '********************************************************************
  847. Private Function blnErrorOccurred (ByVal strIn)
  848.  
  849.     If Err.Number Then
  850.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  851.         If Err.Description <> "" Then
  852.             Call Wscript.Echo( "Error description: " & Err.Description)
  853.         End If
  854.         Err.Clear
  855.         blnErrorOccurred = True
  856.     Else
  857.         blnErrorOccurred = False
  858.     End If
  859.  
  860. End Function
  861.  
  862. '********************************************************************
  863. '* 
  864. '* Function blnOpenFile
  865. '*
  866. '* Purpose: Opens a file.
  867. '*
  868. '* Input:   strFileName        A string with the name of the file.
  869. '*
  870. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  871. '*            Nothing upon Failure.
  872. '* 
  873. '********************************************************************
  874. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  875.  
  876.     ON ERROR RESUME NEXT
  877.  
  878.     Dim objFileSystem
  879.  
  880.     Set objFileSystem = Nothing
  881.  
  882.     If IsEmpty(strFileName) OR strFileName = "" Then
  883.         blnOpenFile = False
  884.         Set objOpenFile = Nothing
  885.         Exit Function
  886.     End If
  887.  
  888.     'Create a file object
  889.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  890.     If blnErrorOccurred("Could not create filesystem object.") Then
  891.         blnOpenFile = False
  892.         Set objOpenFile = Nothing
  893.         Exit Function
  894.     End If
  895.  
  896.     'Open the file for output
  897.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  898.     If blnErrorOccurred("Could not open") Then
  899.         blnOpenFile = False
  900.         Set objOpenFile = Nothing
  901.         Exit Function
  902.     End If
  903.     blnOpenFile = True
  904.  
  905. End Function
  906.  
  907. '********************************************************************
  908. '*                                                                  *
  909. '*                           End of File                            *
  910. '*                                                                  *
  911. '********************************************************************