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

  1. '********************************************************************
  2. '*
  3. '* File:           OsReconfig.vbs
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Get the OS recovery configuration for a machine
  8. '*
  9. '*  OsReconfig.vbs  [/S <server>] [/U <username>] [/W <password>] 
  10. '*                   [/O <outputfile>] [/T <code>]
  11. '*
  12. '* Copyright (C) 1998 Microsoft Corporation
  13. '*
  14. '********************************************************************
  15. OPTION EXPLICIT
  16.  
  17.     'Define constants
  18.     CONST CONST_ERROR                   = 0
  19.     CONST CONST_WSCRIPT                 = 1
  20.     CONST CONST_CSCRIPT                 = 2
  21.     CONST CONST_SHOW_USAGE              = 3
  22.     CONST CONST_LIST                    = 4
  23.     CONST CONST_TOGGLE                  = 5
  24.  
  25.     'Declare variables
  26.     Dim intOpMode, i
  27.     Dim strServer, strUserName, strPassword, strOutputFile, strCode
  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.                                 strCode          )
  38.  
  39.  
  40.     Select Case intOpMode
  41.  
  42.         Case CONST_SHOW_USAGE
  43.             Call ShowUsage()
  44.  
  45.         Case CONST_LIST                 
  46.             Call GetOSReconfig(strServer     , _
  47.                                 strOutputFile , _
  48.                                 strUserName   , _
  49.                                 strPassword     )
  50.  
  51.         Case CONST_TOGGLE                 
  52.             Call ToggleOSConfig(strServer     , _
  53.                                 strOutputFile , _
  54.                                 strUserName   , _
  55.                                 strPassword   , _
  56.                                 strCode         )
  57.  
  58.         Case CONST_ERROR
  59.             'Do Nothing
  60.  
  61.         Case Else                    'Default -- should never happen
  62.             Call Wscript.Echo("Error occurred in passing parameters.")
  63.  
  64.     End Select
  65.  
  66. '********************************************************************
  67. '*
  68. '* Sub GetOSReconfig()
  69. '*
  70. '* Purpose: Get the OS recovery configuration for a machine.
  71. '*
  72. '* Input:   strServer           a machine name
  73. '*          strOutputFile       an output file name
  74. '*          strUserName         the current user's name
  75. '*          strPassword         the current user's password
  76. '*
  77. '* Output:  Results are either printed on screen or saved in strOutputFile.
  78. '*
  79. '********************************************************************
  80. Private Sub GetOSReconfig(strServer, strOutputFile, strUserName, strPassword)
  81.  
  82.     ON ERROR RESUME NEXT
  83.  
  84.     Dim objFileSystem, objOutputFile, objService, objOsConSet, objOsCon
  85.     Dim strQuery, strMessage, strWBEMClass
  86.  
  87.     strWBEMClass = "Win32_OSRecoveryConfiguration"
  88.  
  89.     'Open a text file for output if the file is requested
  90.     If Not IsEmpty(strOutputFile) Then
  91.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  92.             Call Wscript.Echo ("Could not open an output file.")
  93.             Exit Sub
  94.         End If
  95.     End If
  96.  
  97.     'Establish a connection with the server.
  98.     If blnConnect("root\cimv2" , _
  99.                    strUserName , _
  100.                    strPassword , _
  101.                    strServer   , _
  102.                    objService  ) Then
  103.         Call Wscript.Echo("")
  104.         Call Wscript.Echo("Please check the server name, " _
  105.                         & "credentials and WBEM Core.")
  106.         Exit Sub
  107.     End If
  108.  
  109.     'Get the set
  110.     Set objOsConSet = objService.InstancesOf(strWBEMClass)
  111.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  112.         Exit Sub
  113.     End If
  114.  
  115.     If objOsConSet.Count = 0 Then
  116.         Call WriteLine("No OS recovery information is available.", _
  117.                         objOutputFile)    
  118.         Exit Sub
  119.     End If
  120.  
  121.     Call WriteLine("OS recovery information for Machine " & _
  122.                    strServer, objOutputFile)
  123.     Call WriteLine("", objOutputFile)
  124.  
  125.     For Each objOsCon In objOsConSet
  126.         Call WriteLine("Name                          = " & _
  127.              ParseNameFirst(objOsCon.Name), objOutputFile)
  128.         Call WriteLine("Boot Path                     = " & _
  129.              ParseNameSecond(objOsCon.Name), objOutputFile)
  130.         Call WriteLine("Boot Partition                = " & _
  131.              ParseNameThird(objOsCon.Name), objOutputFile)
  132.         Call WriteLine("Debug File Path               = " & _
  133.              objOsCon.DebugFilePath, objOutputFile)
  134.         Call WriteLine("AutoReboot                    = " & _
  135.              objOsCon.AutoReboot, objOutputFile)
  136.         Call WriteLine("Kernel Dump Only              = " & _
  137.              objOsCon.KernelDumpOnly, objOutputFile)
  138.         Call WriteLine("Overwrite Existing Debug File = " & _
  139.              objOsCon.OverwriteExistingDebugFile, objOutputFile)
  140.         Call WriteLine("Send Admin Alert              = " & _
  141.              objOsCon.SendAdminAlert, objOutputFile)
  142.         Call WriteLine("Write Debug Info              = " & _
  143.              objOsCon.WriteDebugInfo, objOutputFile)
  144.         Call WriteLine("Write To System Log           = " & _
  145.              objOsCon.WriteToSystemLog, objOutputFile)
  146.         Call WriteLine("", objOutputFile)
  147.     Next
  148.  
  149.     If IsObject(objOutputFile) Then
  150.         objOutputFile.Close
  151.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  152.     End If
  153.  
  154. End Sub
  155.  
  156. '********************************************************************
  157. '*
  158. '* Sub ToggleOSConfig()
  159. '*
  160. '* Purpose: Toggle various boolean OS recovery settings.
  161. '*
  162. '* Input:   strServer           a machine name
  163. '*          strOutputFile       an output file name
  164. '*          strUserName         the current user's name
  165. '*          strPassword         the current user's password
  166. '*          strCode             one of the following 2 letter codes that toggles
  167. '*                              the recovery configuration
  168. '*
  169. '*                              Code      Description
  170. '*
  171. '*                               WS       WriteToSystemLog
  172. '*                               WD       WriteDebugInfo
  173. '*                               SA       SendAdminAlert
  174. '*                               OD       OverwriteExistingDebugFile
  175. '*                               KD       KernelDumpOnly
  176. '*                               AR       AutoReboot
  177. '*
  178. '* Output:  Modifies the revovery state.
  179. '*
  180. '********************************************************************
  181. Private Sub ToggleOSConfig( ByVal strServer,      _
  182.                             ByVal strOutputFile,  _ 
  183.                             ByVal strUserName,    _
  184.                             ByVal strPassword,    _
  185.                             ByVal strCode)
  186.  
  187.     ON ERROR RESUME NEXT
  188.  
  189.     Dim objFileSystem, objOutputFile, objService, objOsConSet, objOsCon
  190.     Dim strQuery, strMessage, strWBEMClass
  191.  
  192.     strWBEMClass = "Win32_OSRecoveryConfiguration"
  193.  
  194.     If IsEmpty(strCode) Then
  195.         Call Wscript.Echo ("Invalid code. Check your input and try again.")
  196.         Exit Sub
  197.     End If
  198.  
  199.     'Open a text file for output if the file is requested
  200.     If Not IsEmpty(strOutputFile) Then
  201.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  202.             Call Wscript.Echo ("Could not open an output file.")
  203.             Exit Sub
  204.         End If
  205.     End If
  206.  
  207.     'Establish a connection with the server.
  208.     If blnConnect("root\cimv2" , _
  209.                    strUserName , _
  210.                    strPassword , _
  211.                    strServer   , _
  212.                    objService  ) Then
  213.         Call Wscript.Echo("")
  214.         Call Wscript.Echo("Please check the server name, " _
  215.                         & "credentials and WBEM Core.")
  216.         Exit Sub
  217.     End If
  218.  
  219.     'Get the set
  220.     Set objOsConSet = objService.InstancesOf(strWBEMClass)
  221.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  222.         Exit Sub
  223.     End If
  224.  
  225.     If objOsConSet.Count = 0 Then
  226.         Call WriteLine("No OS recovery information is available.", objOutputFile)    
  227.         Exit Sub
  228.     End If
  229.  
  230.     'Get the first instance
  231.     Dim i
  232.     Dim objInst
  233.  
  234.     i = 0
  235.     For Each objInst In objOsConSet
  236.         i = i + 1
  237.         If i = 1 Then Set objOsCon = objInst
  238.     Next
  239.  
  240.     strCode = LCase(strCode)
  241.  
  242.     Select Case strCode
  243.         Case "ws"
  244.             objOsCon.WriteToSystemLog           = NOT objOsCon.WriteToSystemLog
  245.         Case "wd"
  246.             objOsCon.WriteDebugInfo             = NOT objOsCon.WriteDebugInfo
  247.         Case "sa"
  248.             objOsCon.SendAdminAlert             = NOT objOsCon.SendAdminAlert
  249.         Case "od"
  250.             objOsCon.OverwriteExistingDebugFile = NOT _
  251.                      objOsCon.OverwriteExistingDebugFile
  252.         Case "kd"
  253.             objOsCon.KernelDumpOnly             = NOT objOsCon.KernelDumpOnly
  254.         Case "ar"
  255.             objOsCon.AutoReboot                 = NOT objOsCon.AutoReboot
  256.         Case Else
  257.             Wscript.Echo "Invalid Code: Configuration not changed."
  258.             objOutputFile.Close
  259.             Exit Sub
  260.     End Select
  261.  
  262.     Call objOsCon.Put_()
  263.     Call WriteLine("OS recovery configuration has been changed.", objOutputFile)  
  264.  
  265.     If IsObject(objOutputFile) Then
  266.         objOutputFile.Close
  267.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  268.     End If
  269.  
  270. End Sub
  271.  
  272. '********************************************************************
  273. '*
  274. '* Function intParseCmdLine()
  275. '*
  276. '* Purpose: Parses the command line.
  277. '* Input:   
  278. '*
  279. '* Output:  strServer         a remote server ("" = local server")
  280. '*          strUserName       the current user's name
  281. '*          strPassword       the current user's password
  282. '*          strOutputFile     an output file name
  283. '*
  284. '********************************************************************
  285. Private Function intParseCmdLine( ByRef strServer,        _
  286.                                   ByRef strUserName,      _
  287.                                   ByRef strPassword,      _
  288.                                   ByRef strOutputFile,    _
  289.                                   ByRef strCode           )
  290.  
  291.  
  292.     ON ERROR RESUME NEXT
  293.  
  294.     Dim strFlag
  295.     Dim intState, intArgIter
  296.     Dim objFileSystem
  297.  
  298.     If Wscript.Arguments.Count > 0 Then
  299.         strFlag = Wscript.arguments.Item(0)
  300.     End If
  301.  
  302.     If IsEmpty(strFlag) Then                'No arguments have been received
  303.         intParseCmdLine = CONST_LIST
  304.         Exit Function
  305.     End If
  306.  
  307.     'Check if the user is asking for help or is just confused
  308.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  309.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  310.         OR (strFlag="h") Then
  311.         intParseCmdLine = CONST_SHOW_USAGE
  312.         Exit Function
  313.     End If
  314.  
  315.     'Retrieve the command line and set appropriate variables
  316.      intArgIter = 0
  317.     Do While intArgIter <= Wscript.arguments.Count - 1
  318.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  319.   
  320.             Case "/s"
  321.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  322.                     intParseCmdLine = CONST_ERROR
  323.                     Exit Function
  324.                 End If
  325.                 intArgIter = intArgIter + 1
  326.  
  327.             Case "/o"
  328.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  329.                     intParseCmdLine = CONST_ERROR
  330.                     Exit Function
  331.                 End If
  332.                 intArgIter = intArgIter + 1
  333.  
  334.             Case "/u"
  335.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  336.                     intParseCmdLine = CONST_ERROR
  337.                     Exit Function
  338.                 End If
  339.                 intArgIter = intArgIter + 1
  340.  
  341.             Case "/w"
  342.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  343.                     intParseCmdLine = CONST_ERROR
  344.                     Exit Function
  345.                 End If
  346.                 intArgIter = intArgIter + 1
  347.  
  348.             Case "/t"
  349.                 intParseCmdLine = CONST_TOGGLE
  350.                 If Not blnGetArg("Code", strCode, intArgIter) Then
  351.                     intParseCmdLine = CONST_ERROR
  352.                     Exit Function
  353.                 End If
  354.                 intArgIter = intArgIter + 1
  355.                 
  356.             Case Else 'We shouldn't get here
  357.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  358.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  359.                    & "Please check the input and try again," & vbCRLF _
  360.                    & "or invoke with '/?' for help with the syntax.")
  361.                 Wscript.Quit
  362.  
  363.         End Select
  364.  
  365.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  366.  
  367.     If IsEmpty(intParseCmdLine) Then _
  368.         intParseCmdLine = CONST_LIST
  369.  
  370. End Function
  371.  
  372. '********************************************************************
  373. '*
  374. '* Sub ShowUsage()
  375. '*
  376. '* Purpose: Shows the correct usage to the user.
  377. '*
  378. '* Input:   None
  379. '*
  380. '* Output:  Help messages are displayed on screen.
  381. '*
  382. '********************************************************************
  383. Private Sub ShowUsage()
  384.  
  385.     Wscript.Echo ""
  386.     Wscript.Echo "Get or toggle the OS recovery configuration for a machine."
  387.     Wscript.Echo ""
  388.     Wscript.Echo "SYNTAX:"
  389.     Wscript.Echo "  OsReconfig.vbs [/S <server>] [/U <username>]" _
  390.                 &" [/W <password>] [/T <code>]"
  391.     Wscript.Echo "  [/O <outputfile>]"
  392.     Wscript.Echo ""
  393.     Wscript.Echo "PARAMETER SPECIFIERS:"
  394.     Wscript.Echo "   code          A two letter code signifying " _
  395.                & "which option to toggle."
  396.     Wscript.Echo "                 The possible codes are list below."
  397.     Wscript.Echo ""
  398.     Wscript.Echo "                 Code      Description"
  399.     Wscript.Echo "                 WS         Write To System Log"
  400.     Wscript.Echo "                 WD         Write Debug Info"
  401.     Wscript.Echo "                 SA         Send Admin Alert"
  402.     Wscript.Echo "                 OD         Overwrite Existing Debug File"
  403.     Wscript.Echo "                 KD         Kernel Dump Only"
  404.     Wscript.Echo "                 AR         Auto Reboot"
  405.     Wscript.Echo ""
  406.     Wscript.Echo "   server        A machine name."
  407.     Wscript.Echo "   username      The current user's name."
  408.     Wscript.Echo "   password      Password of the current user."
  409.     Wscript.Echo "   outputfile    The output file name."
  410.     Wscript.Echo ""
  411.     Wscript.Echo "EXAMPLE:"
  412.     Wscript.Echo "1. cscript OsReconfig.vbs"
  413.     Wscript.Echo "   Get the OS recovery configuration for the current machine."
  414.     Wscript.Echo "2. cscript OsReconfig.vbs /S MyMachine2 /T ws"
  415.     Wscript.Echo "   Toggle the ""Write To System Log"" option " _
  416.                & "for the machine MyMachine2."
  417.  
  418. End Sub
  419.  
  420. '********************************************************************
  421. '* General Routines
  422. '********************************************************************
  423.  
  424. '********************************************************************
  425. '* 
  426. '* Function ParseNameFirst()
  427. '*
  428. '* Purpose: Parses the Name Property into the first line
  429. '*
  430. '* Input:   strName        The Property to be parsed
  431. '*
  432. '* Output:  The First Line of Name property data
  433. '* 
  434. '********************************************************************
  435.  
  436. Private Function ParseNameFirst(strName)
  437.  
  438.     Dim CurrentLetter
  439.     CurrentLetter = 1
  440.     Do Until Mid(strName,CurrentLetter,1) = "|" and Mid(strName,CurrentLetter+2,2) = ":\"
  441.         CurrentLetter = CurrentLetter + 1
  442.     Loop
  443.     ParseNameFirst = Left(strName,CurrentLetter - 1)
  444.  
  445. End Function
  446.  
  447. '********************************************************************
  448. '* 
  449. '* Function ParseNameSecond()
  450. '*
  451. '* Purpose: Parses the Name Property into the second line
  452. '*
  453. '* Input:   strName        The Property to be parsed
  454. '*
  455. '* Output:  The Second Line of Name property data
  456. '* 
  457. '********************************************************************
  458.  
  459. Private Function ParseNameSecond(strName)
  460.     Dim TempString
  461.     Dim EndLetter
  462.  
  463.     TempString = ParseNameFirst(strName)
  464.     TempString = Right(strName,Len(strName)-(Len(TempString)+1))
  465.     EndLetter = 1
  466.     Do until Mid(TempString,EndLetter,2) = "|\"
  467.         EndLetter=EndLetter + 1
  468.     Loop
  469.  
  470.     ParseNameSecond = Left(TempString,EndLetter - 1)
  471.  
  472. End Function
  473.  
  474. '********************************************************************
  475. '* 
  476. '* Function ParseNameThird()
  477. '*
  478. '* Purpose: Parses the Name Property into the second line
  479. '*
  480. '* Input:   strName        The Property to be parsed
  481. '*
  482. '* Output:  The Third Line of Name property data
  483. '* 
  484. '********************************************************************
  485.  
  486. Private Function ParseNameThird(strName)
  487.  
  488.     ParseNameThird = Right(strName,(Len(strName)-(Len(ParseNameFirst(strName)) + Len(ParseNameSecond(strName))+2)))
  489.  
  490. End Function
  491.  
  492. '********************************************************************
  493. '*
  494. '* Function strPackString()
  495. '*
  496. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  497. '*
  498. '* Input:   strString   a string
  499. '*          intWidth    the intended length of the string
  500. '*          blnAfter    Should spaces be added after the string?
  501. '*          blnTruncate specifies whether to truncate the string or not if
  502. '*                      the string length is longer than intWidth
  503. '*
  504. '* Output:  strPackString is returned as the packed string.
  505. '*
  506. '********************************************************************
  507. Private Function strPackString( ByVal strString, _
  508.                                 ByVal intWidth,  _
  509.                                 ByVal blnAfter,  _
  510.                                 ByVal blnTruncate)
  511.  
  512.     ON ERROR RESUME NEXT
  513.  
  514.     intWidth      = CInt(intWidth)
  515.     blnAfter      = CBool(blnAfter)
  516.     blnTruncate   = CBool(blnTruncate)
  517.  
  518.     If Err.Number Then
  519.         Call Wscript.Echo ("Argument type is incorrect!")
  520.         Err.Clear
  521.         Wscript.Quit
  522.     End If
  523.  
  524.     If IsNull(strString) Then
  525.         strPackString = "null" & Space(intWidth-4)
  526.         Exit Function
  527.     End If
  528.  
  529.     strString = CStr(strString)
  530.     If Err.Number Then
  531.         Call Wscript.Echo ("Argument type is incorrect!")
  532.         Err.Clear
  533.         Wscript.Quit
  534.     End If
  535.  
  536.     If intWidth > Len(strString) Then
  537.         If blnAfter Then
  538.             strPackString = strString & Space(intWidth-Len(strString))
  539.         Else
  540.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  541.         End If
  542.     Else
  543.         If blnTruncate Then
  544.             strPackString = Left(strString, intWidth-1) & " "
  545.         Else
  546.             strPackString = strString & " "
  547.         End If
  548.     End If
  549.  
  550. End Function
  551.  
  552. '********************************************************************
  553. '* 
  554. '*  Function blnGetArg()
  555. '*
  556. '*  Purpose: Helper to intParseCmdLine()
  557. '* 
  558. '*  Usage:
  559. '*
  560. '*     Case "/s" 
  561. '*       blnGetArg ("server name", strServer, intArgIter)
  562. '*
  563. '********************************************************************
  564. Private Function blnGetArg ( ByVal StrVarName,   _
  565.                              ByRef strVar,       _
  566.                              ByRef intArgIter) 
  567.  
  568.     blnGetArg = False 'failure, changed to True upon successful completion
  569.  
  570.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  571.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  572.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  573.                 strVar = Right(Wscript.Arguments(intArgIter), _
  574.                          Len(Wscript.Arguments(intArgIter)) - 3)
  575.                 blnGetArg = True
  576.                 Exit Function
  577.             Else
  578.                 intArgIter = intArgIter + 1
  579.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  580.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  581.                     Call Wscript.Echo( "Please check the input and try again.")
  582.                     Exit Function
  583.                 End If
  584.  
  585.                 strVar = Wscript.Arguments.Item(intArgIter)
  586.                 If Err.Number Then
  587.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  588.                     Call Wscript.Echo( "Please check the input and try again.")
  589.                     Exit Function
  590.                 End If
  591.  
  592.                 If InStr(strVar, "/") Then
  593.                     Call Wscript.Echo( "Invalid " & StrVarName)
  594.                     Call Wscript.Echo( "Please check the input and try again.")
  595.                     Exit Function
  596.                 End If
  597.  
  598.                 blnGetArg = True 'success
  599.             End If
  600.         Else
  601.             strVar = Right(Wscript.Arguments(intArgIter), _
  602.                      Len(Wscript.Arguments(intArgIter)) - 2)
  603.             blnGetArg = True 'success
  604.             Exit Function
  605.         End If
  606.     Else
  607.         intArgIter = intArgIter + 1
  608.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  609.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  610.             Call Wscript.Echo( "Please check the input and try again.")
  611.             Exit Function
  612.         End If
  613.  
  614.         strVar = Wscript.Arguments.Item(intArgIter)
  615.         If Err.Number Then
  616.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  617.             Call Wscript.Echo( "Please check the input and try again.")
  618.             Exit Function
  619.         End If
  620.  
  621.         If InStr(strVar, "/") Then
  622.             Call Wscript.Echo( "Invalid " & StrVarName)
  623.             Call Wscript.Echo( "Please check the input and try again.")
  624.             Exit Function
  625.         End If
  626.         blnGetArg = True 'success
  627.     End If
  628. End Function
  629.  
  630. '********************************************************************
  631. '*
  632. '* Function blnConnect()
  633. '*
  634. '* Purpose: Connects to machine strServer.
  635. '*
  636. '* Input:   strServer       a machine name
  637. '*          strNameSpace    a namespace
  638. '*          strUserName     name of the current user
  639. '*          strPassword     password of the current user
  640. '*
  641. '* Output:  objService is returned  as a service object.
  642. '*          strServer is set to local host if left unspecified
  643. '*
  644. '********************************************************************
  645. Private Function blnConnect(ByVal strNameSpace, _
  646.                             ByVal strUserName,  _
  647.                             ByVal strPassword,  _
  648.                             ByRef strServer,    _
  649.                             ByRef objService)
  650.  
  651.     ON ERROR RESUME NEXT
  652.  
  653.     Dim objLocator, objWshNet
  654.  
  655.     blnConnect = False     'There is no error.
  656.  
  657.     'Create Locator object to connect to remote CIM object manager
  658.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  659.     If Err.Number then
  660.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  661.                            " occurred in creating a locator object." )
  662.         If Err.Description <> "" Then
  663.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  664.         End If
  665.         Err.Clear
  666.         blnConnect = True     'An error occurred
  667.         Exit Function
  668.     End If
  669.  
  670.     'Connect to the namespace which is either local or remote
  671.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  672.        strUserName, strPassword)
  673.     ObjService.Security_.impersonationlevel = 3
  674.     If Err.Number then
  675.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  676.                            " occurred in connecting to server " _
  677.            & strServer & ".")
  678.         If Err.Description <> "" Then
  679.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  680.         End If
  681.         Err.Clear
  682.         blnConnect = True     'An error occurred
  683.     End If
  684.  
  685.     'Get the current server's name if left unspecified
  686.     If IsEmpty(strServer) Then
  687.         Set objWshNet = CreateObject("Wscript.Network")
  688.     strServer     = objWshNet.ComputerName
  689.     End If
  690.  
  691. End Function
  692.  
  693. '********************************************************************
  694. '*
  695. '* Sub      VerifyHostIsCscript()
  696. '*
  697. '* Purpose: Determines which program is used to run this script.
  698. '*
  699. '* Input:   None
  700. '*
  701. '* Output:  If host is not cscript, then an error message is printed 
  702. '*          and the script is aborted.
  703. '*
  704. '********************************************************************
  705. Sub VerifyHostIsCscript()
  706.  
  707.     ON ERROR RESUME NEXT
  708.  
  709.     Dim strFullName, strCommand, i, j, intStatus
  710.  
  711.     strFullName = WScript.FullName
  712.  
  713.     If Err.Number then
  714.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  715.         If Err.Description <> "" Then
  716.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  717.         End If
  718.         intStatus =  CONST_ERROR
  719.     End If
  720.  
  721.     i = InStr(1, strFullName, ".exe", 1)
  722.     If i = 0 Then
  723.         intStatus =  CONST_ERROR
  724.     Else
  725.         j = InStrRev(strFullName, "\", i, 1)
  726.         If j = 0 Then
  727.             intStatus =  CONST_ERROR
  728.         Else
  729.             strCommand = Mid(strFullName, j+1, i-j-1)
  730.             Select Case LCase(strCommand)
  731.                 Case "cscript"
  732.                     intStatus = CONST_CSCRIPT
  733.                 Case "wscript"
  734.                     intStatus = CONST_WSCRIPT
  735.                 Case Else       'should never happen
  736.                     Call Wscript.Echo( "An unexpected program was used to " _
  737.                                        & "run this script." )
  738.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  739.                                        & "be used to run this script." )
  740.                     intStatus = CONST_ERROR
  741.                 End Select
  742.         End If
  743.     End If
  744.  
  745.     If intStatus <> CONST_CSCRIPT Then
  746.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  747.              "This can be achieved by" & vbCRLF & _
  748.              "1. Using ""CScript OSReconfig.vbs arguments"" for Windows 95/98 or" _
  749.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  750.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  751.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  752.              "    ""OSReconfig.vbs arguments"" for Windows NT/2000." )
  753.         WScript.Quit
  754.     End If
  755.  
  756. End Sub
  757.  
  758. '********************************************************************
  759. '*
  760. '* Sub WriteLine()
  761. '* Purpose: Writes a text line either to a file or on screen.
  762. '* Input:   strMessage  the string to print
  763. '*          objFile     an output file object
  764. '* Output:  strMessage is either displayed on screen or written to a file.
  765. '*
  766. '********************************************************************
  767. Sub WriteLine(ByVal strMessage, ByVal objFile)
  768.  
  769.     On Error Resume Next
  770.     If IsObject(objFile) then        'objFile should be a file object
  771.         objFile.WriteLine strMessage
  772.     Else
  773.         Call Wscript.Echo( strMessage )
  774.     End If
  775.  
  776. End Sub
  777.  
  778. '********************************************************************
  779. '* 
  780. '* Function blnErrorOccurred()
  781. '*
  782. '* Purpose: Reports error with a string saying what the error occurred in.
  783. '*
  784. '* Input:   strIn        string saying what the error occurred in.
  785. '*
  786. '* Output:  displayed on screen 
  787. '* 
  788. '********************************************************************
  789. Private Function blnErrorOccurred (ByVal strIn)
  790.  
  791.     If Err.Number Then
  792.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  793.         If Err.Description <> "" Then
  794.             Call Wscript.Echo( "Error description: " & Err.Description)
  795.         End If
  796.         Err.Clear
  797.         blnErrorOccurred = True
  798.     Else
  799.         blnErrorOccurred = False
  800.     End If
  801.  
  802. End Function
  803.  
  804. '********************************************************************
  805. '* 
  806. '* Function blnOpenFile
  807. '*
  808. '* Purpose: Opens a file.
  809. '*
  810. '* Input:   strFileName        A string with the name of the file.
  811. '*
  812. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  813. '*            Nothing upon Failure.
  814. '* 
  815. '********************************************************************
  816. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  817.  
  818.     ON ERROR RESUME NEXT
  819.  
  820.     Dim objFileSystem
  821.  
  822.     Set objFileSystem = Nothing
  823.  
  824.     If IsEmpty(strFileName) OR strFileName = "" Then
  825.         blnOpenFile = False
  826.         Set objOpenFile = Nothing
  827.         Exit Function
  828.     End If
  829.  
  830.     'Create a file object
  831.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  832.     If blnErrorOccurred("Could not create filesystem object.") Then
  833.         blnOpenFile = False
  834.         Set objOpenFile = Nothing
  835.         Exit Function
  836.     End If
  837.  
  838.     'Open the file for output
  839.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  840.     If blnErrorOccurred("Could not open") Then
  841.         blnOpenFile = False
  842.         Set objOpenFile = Nothing
  843.         Exit Function
  844.     End If
  845.     blnOpenFile = True
  846.  
  847. End Function
  848.  
  849. '********************************************************************
  850. '*                                                                  *
  851. '*                           End of File                            *
  852. '*                                                                  *
  853. '********************************************************************