如何从安装媒体中的 autounattend.xml 文件中运行脚本?

如何从安装媒体中的 autounattend.xml 文件中运行脚本?

我编写了一些 PowerShell (.ps1) 和批处理 (.bat) 脚本,希望将其作为 Windows 自动安装的一部分。我已经使用 Windows 系统映像管理器配置了 autounattend.xml 文件中的所有其他内容,除了执行我的脚本之外,它运行良好。

在尝试完全自动化之前,我曾经绕过 OOBE 进入审计模式 (CTRL+SHIFT+F3),然后从那里运行我的脚本。知道我的脚本在审计模式下工作,我决定在 Windows 系统映像管理器中在 Pass 6 audituser 处插入同步命令。

这是我目前的 autouattend.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="oobeSystem">
        <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">
            <AutoLogon>
                <Password>
                    <Value>cABhAHMAcwB3AG8AcgBkAFAAYQBzAHMAdwBvAHIAZAA=</Value>
                    <PlainText>false</PlainText>
                </Password>
                <Enabled>true</Enabled>
                <LogonCount>3</LogonCount>
                <Username>LocalAdmin</Username>
            </AutoLogon>
            <UserAccounts>
                <LocalAccounts>
                    <LocalAccount wcm:action="add">
                        <Password>
                            <Value>cABhAHMAcwB3AG8AcgBkAFAAYQBzAHMAdwBvAHIAZAA=</Value>
                            <PlainText>false</PlainText>
                        </Password>
                        <Name>LocalAdmin</Name>
                        <Group>Administrators</Group>
                        <DisplayName></DisplayName>
                        <Description>Local adminstrator stored on the system drive</Description>
                    </LocalAccount>
                </LocalAccounts>
            </UserAccounts>
            <OOBE>
                <SkipMachineOOBE>true</SkipMachineOOBE>
                <SkipUserOOBE>true</SkipUserOOBE>
                <HideOnlineAccountScreens>true</HideOnlineAccountScreens>
                <HideEULAPage>true</HideEULAPage>
            </OOBE>
        </component>
    </settings>
    <settings pass="windowsPE">
        <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">
            <EnableNetwork>false</EnableNetwork>
            <DiskConfiguration>
                <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>
                            <Size>100</Size>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>4</Order>
                            <Extend>true</Extend>
                            <Type>Primary</Type>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>3</Order>
                            <Size>16</Size>
                            <Type>MSR</Type>
                        </CreatePartition>
                    </CreatePartitions>
                    <ModifyPartitions>
                        <ModifyPartition wcm:action="add">
                            <Format>NTFS</Format>
                            <Label></Label>
                            <Order>1</Order>
                            <PartitionID>1</PartitionID>
                            <TypeID>DE94BBA4-06D1-4D40-A16A-BFD50179D6AC</TypeID>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <Order>2</Order>
                            <Format>FAT32</Format>
                            <PartitionID>2</PartitionID>
                            <Label>System</Label>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <PartitionID>3</PartitionID>
                            <Order>3</Order>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <PartitionID>4</PartitionID>
                            <Order>4</Order>
                            <Format>NTFS</Format>
                        </ModifyPartition>
                    </ModifyPartitions>
                    <WillWipeDisk>true</WillWipeDisk>
                    <DiskID>0</DiskID>
                </Disk>
            </DiskConfiguration>
            <ImageInstall>
                <OSImage>
                    <InstallTo>
                        <DiskID>0</DiskID>
                        <PartitionID>4</PartitionID>
                    </InstallTo>
                    <InstallFrom>
                        <MetaData wcm:action="add">
                            <Key>/IMAGE/NAME</Key>
                            <Value>Windows 10 Pro</Value>
                        </MetaData>
                    </InstallFrom>
                </OSImage>
            </ImageInstall>
            <UserData>
                <ProductKey>
                    <WillShowUI>OnError</WillShowUI>
                    <Key />
                </ProductKey>
                <AcceptEula>true</AcceptEula>
            </UserData>
        </component>
        <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-gb</UILanguage>
            </SetupUILanguage>
            <InputLocale>en-gb</InputLocale>
            <SystemLocale>en-gb</SystemLocale>
            <UILanguageFallback>en-us</UILanguageFallback>
            <UILanguage>en-gb</UILanguage>
        </component>
    </settings>
    <settings pass="auditUser">
        <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>2</Order>
                    <Path>powershell.exe -executionpolicy remotesigned -File &quot;Files\Scripts\2A-Create_Daily_System_Restore_Point.ps1&quot;</Path>
                </RunSynchronousCommand>
                <RunSynchronousCommand wcm:action="add">
                    <Description>Remove Windows Bloatware Provisioned Apps</Description>
                    <Order>1</Order>
                    <Path>powershell.exe -executionpolicy remotesigned -File &quot;%~dp0\Files\Scripts\1-Remove_Bloatware_Apps.ps1&quot;</Path>
                </RunSynchronousCommand>
                <RunSynchronousCommand wcm:action="add">
                    <Order>3</Order>
                    <Path>cmd.exe /c %~dp0\Files\Scripts\3-Remove_Folders_from_This_PC.bat</Path>
                </RunSynchronousCommand>
            </RunSynchronous>
        </component>
    </settings>
    <cpi:offlineImage cpi:source="wim:d:/users/will/documents/it%20projects/windows%2010%20image/install.wim#Windows 10 Home" xmlns:cpi="urn:schemas-microsoft-com:cpi" />
