Partager via


Scenario1.vbs

By The Scripting Guys, Microsoft Corporation

This script initiates an automated installation and configuration process for Windows XP Service Pack 2 on multiple network hosts. Scenario1.vbs gets a list of remote machines from a text file, copies two scripts, install.vbs and runonce.vbs, to each machine, and then launches the first script on each machine.

The script runs on the administrative workstation, and all scripts must be located in the same folder. The credentials under which the scripts run must have administrative privileges on each host.

In the basic scenario, the SP2 setup file is on a remote server accessible to all network hosts on the list. To run a variation of the scenario in which the SP2 setup is copied to local hosts and run from there, rename install.vbs to another name (such as install-remote.vbs) and then rename install-local.vbs to install.vbs. You must also make minor changes in scenario1.vbs and install.vbs documented in those scripts.

The other scripts perform the following functions:

Install.vbs

  • Runs SP2 setup from a remote server.

  • Sets AutoAdmin and RunOnce registry entries on host.

  • Logs results to a text file, computername-sp2-instlog.txt, and copies the file back to administrative workstation.

  • Forces a reboot, after which runonce.vbs is automatically launched.

Runonce.vbs

  • Runs after machine reboots for first time, launched by RunOnce reg entry.

  • Configures Windows Firewall to allow certain programs and open certain ports. You must edit these settings to reflect the configuration of your network.

  • Enables remote administration on Windows Firewall so that remote scripts and administrative tools can again run against this host.

  • Resets AutoAdmin and RunOnce registry entries.

  • Logs results to text file, computername-sp2-clnuplog.txt and copies the file back to admin workstation.

  • Again forces a reboot.

Further explanation of Scenario 1 and the role of each script is contained in the introduction to these scripts at https://www.microsoft.com/technet/scriptcenter/solutions/appcompat/default.mspx

Scenario1.vbs corresponds and adds functionality to StartRemoteIns.vbs, one of the scripts that ships with Application Compatibility Testing and Mitigation Guide for Windows XP Service Pack 2 and which are documented in the Appendix. You can download a Windows Installer (.msi) file that installs the Guide and its associated scripts from:

https://www.microsoft.com/downloads/details.aspx?FamilyId=9300BECF-2DEE-4772-ADD9-AD0EAF89C4A7&displaylang=en

Input for this script comes from a text file, computers.txt, which contains the names of computers against which the script will run.

Each line of the input file must consist of the name of a computer. Each computer must be accessible on the network and the credentials under which the script is run must have administrative privileges on it. For example:

client1
client2
client3
client4

Be sure there is no line break after the final line, as this will be interpreted by the script as an empty string.

To use the script, copy the code, paste it into Notepad, and then save the script as scenario1.vbs. Then create a text file with the name of a computer on each line, as described above, and save it as computers.txt in the same directory as the script. To run the script, open a command prompt window to the directory of the script and type:

cscript scenario1.vbs

If the default script host on the computer is Cscript.exe, the initial "cscript" may be omitted.

Script Code

'******************************************************************************
'scenario1.vbs
'Author: Peter Costantini, the Microsoft Scripting Guys
'Date: 9/2/04
'This script runs on the admin workstation. It gets a list of remote machines
'from a text file, copies the scripts to each machine, and
'then launches the first script on each machine.
'All scripts must be in the same folder.
'******************************************************************************

On Error Resume Next

'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computers.txt"

'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(g_strHostFile) Then
  Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
Else
  WScript.Echo "Input file " & g_strHostFile & " not found."
  WScript.Quit
End If

'Loop through list of computers and perform tasks on each.
Do Until objTextStream.AtEndOfStream
  g_strComputer = objTextStream.ReadLine
  Wscript.Echo VbCrLf & g_strComputer
  Wscript.Echo String(Len(g_strComputer), "-")
'Ping host to ensure that it is accessible.
  blnPing = PingHost
  If blnPing = True Then
    Wscript.Echo g_strComputer & " responded to ping."
'Call functions and sub-routines and handle logic.
    blnCopyFiles = CopyFiles
    If blnCopyFiles = True Then
      Wscript.Echo "Files copied. Running configuration and SP 2 " & _
       "installation."
      RunRemote
    Else
      Wscript.Echo "ERROR: Unable to copy files. Operation failed."
    End If
  Else
    WScript.Echo "ERROR: " & g_strComputer & " did not respond to ping."
  End If
Loop
objTextStream.Close

'******************************************************************************

Function PingHost

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & g_strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
  PingHost = True
Else
  PingHost = False
End If

End Function

'******************************************************************************

Function CopyFiles

'Copies install.vbs and runonce.vbs from admin workstation to host.
'These scripts should be in same directory as this script.
'If not, edit the filenames in the array to include full path.

strRemoteFolder = "\\" & g_strComputer & "\c$\temp-ac"
arrFiles = Array("install.vbs", "runonce.vbs")
'If running Service Pack 2 setup from file to be copied to local computer,
'comment out the previous line and uncomment the following two lines:
'arrFiles = Array("install.vbs", "runonce.vbs", _
' "WindowsXP-KB835935-SP2-ENU.exe")
'In this case, you must also rename install-local.vbs to install.vbs.


'Check for folder on host and 
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strRemoteFolder) Then
  objFSO.CreateFolder(strRemoteFolder)
  If Err = 0 Then
    WScript.Echo "Created folder " & strRemoteFolder & "."
  Else
    WScript.Echo "Unable to create folder " & strRemoteFolder & "."
    WScript.Echo "Error Number: " & Err.Number
    WScript.Echo "Error Source: " & Err.Source
    WScript.Echo "Error Description: " & Err.Description
    Err.Clear
    CopyFile = False
    Exit Function
  End If
End If

'Copy files.
intCount = 0
For Each strFile in arrFiles
  If objFSO.FileExists(strFile) Then
    objFSO.CopyFile strFile, strRemoteFolder & "\"
    If Err = 0 Then
      WScript.Echo "Copied file " & strFile & " to folder " & _
       strRemoteFolder & "."
      intCount = intCount + 1
    Else
      WScript.Echo "Unable to copy file " & strFile & "."
    End If
    Err.Clear
  Else
    WScript.Echo "File " & strFile & " not found."
  End If
Next

If (intCount = UBound(arrFiles) + 1) Then
  CopyFiles = True
Else
  CopyFiles = False
End If

End Function

'******************************************************************************

Sub RunRemote

'Runs Windows XP SP2 setup and configuration script on remote machine.
'Use cscript to insure that the script runs under cmd.exe
strScript = "cscript c:\temp-ac\install.vbs"

'Connect to WMI service on remote machine.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
 & g_strComputer & "\root\cimv2")
If Err <> 0 Then
  WScript.Echo "Error connecting to WMI on \\" & g_strComputer & "."
  WScript.Echo "Error number: " & Err.Number
  WScript.Echo "Error source: " & Err.Source
  WScript.Echo "Error description: " & Err.Description
  Error.Clear
  Exit Sub
End If

'Get a Win32_Process object.
Set objProcess = objWMIService.Get("Win32_Process")

'Create a new process and run strScript in it.
intReturn = objProcess.Create(strScript, , , intProcessID)
If intReturn = 0 Then
  WScript.Echo "Launched " & strScript & " to install Windows XP " & _
   "Service Pack 2 and prepare the computer for configuration." & VbCrLf & _
   "Process ID: " & intProcessID & "."
Else
  WScript.Echo "Error: " & strScript & " could not be started." & VbCrLf & _
   "Return code: " & intReturn & "."
End If

End Sub

For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.

Disclaimer

This sample script is not supported under any Microsoft standard support program or service. The sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.