有人告诉我可以在操作系统安装期间直接从 Unattend.xml 运行 Powershell。
我试图将 ComputerName 设置为 (A + “SerialNumber”),但无论我如何写入文件,它都会不断被跳过。
目前我有如下的 XML
<settings pass="specialize">
<component name="Microsoft-Windows-International-Core" 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">
<InputLocale>en-IE</InputLocale>
<SystemLocale>en-IE</SystemLocale>
<UILanguage>en-IE</UILanguage>
<UserLocale>en-IE</UserLocale>
<UILanguageFallback></UILanguageFallback>
</component>
<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">
<RunAsynchronous>
<RunAsynchronousCommand wcm:action="add">
<Path>powershell.exe –ExecutionPolicy Bypass -File "\\server\store\Software\Powershell\UnattendedInstallComputerName.ps1"</Path>
<Description>Use script to rename computer with Serial number</Description>
<Order>1</Order>
</RunAsynchronousCommand>
</RunAsynchronous>
</component>
</settings>
<settings pass="oobeSystem">
<component name="Microsoft-Windows-International-Core" 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">
<InputLocale>en-IE</InputLocale>
<SystemLocale>en-IE</SystemLocale>
<UILanguage>en-IE</UILanguage>
<UserLocale>en-IE</UserLocale>
</component>
</settings>
脚本的调用方式如下
$Serial = Get-WmiObject win32_bios | select -expand serialnumber
$CompName = "A" + $Serial
Rename-Computer $CompName
现在我已经在不同的运行中对上述操作进行了大量的尝试,但都没有成功。老实说,我只是把“powershell.exe –ExecutionPolicy Bypass -File”当作万福玛利亚。
任何帮助,将不胜感激
答案1
我在 Windows 10 Pro 上使用它,请注意:专业化阶段的同步命令似乎正在运行前同一阶段的其他部分。因此,如果您在 WDS 客户端命名策略中指定了 PC 名称,并且已将 xml 配置为重命名,那么您就会遇到该问题。
专家提示(部署后,转到 C:\windows\panther\UnattendGC\setupact.log 查看处理的内容和时间。)
我建议在 oobe 阶段运行 pc rename。
另外,要执行的命令的最大长度总共约为 250 个字符。它说的是 ~1000,但我尝试过使用 ~500 个字符的 powershell 命令,它们都失败了。
简而言之,在 oobe 阶段尝试此命令:C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicybypass-noprofile-command“Start-Transcript;(Get-WmiObjectwin32_computersystem).Rename((Get-WmiObject'win32_bios').serialnumber.trim());Stop-Transcript”
请注意,此 xml 还包含 oobe 阶段域加入的结构,这对我很有用。如果您有安全意识,请创建一个 wds 域加入帐户,仅用于控制特定 OU 计算机对象(有相关指南),并在大规模部署后更改域加入服务帐户上的密码。您可以在 xml 中快速更新密码,并在进行另一个批量部署操作时重新导入到 WDS 服务器上。
XML 供参考...
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<servicing></servicing>
<settings pass="specialize">
<component name="Microsoft-Windows-Shell-Setup" 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">
<DesktopOptimization>
<ShowWindowsStoreAppsOnTaskbar>false</ShowWindowsStoreAppsOnTaskbar>
</DesktopOptimization>
<ComputerName></ComputerName>
<CopyProfile>true</CopyProfile>
<ShowPowerButtonOnStartScreen>true</ShowPowerButtonOnStartScreen>
<TimeZone>US Mountain Standard Time</TimeZone>
</component>
<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>4</Order>
<Path>CMD /c REG.exe add HKLM\System\CurrentControlSet\Control\Network\NewNetworkWindowOff /f</Path>
<Description>prevent the Network location from appearing</Description>
</RunSynchronousCommand>
</RunSynchronous>
</component>
<component name="Microsoft-Windows-Security-SPP-UX" 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">
<SkipAutoActivation>true</SkipAutoActivation>
</component>
<component name="Microsoft-Windows-TerminalServices-LocalSessionManager" 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">
<fDenyTSConnections>false</fDenyTSConnections>
</component>
<component name="Microsoft-Windows-SQMApi" 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">
<CEIPEnabled>0</CEIPEnabled>
</component>
</settings>
<settings pass="oobeSystem">
<component name="Microsoft-Windows-International-Core" 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">
<InputLocale>en-US</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UserLocale>en-US</UserLocale>
<UILanguageFallback>en-US</UILanguageFallback>
</component>
<component name="Microsoft-Windows-Shell-Setup" 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">
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<NetworkLocation>Work</NetworkLocation>
<HideLocalAccountScreen>true</HideLocalAccountScreen>
<ProtectYourPC>3</ProtectYourPC>
<SkipMachineOOBE>true</SkipMachineOOBE>
<SkipUserOOBE>true</SkipUserOOBE>
<UnattendEnableRetailDemo>false</UnattendEnableRetailDemo>
</OOBE>
<UserAccounts>
<LocalAccounts>
<LocalAccount wcm:action="add">
<Password>
<Value>=OBVIOUSLYREPLACED...</Value>
<PlainText>false</PlainText>
</Password>
<Description>Admin User</Description>
<DisplayName>Admin User</DisplayName>
<Group>administrators</Group>
<Name>admin</Name>
</LocalAccount>
</LocalAccounts>
</UserAccounts>
<AutoLogon>
<Password>
<Value>=OBVIOUSLYREPLACED...</Value>
<PlainText>false</PlainText>
</Password>
<LogonCount>5</LogonCount>
<Enabled>true</Enabled>
<Username>admin</Username>
</AutoLogon>
<DesktopOptimization>
<ShowWindowsStoreAppsOnTaskbar>false</ShowWindowsStoreAppsOnTaskbar>
</DesktopOptimization>
<FirstLogonCommands>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noprofile -command "Start-Transcript;(Get-WmiObject win32_computersystem).Rename((Get-WmiObject 'win32_bios').serialnumber.trim());Stop-Transcript"</CommandLine>
<Description>Rename PC to ServiceTag</Description>
<Order>10</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\Windows\System32\shutdown.exe /r /f /t 0</CommandLine>
<Description>Reboot to Rename</Description>
<Order>11</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /c "Del C:\j.ps1 /F /Q"</CommandLine>
<Description>Delete powershell script to join to domain</Description>
<Order>40</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\Windows\System32\shutdown.exe /r /f /t 0</CommandLine>
<Description>Reboot after Domain Join</Description>
<Order>48</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /c REG.exe add HKLM\System\CurrentControlSet\Control\Network\NewNetworkWindowOff /f</CommandLine>
<Order>1</Order>
<Description>prevent the Network location from appearing</Description>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>21</Order>
<CommandLine>CMD /c "Del C:\t.ps1 /F /Q"</CommandLine>
<Description>Delete powershell script for power options</Description>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Description>Execute script to Disable lid close action Sleep While Powered and never sleep while on AC power</Description>
<Order>25</Order>
<RequiresUserInput>false</RequiresUserInput>
<CommandLine>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoLogo -Noninteractive -File "C:\t.ps1"</CommandLine>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noprofile -command "Start-Transcript;(Invoke-Expression -Command $('POWERCFG /H Off') -Verbose);Stop-Transcript;"</CommandLine>
<Description>Disable Hybernate</Description>
<Order>20</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\Windows\System32\gpupdate.exe /target:computer /force</CommandLine>
<Description>Force a GPO Update</Description>
<Order>50</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\Windows\System32\shutdown.exe /r /f /t 0</CommandLine>
<Description>Reboot to complete gpo update</Description>
<Order>51</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noprofile -command "Start-Transcript;Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name 'fDenyTSConnections' –Value 0"</CommandLine>
<Description>Enable RDP</Description>
<Order>30</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noprofile -command "Start-Transcript;Enable-NetFirewallRule -DisplayGroup 'Remote Desktop' -verbose;Stop-Transcript"</CommandLine>
<Description>Enable Firewall RDP Rule</Description>
<Order>32</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\windows\system32\cmd.exe /c "Netsh advfirewall firewall set rule group="remote desktop" new enable=yes"</CommandLine>
<Description>Enable Remote Desktop Rule via cmd</Description>
<Order>31</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Description>Wait and Reboot Once more After Gpupdate</Description>
<CommandLine>C:\Windows\System32\shutdown.exe /r /f /t 0</CommandLine>
<Order>55</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\Windows\System32\shutdown.exe /r /f /t 0</CommandLine>
<Description>Reboot once more</Description>
<Order>60</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>22</Order>
<CommandLine>CMD /c "echo Start-Transcript;$PSO=(Invoke-Expression -Command $('POWERCFG /GETACTIVESCHEME'));[GUID]$CPD=$PSO.split(':')[1].split('(')[0].trim() >> C:\t.ps1"</CommandLine>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /c "echo [GUID]$SSG='4f971e89-eebd-4455-a8de-9e59040e7347';[GUID]$SAS='5ca83367-6e45-459f-a27b-476b1d01c936';$SDS=000;(Invoke-Expression -Command $("POWERCFG /SETACVALUEINDEX $CPD $SSG $SAS $SDS") -Verbose) >> C:\t.ps1"</CommandLine>
<Order>23</Order>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /c "echo [GUID]$SSG='238c9fa8-0aad-41ed-83f4-97be242c8f20';[GUID]$SAS='29f6c1db-86da-48c5-9fdb-f2b67b1f44da';$SDS=000;(Invoke-Expression -Command $("POWERCFG /SETACVALUEINDEX $CPD $SSG $SAS $SDS") -Verbose);Stop-Transcript >> C:\t.ps1"</CommandLine>
<Order>24</Order>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>41</Order>
<CommandLine>CMD /c "echo Start-Transcript;Write-Host 'Waiting 20 seconds for Network...';Start-Sleep -seconds 20 >> C:\j.ps1"</CommandLine>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /c "echo $Cred = $(New-Object -Type 'PSCredential' -Arg 'MYDOMAIN.COM\WDSDomainJoinAccount',$(ConvertTo-SecureString '=OBVIOUSLYREPLACED...' -AsPlain -Force)) >> C:\j.ps1"</CommandLine>
<Order>42</Order>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /c "echo $OU = 'OU=Windows Deployment Machines,OU=SBSComputers,OU=Computers,OU=MyBusiness,DC=MYDOMAIN.COM,DC=local' >> C:\j.ps1"</CommandLine>
<Order>43</Order>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /c "echo Add-Computer -Domain 'MYDOMAIN.COM.local' -Cred $Cred -OU $OU -Force -Verbose;Stop-Transcript >> C:\j.ps1"</CommandLine>
<Order>44</Order>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoLogo -Noninteractive -File "C:\j.ps1"</CommandLine>
<Order>45</Order>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>CMD /C "Del C:\*.ps1 /f /q"</CommandLine>
<Description>Remove powershell scripts</Description>
<Order>70</Order>
<RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand>
</FirstLogonCommands>
<WindowsFeatures>
<ShowInternetExplorer>true</ShowInternetExplorer>
</WindowsFeatures>
</component>
</settings>
<settings pass="windowsPE">
<component name="Microsoft-Windows-International-Core-WinPE" 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">
<SetupUILanguage>
<UILanguage>en-US</UILanguage>
</SetupUILanguage>
<InputLocale>en-US</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UserLocale>en-US</UserLocale>
<UILanguageFallback>en-US</UILanguageFallback>
</component>
<component name="Microsoft-Windows-Setup" 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">
<DiskConfiguration>
<WillShowUI>OnError</WillShowUI>
<Disk wcm:action="add">
<CreatePartitions>
<CreatePartition wcm:action="add">
<Order>1</Order>
<Size>500</Size>
<Type>Primary</Type>
</CreatePartition>
<CreatePartition wcm:action="add">
<Order>2</Order>
<Type>EFI</Type>
<Extend>false</Extend>
<Size>100</Size>
</CreatePartition>
<CreatePartition wcm:action="add">
<Order>3</Order>
<Size>16</Size>
<Type>MSR</Type>
</CreatePartition>
<CreatePartition wcm:action="add">
<Extend>true</Extend>
<Order>4</Order>
<Type>Primary</Type>
</CreatePartition>
</CreatePartitions>
<ModifyPartitions>
<ModifyPartition wcm:action="add">
<Format>NTFS</Format>
<Label>WinRE</Label>
<Order>1</Order>
<PartitionID>1</PartitionID>
<TypeID>DE94BBA4-06D1-4D40-A16A-BFD50179D6AC</TypeID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Format>FAT32</Format>
<Order>2</Order>
<PartitionID>2</PartitionID>
<Label>System</Label>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>3</Order>
<PartitionID>3</PartitionID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<PartitionID>4</PartitionID>
<Order>4</Order>
<Letter>C</Letter>
<Label>Windows</Label>
<Format>NTFS</Format>
</ModifyPartition>
</ModifyPartitions>
<DiskID>0</DiskID>
<WillWipeDisk>true</WillWipeDisk>
</Disk>
</DiskConfiguration>
<ImageInstall>
<OSImage>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>4</PartitionID>
</InstallTo>
</OSImage>
</ImageInstall>
<UserData>
<ProductKey>
<Key>VK7JG-NPHTM-C97JM-9MPGT-3V66T</Key>
</ProductKey>
<AcceptEula>true</AcceptEula>
</UserData>
<WindowsDeploymentServices>
<Login>
<Credentials>
<Domain>MYDOMAIN.COM.local</Domain>
<Username>WDSServiceAccount</Username>
<Password>23473wtusjuwe6wsths</Password>
</Credentials>
</Login>
<ImageSelection>
<InstallImage>
<Filename>Windows 10 Pro Dell.wim</Filename>
<ImageGroup>Windows10</ImageGroup>
<ImageName>Windows 10 Pro Dell</ImageName>
</InstallImage>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>4</PartitionID>
</InstallTo>
</ImageSelection>
</WindowsDeploymentServices>
</component>
</settings>
<cpi:offlineImage cpi:source="catalog:c:/users/=OBVIOUSLYREPLACED.../desktop/windows sim catalog files/install_windows 10 pro.clg" xmlns:cpi="urn:schemas-microsoft-com:cpi" />
</unattend>