如何在 WDS 部署的 specialize 阶段运行 PowerShell 脚本?

sno*_*gle 6 powershell wds windows-pe windows-server-2012

我正在使用安装介质上的默认 boot.wim 文件为Windows Server 2012无人值守部署设置Windows 部署服务(WDS) 。我有一个 PowerShell 脚本,可以为我们的站点执行自动自定义。我希望这个脚本在 specialize 阶段运行,所以我不必搞乱自动登录,并且能够在配置期间保存自己重新启动。脚本似乎没有运行,日志只给出了无用的错误代码。

这是我的无人值守文件的相关部分:

    <settings pass="specialize">
        <component name="Microsoft-Windows-Deployment" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <RunSynchronous>
                <RunSynchronousCommand wcm:action="add">
                    <Order>1</Order>
                    <Credentials>
                        <Domain>WDSSERVER</Domain>
                        <Password>APASSWORD</Password>
                        <Username>AUSERNAME</Username>
                    </Credentials>
                    <Path>"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -executionpolicy unrestricted -command "\\<REMOTESERVER>\reminst\customize\specialize.ps1"</Path>
                </RunSynchronousCommand>
            </RunSynchronous>
        </component>
    </settings>
Run Code Online (Sandbox Code Playgroud)

响应来自 kce 的请求。这是脚本本身:

write-host "Executing customisation script."
write-host "enabling powershell script execution"
Set-ExecutionPolicy Unrestricted

write-host "Bringing non-system disks online..."
Get-Disk | Where-Object IsOffline –Eq $True | Set-Disk –IsOffline $False
Set-Disk -Number 1 -IsReadOnly $False
Set-Disk -Number 2 -IsReadOnly $False

write-host "Setting up NTP..."
W32tm /register
start-service w32time
w32tm /config /manualpeerlist:uk.pool.ntp.org
restart-service w32time
Set-Service W32Time -StartupType Automatic
sc triggerinfo w32time start/networkon stop/networkoff
sc config W32Time start=auto

write-host "Determining system RAM and setting pagefile..."
$RAM = Get-WmiObject -Class Win32_OperatingSystem | Select TotalVisibleMemorySize
$RAM = ($RAM.TotalVisibleMemorySize / 1kb).tostring("F00")
write-host "disable automanage"
wmic computersystem set AutomaticManagedPagefile=False
Write-Host "removing old pagefile"
wmic pagefileset delete
write-host "creating new pagefile on E:\"
wmic pagefileset create name=“e:\pagefile.sys”
write-host "set size"
$PageFile = Get-WmiObject -Class Win32_PageFileSetting
$PageFile.InitialSize = $RAM
$PageFile.MaximumSize = $RAM
[void]$PageFile.Put()

write-host "Disabling Windows Firewall..."
netsh advfirewall set allprofiles state off

write-host "Enabling powershell remoting..."
Enable-PSRemoting -Force

write-host "Sorting out remote management trusted hosts..."
winrm s winrm/config/client '@{TrustedHosts="*"}'

write-host "Disabling Windows error reporting..."
Disable-WindowsErrorReporting

write-host "Installing VMware Tools..."
c:\vmware-tools.exe /S /v"/qn"
Run Code Online (Sandbox Code Playgroud)

MDM*_*313 2

根据我所读到的内容,未捕获的抛出导致退出代码为一。另外,当脚本路径-command应该通过开关传递时,您正在通过-file开关传递脚本路径;参见参考资料-command会将您的字符串视为命令,并且由于它是文件路径,因此它将抛出我们在 PowerShell 窗口中喜欢的相同红字异常之一,瞧!由于未捕获异常,因此退出代码 1。当然,所有这些都是猜测,除非

"powershell.exe" -executionpolicy bypass -noprofile -file "\\<REMOTESERVER>\reminst\customize\specialize.ps1"
Run Code Online (Sandbox Code Playgroud)

实际上可以工作,假设它运行的帐户具有文件共享的权限。为了避免这些权限问题,您只需将应答文件中的代码粘贴到 {} 之间,然后您就可以使用该-command选项,

"powershell.exe" -executionpolicy bypass -noprofile -command {...}
Run Code Online (Sandbox Code Playgroud)