Share via


By The Scripting Guys, Microsoft Corporation

This script is launched on a network host by scenario1.vbs. Install.vbs runs locally on the host on which SP2 is to be installed, performing the following tasks:

  • 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.

In the basic scenario, the SP 2 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 SP 2 setup is copied to local hosts and run from there, rename this script 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 the new install.vbs documented in those scripts.

Further explanation of Scenario 1 and the role of each script is contained in the introduction to these scripts at:

Install.vbs corresponds and adds functionality to install.cmd, 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:

To use the script, copy the code, paste it into Notepad, and then save the script as install.vbs. The script is designed to be run automatically as part of the process initiated by scenario1.vbs.

Script Code

'Author: Peter Costantini, the Microsoft Scripting Guys
'Date: 9/1/04
'Must be deployed to a client and launched remotely by scenario1.vbs.
'Assumes that runonce.vbs is in same directory as script.
'Assumes that Windows XP Service Pack 2 setup program is on a remote server
'and runonce.vbs are in same directory as script.
'1. Runs Service Pack 2 setup program from remote server to install
'   Windows XP Service Pack 2. This could take one or two hours.
'2. Configures the AutoAdmin and RunOnce registry settings necessary
'   to run runonce.vbs.
'3. Logs results to text file, <computername>-sp2-instlog.txt and copies
'   the file back to admin workstation.
'4. Forces a reboot of the local machine so that the AutoAdmin and RunOnce 
'   registry settings take effect.

On Error Resume Next

'Initialize global constants and variables.
g_strLocalFolder = "c:\temp-ac"
'Change name of computer to actual administrative workstation or local 
'path to which log should be copied.
g_strRemoteFolder = "\\<adminwkstn>\c$\temp-ac"

'Get computer name.
g_strComputer = GetComputerName
g_strLogFile = g_strComputer & "-sp2-instlog.txt"

'Create log file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(g_strLogFile, FOR_APPENDING, True)
objTextStream.WriteLine "Windows XP Service Pack 2 " & _
 "Installation and Configuration Log: Phase 1"
objTextStream.WriteLine Now
objTextStream.WriteLine g_strComputer
objTextStream.WriteLine String(Len(g_strComputer), "-")

'Handle logic of calling functions and sub-routines to install Service Pack 2
'and configure AutoAdministration.
blnInstallSP = InstallSP
If blnInstallSP = False Then
End If
blnAutoAdmin = ConfigAutoAdmin
If blnAutoAdmin = False Then
End If


Function GetComputerName

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\." _
Set colSystems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objSytem In colSystems
  GetComputerName = objSytem.Name

End Function


Function InstallSP

'Edit this line to include the server and share name where the Windows XP
'Service Pack 2 setup program is located.
strInstallPath = "\\servername\xpsp2\WindowsXP-KB835935-SP2-ENU.exe " & _
 "/quiet /norestart /o"

Set WshShell = CreateObject("Wscript.Shell")
Set objExec = WshShell.Exec(strInstallPath) 
'This could take one or two hours.
objTextStream.WriteLine "Installation started ..."
If Err = 0 Then
'Loop until Exec is finished - Status = 1.
  Do While objExec.Status = 0
'Pause for 10 seconds before checking.
'To reduce network traffic, make interval longer.
    WScript.Sleep 10000
  objTextStream.WriteLine "Service Pack 2 installation completed."
  InstallSP = True
  objTextStream.WriteLine "Unable to install Service Pack 2." & VbCrLf & _
   "Error connecting to Service Pack 2 on server." & VbCrLf & _
   "Error number: " & Err.Number & VbCrLf & _
   "Error source: " & Err.Source & VbCrLf & _
   "Error description: " & Err.Description
  InstallSP = False
End If

End Function


Function ConfigAutoAdmin

Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath1 = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
strKeyPath2 = "SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
strDefaultUserName = "Administrator"
strDefaultPassword = "P@ssw0rd"
strDefaultDomainName = "Contoso"
intAutoAdminLogon = 1
strRunOnceEntry = "MyScript"
strRunoncePath = g_strLocalFolder & "\runonce.vbs"

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 g_strComputer & "\root\default:StdRegProv")

'Set strDefaultUserName to user with Administrator credentials.
intRet1 = objReg.SetStringValue(HKEY_LOCAL_MACHINE, strKeyPath1, _
 "DefaultUserName", strDefaultUserName)
If intRet1 <> 0 Then
  objTextStream.WriteLine "Error: DefaultUserName not configured."
End If

'Set strDefaultPassword to password of default username.
intRet2 = objReg.SetStringValue(HKEY_LOCAL_MACHINE, strKeyPath1, _
 "DefaultPassword", strDefaultPassword)
If intRet2 <> 0 Then
  objTextStream.WriteLine "Error: DefaultPassword not configured."
End If

'Uncomment next 5 lines and edit last parameter if default domain
'for the credentials is different from that already set.
'intRet3 = objReg.SetStringValue(HKEY_LOCAL_MACHINE, strKeyPath1, _
' "DefaultDomainName", strDefaultDomainName)
'If intRet3 <> 0 Then
'  objTextStream.WriteLine "Error: DefaultDomainName not configured."
'End If

'Turn on AutoAdminLogon
intRet4 = objReg.SetStringValue(HKEY_LOCAL_MACHINE, strKeyPath1, _
 "AutoAdminLogon", "1")
If intRet4 <> 0 Then
  objTextStream.WriteLine "Error: AutoAdminLogon not configured."
End If

'Add MyScript entry to RunOnce subkey.
intRet5 = objReg.SetStringValue(HKEY_LOCAL_MACHINE, strKeyPath2, _
 strRunOnceEntry, strRunoncePath)
If intRet5 <> 0 Then
  objTextStream.WriteLine "Error: MyScript RunOnce entry not configured."
End If

'Check that all registry write operations succeeded.
If (intRet1 + intRet2 + intRet3 + intRet4 + intRet5) = 0 Then
  objTextStream.WriteLine "AutoAdminLogon and RunOnce configured."
  ConfigAutoAdmin = True
  objTextStream.WriteLine "Error: AutoAdminLogon and RunOnce not fully " & _
  ConfigAutoAdmin = False
End If

End Function


Sub Reboot

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate," & _
 "(Shutdown)}!\\" & g_strComputer & "\root\cimv2")
Set colOSes = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
objTextStream.WriteLine "Attempting to reboot ..."
For Each objOS In colOSes 'Only one objOS in collection
  intReturn = objOS.Win32Shutdown(FORCED_REBOOT)
  If intReturn <> 0 Then
    Set objTextStream = objFSO.OpenTextFile(g_strLogFile, FOR_APPENDING, True)
    objTextStream.WriteLine Now
    objTextStream.WriteLine "Error: Unable to reboot. " & VbCrLf & _
     "Return code: " & intReturn
  End If

End Sub


Sub CopyLog

'Close text file.
objTextStream.WriteLine "Closing log and attempting to copy file to " & _
 "administrative workstation."
objTextStream.WriteLine String(80, "-")

'Copy log.
If Not objFSO.FolderExists(g_strRemoteFolder) Then
  If Err <> 0 Then
    Exit Sub
  End If
End If
objFSO.CopyFile g_strLogFile, g_strRemoteFolder & "\"

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.


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.