PowerShell 脚本无法从 autounattend.xml 文件运行

PowerShell 脚本无法从 autounattend.xml 文件运行

再会!

我创建了一个 autounattend.xml 文件,其中的设置包括跳过 OOBE、创建用户配置文件和在首次登录时运行 PowerShell 脚本。除了运行脚本之外,前两个都运行良好。我读过很多关于这个问题的主题,但不幸的是,没有一个能给我提供可行的答案。

以下是 autounattend.xml 的一个示例:

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State">
  <settings pass="offlineServicing" />
  <settings pass="windowsPE">
    <component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
      <SetupUILanguage>
        <UILanguage>en-US</UILanguage>
      </SetupUILanguage>
      <InputLocale>0409:00000409</InputLocale>
      <SystemLocale>en-US</SystemLocale>
      <UILanguage>en-US</UILanguage>
      <UserLocale>en-US</UserLocale>
    </component>
  </settings>
  <settings pass="generalize" />
  <settings pass="specialize" />
  <settings pass="auditSystem" />
  <settings pass="auditUser" />
  <settings pass="oobeSystem">
    <component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
      <InputLocale>0409:00000409</InputLocale>
      <SystemLocale>en-US</SystemLocale>
      <UILanguage>en-US</UILanguage>
      <UserLocale>en-US</UserLocale>
    </component>
    <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
      <UserAccounts>
        <LocalAccounts>
          <LocalAccount wcm:action="add">
            <Name>Admin</Name>
            <Group>Administrators</Group>
            <Password>
              <Value>mypassword</Value>
              <PlainText>true</PlainText>
            </Password>
          </LocalAccount>
        </LocalAccounts>
      </UserAccounts>
      <OOBE>
        <HideEULAPage>true</HideEULAPage>
        <ProtectYourPC>3</ProtectYourPC>
        <HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
        <NetworkLocation>Work</NetworkLocation>
      </OOBE>
      <FirstLogonCommands>
        <SynchronousCommand wcm:action="add">
          <CommandLine>FOR %i IN (C D E F G H I J K L N M) DO IF EXIST %i:\setup.ps1 Powershell -executionpolicy ByPass %i:\setup.ps1</CommandLine>
          <Description>Setup</Description>
          <Order>1</Order>
          <RequiresUserInput>false</RequiresUserInput>
        </SynchronousCommand>
      </FirstLogonCommands>
    </component>
  </settings>
</unattend>

你能帮我弄清楚吗?

谢谢你!

===已更新===

脚本很简单,脚本内容如下:

为了调试目的,我在桌面和 C 盘中的 txt 文件中添加了输出字符串。

$machineName = (Get-WmiObject Win32_bios).SerialNumber
Rename-Computer $machineName
"Hello World!" | Out-File "C:\delete_me.txt"
"Hello World!" | Out-File "$($home)\Desktop\delete_me.txt"

这意味着它不适用于普通权限和管理员权限。此外,setupact.log 会先尝试运行脚本,然后创建用户。这可能是问题所在吗?

2023-03-09 23:36:39, Info                         [Shell Unattend] Running 'oobeSystem' pass
2023-03-09 23:36:39, Info                         [Shell Unattend] LogonCommands: Set command 'FOR %i IN (C D E F G H I J K L N M) DO IF EXIST %i:\setup.ps1 Powershell -executionpolicy ByPass %i:\setup.ps1' 
2023-03-09 23:36:39, Info                         [Shell Unattend] UserAccounts: created account 'User'
2023-03-09 23:36:39, Info                         [Shell Unattend] UserAccounts: added 'User2' to group 'Users'
2023-03-09 23:36:39, Info                         [Shell Unattend] UserAccounts: Password set for 'User' 
2023-03-09 23:36:39, Info                         [Shell Unattend] UserAccounts: added 'User2' to group 'Administrators'
2023-03-09 23:36:39, Info                         [Shell Unattend] Exiting 'oobeSystem' pass with status 0x00000000

答案1

要开始排除故障,您需要更多关于出错情况的背景信息。您可以从以下位置获取详细的错误日志:

%WINDIR%\Panther\UnattendGC\SetupAct.log

获得此文件的内容后,请更新您的帖子,我们可以提供更多帮助。

更新:

因此看起来您正在尝试在 LogonCommands 中执行管理员操作;这并不理想,因为这将在用户上下文中运行,并且即使用户是管理员,也很可能不会被提升。

我建议,考虑到您的用例需要动态设置主机名,您应在专门的过程中使用 RunSynchronousCommand,这样它将作为系统运行。

https://learn.microsoft.com/en-us/windows-hardware/customize/desktop/unattend/microsoft-windows-deployment-runsynchronous-runsynchronouscommand

此外,您可以内联运行 PowerShell 命令,而不是使用需要搜索的外部文件。请参阅以下示例。

<RunSynchronousCommand wcm:action="add">
    <Description>Rename computer using PowerShell</Description>
    <Order>1</Order>
    <CommandLine>powershell -ExecutionPolicy Bypass -Command "& {(Get-WmiObject Win32_bios).SerialNumber | Rename-Computer}"</CommandLine>
    <RequiresUserInput>false</RequiresUserInput>
</RunSynchronousCommand>

答案2

感谢您向我指出该文档!

我能够使用以下配置运行脚本:(仍然想使用脚本文件,而不是命令,因为我添加了额外的代码)

 <settings pass="specialize">
    <component name="Microsoft-Windows-Deployment" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
      <RunSynchronous>
          <RunSynchronousCommand wcm:action="add">
              <Description>Setup</Description>
              <Order>1</Order>
              <Path>cmd /c FOR %i IN (C D E F G H I J K L N M) DO IF EXIST %i:\setup.ps1 Powershell -executionpolicy ByPass %i:\setup.ps1</Path>
              <WillReboot>Never</WillReboot>
          </RunSynchronousCommand>
      </RunSynchronous>
    </component>
  </settings>

相关内容