Install.vbs
发布者MicrosoftCorporation脚本专家
此脚本由scenario1.vbs在一台网络主机上启动。Install.vbs可以在安装了SP2的主机上以本地方式运行,它执行以下任务:
?从一台远程服务器运行SP2安装程序。
?在主机上设置AutoAdmin和RunOnce两个注册表项。
?将结果记录到文本文件computername-sp2-instlog.txt并将该文件复制回管理工作站。
?强制重新启动,随后runonce.vbs将自动启动。
在基本方案中,SP2安装程序文件位于列表中的所有网络主机均可访问的一台远程服务器上。在该方案的某种变化方案中,如果将SP2安装程序复制到本地主机并从这里运行,则应重命名此脚本(例如重命名为install-remote.vbs),然后将install-local.vbs重命名为install.vbs。您还必须对这些脚本中提到的scenario1.vbs和新的install.vbs做一些细微更改。
有关方案1以及各个脚本的作用的进一步说明,请参见对这些脚本的介绍,网址是:
http://www.microsoft.com/technet/scriptcenter/solutions/appcompat.msxp
Install.vbs对应于install.cmd,但增加了一些新功能;install.cmd是ApplicationCompatibilityTestingandMitigationGuideforWindowsXPServicePack2(WindowsXPServicePack2应用程序兼容性测试和缓解指南)“附录”中介绍的附带脚本之一。您可以从以下网址下载用来安装该指南及其关联脚本的WindowsInstaller(.msi)文件:
http://www.microsoft.com/downloads/details.aspx?FamilyId=9300BECF-2DEE-4772-ADD9-AD0EAF89C4A7&displaylang=en
要使用此脚本,请复制代码,将代码粘贴到记事本中,然后将脚本另存为install.vbs。此脚本被设计成了作为scenario1.vbs启动的进程的一部分自动运行。
脚本代码
复制代码 代码如下:
'******************************************************************************
'install.vbs
'Author:PeterCostantini,theMicrosoftScriptingGuys
'Date:9/1/04
'Mustbedeployedtoaclientandlaunchedremotelybyscenario1.vbs.
'Assumesthatrunonce.vbsisinsamedirectoryasscript.
'AssumesthatWindowsXPServicePack2setupprogramisonaremoteserver
'andrunonce.vbsareinsamedirectoryasscript.
'1.RunsServicePack2setupprogramfromremoteservertoinstall
'WindowsXPServicePack2.Thiscouldtakeoneortwohours.
'2.ConfigurestheAutoAdminandRunOnceregistrysettingsnecessary
'torunrunonce.vbs.
'3.Logsresultstotextfile,<computername>-sp2-instlog.txtandcopies
'thefilebacktoadminworkstation.
'4.ForcesarebootofthelocalmachinesothattheAutoAdminandRunOnce
'registrysettingstakeeffect.
'******************************************************************************
OnErrorResumeNext
'Initializeglobalconstantsandvariables.
ConstFOR_APPENDING=8
g_strLocalFolder="c:temp-ac"
'Changenameofcomputertoactualadministrativeworkstationorlocal
'pathtowhichlogshouldbecopied.
g_strRemoteFolder="<adminwkstn>c$temp-ac"
'Getcomputername.
g_strComputer=GetComputerName
g_strLogFile=g_strComputer&"-sp2-instlog.txt"
'Createlogfile.
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjTextStream=objFSO.OpenTextFile(g_strLogFile,FOR_APPENDING,True)
objTextStream.WriteLine"WindowsXPServicePack2"&_
"InstallationandConfigurationLog:Phase1"
objTextStream.WriteLineNow
objTextStream.WriteLineg_strComputer
objTextStream.WriteLineString(Len(g_strComputer),"-")
'Handlelogicofcallingfunctionsandsub-routinestoinstallServicePack2
'andconfigureAutoAdministration.
blnInstallSP=InstallSP
IfblnInstallSP=FalseThen
CopyLog
WScript.Quit
EndIf
blnAutoAdmin=ConfigAutoAdmin
IfblnAutoAdmin=FalseThen
CopyLog
WScript.Quit
EndIf
Reboot
'******************************************************************************
FunctionGetComputerName
SetobjWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!."_
&"rootcimv2")
SetcolSystems=objWMIService.ExecQuery("SELECT*FROMWin32_ComputerSystem")
ForEachobjSytemIncolSystems
GetComputerName=objSytem.Name
Next
EndFunction
'******************************************************************************
FunctionInstallSP
'EditthislinetoincludetheserverandsharenamewheretheWindowsXP
'ServicePack2setupprogramislocated.
strInstallPath="servernamexpsp2WindowsXP-KB835935-SP2-ENU.exe"&_
"/quiet/norestart/o"
SetWshShell=CreateObject("Wscript.Shell")
SetobjExec=WshShell.Exec(strInstallPath)
'Thiscouldtakeoneortwohours.
objTextStream.WriteLine"Installationstarted..."
IfErr=0Then
'LoopuntilExecisfinished-Status=1.
DoWhileobjExec.Status=0
'Pausefor10secondsbeforechecking.
'Toreducenetworktraffic,makeintervallonger.
WScript.Sleep10000
Loop
objTextStream.WriteLine"ServicePack2installationcompleted."
InstallSP=True
Else
objTextStream.WriteLine"UnabletoinstallServicePack2."&VbCrLf&_
"ErrorconnectingtoServicePack2onserver."&VbCrLf&_
"Errornumber:"&Err.Number&VbCrLf&_
"Errorsource:"&Err.Source&VbCrLf&_
"Errordescription:"&Err.Description
InstallSP=False
EndIf
Err.Clear
EndFunction
'******************************************************************************
FunctionConfigAutoAdmin
ConstHKEY_LOCAL_MACHINE=&H80000002
strKeyPath1="SOFTWAREMicrosoftWindowsNTCurrentVersionWinlogon"
strKeyPath2="SOFTWAREMicrosoftWindowsCurrentVersionRunOnce"
strDefaultUserName="Administrator"
strDefaultPassword="P@ssw0rd"
strDefaultDomainName="Contoso"
intAutoAdminLogon=1
strRunOnceEntry="MyScript"
strRunoncePath=g_strLocalFolder&"runonce.vbs"
SetobjReg=GetObject("winmgmts:{impersonationLevel=impersonate}!"&_
g_strComputer&"rootdefault:StdRegProv")
'SetstrDefaultUserNametouserwithAdministratorcredentials.
intRet1=objReg.SetStringValue(HKEY_LOCAL_MACHINE,strKeyPath1,_
"DefaultUserName",strDefaultUserName)
IfintRet1<>0Then
objTextStream.WriteLine"Error:DefaultUserNamenotconfigured."
EndIf
'SetstrDefaultPasswordtopasswordofdefaultusername.
intRet2=objReg.SetStringValue(HKEY_LOCAL_MACHINE,strKeyPath1,_
"DefaultPassword",strDefaultPassword)
IfintRet2<>0Then
objTextStream.WriteLine"Error:DefaultPasswordnotconfigured."
EndIf
'Uncommentnext5linesandeditlastparameterifdefaultdomain
'forthecredentialsisdifferentfromthatalreadyset.
'intRet3=objReg.SetStringValue(HKEY_LOCAL_MACHINE,strKeyPath1,_
'"DefaultDomainName",strDefaultDomainName)
'IfintRet3<>0Then
'objTextStream.WriteLine"Error:DefaultDomainNamenotconfigured."
'EndIf
'TurnonAutoAdminLogon
intRet4=objReg.SetStringValue(HKEY_LOCAL_MACHINE,strKeyPath1,_
"AutoAdminLogon","1")
IfintRet4<>0Then
objTextStream.WriteLine"Error:AutoAdminLogonnotconfigured."
EndIf
'AddMyScriptentrytoRunOncesubkey.
intRet5=objReg.SetStringValue(HKEY_LOCAL_MACHINE,strKeyPath2,_
strRunOnceEntry,strRunoncePath)
IfintRet5<>0Then
objTextStream.WriteLine"Error:MyScriptRunOnceentrynotconfigured."
EndIf
'Checkthatallregistrywriteoperationssucceeded.
If(intRet1+intRet2+intRet3+intRet4+intRet5)=0Then
objTextStream.WriteLine"AutoAdminLogonandRunOnceconfigured."
ConfigAutoAdmin=True
Else
objTextStream.WriteLine"Error:AutoAdminLogonandRunOncenotfully"&_
"configured."
ConfigAutoAdmin=False
EndIf
EndFunction
'******************************************************************************
SubReboot
ConstFORCED_REBOOT=6
SetobjWMIService=GetObject("winmgmts:{impersonationLevel=impersonate,"&_
"(Shutdown)}!"&g_strComputer&"rootcimv2")
SetcolOSes=objWMIService.ExecQuery("SELECT*FROMWin32_OperatingSystem")
objTextStream.WriteLine"Attemptingtoreboot..."
CopyLog
ForEachobjOSIncolOSes'OnlyoneobjOSincollection
intReturn=objOS.Win32Shutdown(FORCED_REBOOT)
IfintReturn<>0Then
SetobjTextStream=objFSO.OpenTextFile(g_strLogFile,FOR_APPENDING,True)
objTextStream.WriteLineNow
objTextStream.WriteLine"Error:Unabletoreboot."&VbCrLf&_
"Returncode:"&intReturn
CopyLog
EndIf
Next
EndSub
'******************************************************************************
SubCopyLog
'Closetextfile.
objTextStream.WriteLine"Closinglogandattemptingtocopyfileto"&_
"administrativeworkstation."
objTextStream.WriteLine
objTextStream.WriteLineString(80,"-")
objTextStream.WriteLine
objTextStream.Close
'Copylog.
IfNotobjFSO.FolderExists(g_strRemoteFolder)Then
objFSO.CreateFolder(g_strRemoteFolder)
IfErr<>0Then
Err.Clear
ExitSub
EndIf
EndIf
objFSO.CopyFileg_strLogFile,g_strRemoteFolder&""
EndSub
要获得在线同行支持,请加入msnews.microsoft.com新闻服务器上的microsoft.public.windows.server.scripting社区。要提供反馈或报告示例脚本或“脚本指南”中的错误,请联系MicrosoftTechNet。
免责声明
此示例脚本不受任何Microsoft标准支持计划或服务的支持。这里仅按原样提供示例脚本,而不作任何类型的担保。Microsoft进一步明确拒绝所有的暗示担保,包括但不限于对适销性或对特定目的适用性的任何暗示担保。使用或执行示例脚本和文档所引起的全部风险应由您自己承担。在任何情况下,对于使用或不能使用示例脚本或文档所引起的任何损害(包括但不限于商业利润损失、业务中断、商业信息丢失或其他资金损失所造成的损害),Microsoft、其作者以及参与脚本创建、生产或传递的任何其他人员都概不负责,即使Microsoft已被告知存在这些损害的可能性。