如何在 WDS 部署的专门过程中运行 PowerShell 脚本?

如何在 WDS 部署的专门过程中运行 PowerShell 脚本?

我正在设置Windows 部署服务(WDS)Windows 服务器 2012使用安装介质上的默认 boot.wim 文件进行无人值守部署。我有一个 PowerShell 脚本,可以自动为我们的站点进行自定义。我希望这个脚本在专业化过程中运行,这样我就不必处理自动登录,并且可以在配置期间节省重新启动的时间。该脚本似乎没有运行,日志只给出了一个无用的错误代码。

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

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

响应 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"

答案1

据我所知,未捕获的抛出会导致退出代码为 1。此外,您通过 switch 传递了脚本路径-command,而它应该通过-fileswitch 传递;请参阅参考资料.-command会将您的字符串视为命令,并且由于它是文件路径,它会抛出我们在 PowerShell 窗口中喜欢的那些相同的红色字母异常之一,瞧!退出代码 1,因为异常未被捕获。当然,所有这些都是猜测,除非

"powershell.exe" -executionpolicy bypass -noprofile -file "\\<REMOTESERVER>\reminst\customize\specialize.ps1"

确实有效,前提是运行它的帐户具有文件共享的权限。为了避免这些权限问题,您可以将代码粘贴到应答文件中的 {} 之间,然后使用该-command选项,

"powershell.exe" -executionpolicy bypass -noprofile -command {...}

相关内容