</unattend>

我将在 Windows 安装介质的根目录中放置一个名为“Scripts”的目录,所以我的问题是,如何从安装介质中调用 autounattend.xml 中的这些脚本?是否有一个变量链接到安装介质所在的驱动器和路径?

答案1

我认为我已经找到解决办法了。

  1. 将 ISO/Windows 安装媒体提取到文件夹
  2. sources\在解压的安装介质的目录中创建一个名为原装正品原厂配件
  3. 镜像系统驱动器,以便您拥有具有以下路径的目录sources\$OEM$\$$\Setup\Scripts (C:\Windows\安装程序\脚本)

$OEM$ 文件夹镜像系统驱动器,因此这些文件夹中的所有内容都会在安装过程中复制过来。关于从 autounattend.xml 文件运行的脚本,您只需从%SystemDrive%\WinDir\Setup\Scripts

更多信息请见此处 >https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-8.1-and-8/hh825027%28v%3dwin.10%29

答案2

据我所知,auditUser除非系统处于审核模式,否则在 Windows 安装期间不会执行该过程。因此,除了您的解决方案之外,还有另外两种可能的解决方案:

  1. 将命令放在specializepass 或oobeSystempass 中。通常,安装介质会被分配字母X:,但如果您想确保安装程序能够找到脚本,您可以使用一些条件,如下所示:
                <RunSynchronousCommand wcm:action="add">
                    <Description>Run script</Description>
                    <Order>1</Order>
                    <!-- Enumerate through all drives looking for the MYIT_OEMBypass.ps1 script, if found, run it. Leave the command prompt open. -->
                    <Path>cmd /c "(FOR %i IN (C D E F G H I J K L N M) DO IF EXIST %i:\MYIT_OEMBypass.ps1 Powershell -executionpolicy ByPass %i:\MYIT_OEMBypass.ps1) & pause"</Path>
                </RunSynchronousCommand>
  1. oobeSystem通过输入以下命令,指示 Windows 安装程序在开始执行过程时直接进入审核模式 (sysprep) :
        <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">
            <Reseal>
                <Mode>Audit</Mode>
                <ForceShutdownNow>false</ForceShutdownNow>
            </Reseal>
        </component>

使用此方法,您能保持传递命令auditUser。这也有助于设置包含所有您喜欢的调整的默认配置文件,因此以后创建的任何配置文件也将应用所有这些调整。

然而,默认情况下,汽车unattend.xml 将被复制并设置为 sysprep 的 unattend.xml 脚本,这显然会导致问题。因此,您需要进行一些小的更改,以指示 sysprep 使用不同的unattend.xml。此方法的更详细指南可在此处找到: https://rzander.azurewebsites.net/modern-os-deployment-mosd/

相关内容