Install-local.vbs

作者: The Scripting Guys,Microsoft Corporation

這個指令碼由 scenario1.vbs 在網路主機上啟動。Install-local.vbs 在要安裝 SP2 主機的本機執行,並執行下列工作:

  • 在本機電腦上執行 SP2 安裝。

  • 設定 AutoAdmin 及 RunOnce 登錄項目。

  • 將結果記錄到文字檔 computername-sp2-instlog.txt。

  • 強制執行重新開機,這樣就會自動啟動 runonce.vbs。

在基本的案例中,SP 2 安裝檔案是位於可供清單上所有網路主機存取的遠端伺服器。若要針對 SP 2 安裝複製到本機主機上,並從那裡執行的案例做變化的話,請將 install.vbs 重新命名為其他名稱 (例如:install-remote.vbs),然後將這個指令碼重新命名為 install.vbs。同時也必須在 scenario1.vbs 和那些指令碼中所記錄的新 install.vbs 中稍做變更。

案例 1 和 2 以及每個指令碼的角色的進一步說明,都包含在位於以下網址的指令碼簡介中:

http://www.microsoft.com/taiwan/technet/scriptcenter/solutions/appcompat/default.mspx

Install-local.vbs 對應於 install.cmd 並增加其功能;install.cmd 是《Application Compatibility Testing and Mitigation Guide for Window sXP Service Pack 2》(Window sXP Service Pack 2 應用程式相容性測試及緩和指南) 隨附的指令碼之一,並記錄在<附錄>中。您可以下載用來安裝該指南及其相關指令碼的 Windows Installer (.msi) 檔案,網址是:

http://www.microsoft.com/downloads/details.aspx?FamilyId=9300BECF-2DEE-4772-ADD9-AD0EAF89C4A7&displaylang=en (英文)

若要使用指令碼,請複製程式碼並將它貼入「記事本」,再將指令碼儲存為 install-local.vbs。這個指令碼是設計來將它當作 scenario1.vbs 或 scenario2.vbs 所起始程序的一部份來自動執行。

指令碼


'******************************************************************************
'install-local.vbs
'Author: Peter Costantini, the Microsoft Scripting Guys
'Date: 9/1/04
'Must be deployed to a client and launched remotely by scenario1.vbs, or
'present on the local computer for scenario2.vbs.
'Assumes that Windows XP Service Pack 2 setup program and runonce.vbs
'are in same directory as script.
'1. Runs Service Pack 2 setup program 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.
Const FOR_APPENDING = 8
g_strLocalFolder = "c:\temp-ac"
'Change name of computer to actual administrative workstation or local folder
'to which log should be copied.
g_strRemoteFolder = "\\<adminwkstn>\c$\scripts-ac"
'If running this script with scenario2.vbs, change to a local folder, e.g.:
'g_strRemoteFolder = "c:\temp-ac\logs"

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

'Create log file.
Set g_objFSO = CreateObject("Scripting.FileSystemObject")
Set g_objTextStream = g_objFSO.OpenTextFile(g_strLogFile, FOR_APPENDING, True)
g_objTextStream.WriteLine "Windows XP Service Pack 2 " & _
 "Installation and Configuration Log: Phase 1"
g_objTextStream.WriteLine Now
g_objTextStream.WriteLine g_strComputer
g_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
  CopyLog
  WScript.Quit
End If
blnAutoAdmin = ConfigAutoAdmin
If blnAutoAdmin = False Then
  CopyLog
  WScript.Quit
End If
Reboot

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

Function GetComputerName

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

End Function

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

Function InstallSP

'Edit this line to reflect the correct name of the Windows XP Service Pack 2 
'setup program.
strInstallCmd = "WindowsXP-KB835935-SP2-ENU.exe"
strInstallArgs = " /quiet /norestart /o"
strInstallPath = g_strLocalFolder & "\" & strInstallCmd & strInstallArgs

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
 & g_strComputer & "\root\cimv2")
If Err <> 0 Then
  g_objTextStream.WriteLine "Unable to install Service Pack 2." & _
   "Error connecting to WMI." & VbCrLf & _
   "Error number: " & Err.Number & VbCrLf & _
   "Error source: " & Err.Source & VbCrLf & _
   "Error description: " & Err.Description
  InstallSP = False
  Err.Clear
  Exit Function
End If
Set objProcess = objWMIService.Get("Win32_Process")
intReturn = objProcess.Create(strInstallPath, , , intProcessID)
'This could take one or two hours.
If intReturn = 0 Then
  g_objTextStream.WriteLine "Service pack installation process begun ..."
'Pause for 10 seconds (WITHIN 10) before checking.
'To reduce network traffic, make interval longer.
  Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
   ("SELECT * FROM __InstanceDeletionEvent WITHIN 10 WHERE " _
   & "Targetinstance ISA 'Win32_Process' AND " _
   & "TargetInstance.ProcessId='" & intProcessID & "'")
  Set objLatestEvent = colMonitoredEvents.NextEvent
  g_objTextStream.WriteLine "Service Pack 2 installation completed."
  InstallSP = True
Else
  g_objTextStream.WriteLine "Error: Service pack not installed. " & VbCrLf & _
   "Return code: " & intReturn & "."
  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
  g_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
  g_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
'  g_objTextStream.WriteLine "Error: DefaultDomainName not configured."
'End If

'Turn on AutoAdminLogon
intRet4 = objReg.SetStringValue(HKEY_LOCAL_MACHINE, strKeyPath1, _
 "AutoAdminLogon", intAutoAdminLogon)
If intRet4 <> 0 Then
  g_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
  g_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
  g_objTextStream.WriteLine "AutoAdminLogon and RunOnce configured."
  ConfigAutoAdmin = True
Else
  g_objTextStream.WriteLine "Error: AutoAdminLogon and RunOnce not fully " & _
   "configured."
  ConfigAutoAdmin = False
End If

End Function

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

Sub Reboot

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

End Sub

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

Sub CopyLog

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

'If remote folder does not exist, create it.
If Not g_objFSO.FolderExists(g_strRemoteFolder) Then
  g_objFSO.CreateFolder(g_strRemoteFolder)
  If Err <> 0 Then
    Err.Clear
    Exit Sub
  End If
End If
'Copy log.
g_objFSO.CopyFile g_strLogFile, g_strRemoteFolder & "\"

End Sub


如需線上對等支援,請加入 msnews.microsoft.com 新聞伺服器上的 microsoft.public.windows.server.scripting (英文) 社群。若您想要對範例指令碼或指令碼指南,提供意見、回報問題,請與 Microsoft TechNet (英文) 連絡。

免責聲明

此範例指令碼不支援任何 Microsoft 標準技術支援方案或服務。上述的範例指令碼係依「現況」提供,不附帶任何擔保。Microsoft 公司不提供任何的默示擔保,包括但不限於任何商業適售性及特定用途之適用性的默示擔保。您必須承擔此範例指令碼或文件所造成的一切風險。在任何情況下,無論是使用或無法使用此範例指令碼或文件所造成的損害 (包括但不限於營業之損失、營業之中斷、營業資訊之滅失及其他金錢損失),Microsoft 公司、作者群或此指令碼之創作、製造或散發有關之人員概不負責,即使 Microsoft 已經被告知損害發生之可能性亦同。

顯示: