最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

automation - How to trigger a Powershell Script via unattend phase - Stack Overflow

programmeradmin3浏览0评论

I'm trying and failing to run a PowerShell script in an unattend.xml file.

This is the unattend.xml file located in C:\Windows\System32\Sysprep, which is supposed to run when deploying an image on a machine.

I have tried different phases in the file, following the exact syntax for each, but it still doesn't work.

For Example (inside "oobeSystem" phase):

        <FirstLogonCommands>
            <SynchronousCommand wcm:action="add">
                <Order>2</Order>
                <CommandLine>"powershell.exe -ExecutionPolicy Bypass -File C:\Scripts\Startup_Script.ps1"</CommandLine>
                <Description>Repartitioning</Description>
            </SynchronousCommand>
        </FirstLogonCommands>

(The script configures the disk partitions as required by calling diskpart with a dedicated script as a parameter. It resides in a non-system folder, which should prevent permission issues.)

Any insights or suggestions would be greatly appreciated!

I'm trying and failing to run a PowerShell script in an unattend.xml file.

This is the unattend.xml file located in C:\Windows\System32\Sysprep, which is supposed to run when deploying an image on a machine.

I have tried different phases in the file, following the exact syntax for each, but it still doesn't work.

For Example (inside "oobeSystem" phase):

        <FirstLogonCommands>
            <SynchronousCommand wcm:action="add">
                <Order>2</Order>
                <CommandLine>"powershell.exe -ExecutionPolicy Bypass -File C:\Scripts\Startup_Script.ps1"</CommandLine>
                <Description>Repartitioning</Description>
            </SynchronousCommand>
        </FirstLogonCommands>

(The script configures the disk partitions as required by calling diskpart with a dedicated script as a parameter. It resides in a non-system folder, which should prevent permission issues.)

Any insights or suggestions would be greatly appreciated!

Share Improve this question edited Mar 26 at 8:55 Hanoch Sagiv asked Mar 26 at 8:13 Hanoch SagivHanoch Sagiv 11 bronze badge 2
  • Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Mar 26 at 8:24
  • Done @Community. Thanks. – Hanoch Sagiv Commented Mar 26 at 8:56
Add a comment  | 

2 Answers 2

Reset to default 0

What I have seen others do is embed the script in the actual autounattend XML file iteslf and you would have to look at their github page to reverse engineer it for yourself.

This is cschneegans' xml generator

I think there is also a way to edit the ISO image itself and have an $OEM folder or something like that, but it's been too long since I looked at it so you would have to research that.

A good tool I used for editing the ISO image is called "AnyBurn"

Stand by for the admin that tells me how my answer does not solve your problem
in 3, 2, . . .

Your problem is to partition the storage drive using an operating system answer file (autounattend.xml).

  1. Partition using diskpart

The solution you proposed to use is the diskpart tool, called by powershell, in one of the ISO image installation phases.

I started using some answer file generators, such as "schneegans.de" (https://schneegans.de/windows/unattend-generator/), suggested by Vern_Anderson. Although it is an excellent tool, the problem is that you are tied to it. If you need a custom solution, you don't need support. The ideal is to try to understand how a tool can solve the problem.

In the case of the "schneegans.de" tool, it participated in the "windowsPE" phase. It doesn't make sense to install and then resize. That's why the "windowsPE" phase is used for partitioning. The operating system installation is after the "windowsPE" phase. Avoid using the "specialize" or "oobeSystem" phases for partitioning.

Now comes the first question: after a basic installation of the operating system, does your routine work? If the answer is yes, we can "try" to put the routine in the answer file. But is it possible? The tool "schneegans.de" reports that it is possible, but the "windowsPE" phase is quite restricted. The tool configures it as follows:

    <settings pass="windowsPE">
        <component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
            <UseConfigurationSet>false</UseConfigurationSet>
            <RunSynchronous>
                <RunSynchronousCommand wcm:action="add">
                    <Order>1</Order>
                    <Path>cmd.exe /c "&gt;&gt;"X:\diskpart.txt" (echo SELECT DISK=0&amp;echo CLEAN&amp;echo CONVERT GPT&amp;echo CREATE PARTITION EFI SIZE=499&amp;echo FORMAT QUICK FS=FAT32 LABEL="System"&amp;echo CREATE PARTITION MSR SIZE=16)"</Path>
                </RunSynchronousCommand>
                <RunSynchronousCommand wcm:action="add">
                    <Order>2</Order>
                    <Path>cmd.exe /c "&gt;&gt;"X:\diskpart.txt" (echo CREATE PARTITION PRIMARY&amp;echo SHRINK MINIMUM=699&amp;echo FORMAT QUICK FS=NTFS LABEL="Windows"&amp;echo CREATE PARTITION PRIMARY&amp;echo FORMAT QUICK FS=NTFS LABEL="Recovery")"</Path>
                </RunSynchronousCommand>
                <RunSynchronousCommand wcm:action="add">
                    <Order>3</Order>
                    <Path>cmd.exe /c "&gt;&gt;"X:\diskpart.txt" (echo SET ID="de94bba4-06d1-4d40-a16a-bfd50179d6ac"&amp;echo GPT ATTRIBUTES=0x8000000000000001)"</Path>
                </RunSynchronousCommand>
                <RunSynchronousCommand wcm:action="add">
                    <Order>4</Order>
                    <Path>cmd.exe /c "diskpart.exe /s "X:\diskpart.txt" &gt;&gt;"X:\diskpart.log" || ( type "X:\diskpart.log" &amp; echo diskpart encountered an error. &amp; pause &amp; exit /b 1 )"</Path>
                </RunSynchronousCommand>
            </RunSynchronous>
        </component>
    </settings>

Note the complexity. The XML response file needs to be encoded in the "EncodeHTML" format. So, the ">" symbol becomes "& gt;". The "&" symbol becomes "& amp;". The tool creates the file "X:\diskpart.txt", executing the redirection line by line with the "cmd.exe" command. The last command is to call the "diskpart.exe" tool, passing as a parameter the created file "X:\diskpart.txt".

There is no need to edit the ISO image. I do not recommend this solution. In this case, the tool placed the "code" in the response file, with the limitation of the "EncodeHTML" encoding.

But if I want to inject files, how can I do it? In this case, the best tool is "Ventoy" (https://www.ventoy/en/plugin_injection.html). With this tool, you can pass the response file (https://www.ventoy/en/plugin_autoinstall.html), in addition to being able to inject files.

If you still want to use the powershell code, which calls diskpart.exe, in the "specialize" or "oobeSystem" phases, the "schneegans.de" tool has this feature. But I suggest you explore it to understand how it is done. I can tell you in advance that the procedure is quite complex.

  1. Partitioning using the answer file in the "windowsPE" phase

Here is the solution that I understand to be the most appropriate for your problem. You do not need external commands to partition. The answer file itself does it for you. For example, I create drives C: and D:. For Windows 11, I will need 5 partitions: ESP, WinRE, MSR, Applications and Users. Exactly what you read. I place the user profiles in the "D:\Usuários" folder, in the native language (Brazilian Portuguese).

2.1) Setting up the partitions

    <settings pass="windowsPE">
            <DiskConfiguration>
                <Disk wcm:action="add">
                    <DiskID>0</DiskID>
                    <WillWipeDisk>true</WillWipeDisk>
                    <CreatePartitions>
                        <!-- Sistema (ESP) -->
                        <CreatePartition wcm:action="add">
                            <Order>1</Order>
                            <Type>EFI</Type>
                            <Size>499</Size>
                        </CreatePartition>
                        <!-- Recuperação -->
                        <CreatePartition wcm:action="add">
                            <Order>2</Order>
                            <Type>Primary</Type>
                            <Size>699</Size>
                        </CreatePartition>
                        <!-- Reservada -->
                        <CreatePartition wcm:action="add">
                            <Order>3</Order>
                            <Type>MSR</Type>
                            <Size>99</Size>
                        </CreatePartition>
                        <!-- Sistema operacional -->
                        <CreatePartition wcm:action="add">
                            <Order>4</Order>
                            <Type>Primary</Type>
                            <Size>102400</Size>
                        </CreatePartition>
                        <!-- Dados -->
                        <CreatePartition wcm:action="add">
                            <Order>5</Order>
                            <Type>Primary</Type>
                            <Extend>true</Extend>
                            <!-- <Size>102400</Size> -->
                        </CreatePartition>
                    </CreatePartitions>
                    <ModifyPartitions>
                        <!-- Sistema (ESP) -->
                        <ModifyPartition wcm:action="add">
                            <Order>1</Order>
                            <PartitionID>1</PartitionID>
                            <Label>ESP</Label>
                            <Format>FAT32</Format>
                        </ModifyPartition>
                        <!-- Recuperação -->
                        <ModifyPartition wcm:action="add">
                            <Order>2</Order>
                            <PartitionID>2</PartitionID>
                            <Label>WINRE</Label>
                            <Format>NTFS</Format>
                            <TypeID>DE94BBA4-06D1-4D40-A16A-BFD50179D6AC</TypeID>
                        </ModifyPartition>
                        <!-- Reservada -->
                        <ModifyPartition wcm:action="add">
                            <Order>3</Order>
                            <PartitionID>3</PartitionID>
                        </ModifyPartition>
                        <!-- Sistema operacional -->
                        <ModifyPartition wcm:action="add">
                            <Order>4</Order>
                            <PartitionID>4</PartitionID>
                            <Label>SO</Label>
                            <Letter>C</Letter>
                            <Format>NTFS</Format>
                        </ModifyPartition>
                        <!-- Dados -->
                        <ModifyPartition wcm:action="add">
                            <Order>5</Order>
                            <PartitionID>5</PartitionID>
                            <Label>Aplic</Label>
                            <Letter>D</Letter>
                            <Format>NTFS</Format>
                        </ModifyPartition>
                    </ModifyPartitions>
                </Disk>
            </DiskConfiguration>
            <ImageInstall>
                <OSImage>
                    <Compact>false</Compact>
                    <InstallFrom>
                        <MetaData wcm:action="add">
                            <Key>/image/name</Key>
                            <Value>Windows 11 Pro</Value>
                        </MetaData>
                    </InstallFrom>
                    <InstallTo>
                        <DiskID>0</DiskID>
                        <PartitionID>4</PartitionID>
                    </InstallTo>
                </OSImage>
            </ImageInstall>
            <UserData>
                <ProductKey>
                    <Key>VK7JG-NPHTM-C97JM-9MPGT-3V66T</Key>
                    <WillShowUI>OnError</WillShowUI>
                </ProductKey>
                <AcceptEula>true</AcceptEula>
            </UserData>
        </component>
    </settings>

2.2) Modifying the user profile folder

    <settings pass="oobeSystem">
        <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
            <FolderLocations>
                <ProfilesDirectory>D:\Usu&#225;rios</ProfilesDirectory>
            </FolderLocations>
            <OOBE>
                <ProtectYourPC>3</ProtectYourPC>
                <HideEULAPage>true</HideEULAPage>
                <HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
                <HideOnlineAccountScreens>true</HideOnlineAccountScreens>
            </OOBE>
        </component>
    </settings>

Please note that the profiles folder has an accented character. Therefore, I needed to encode the symbol "á" to "& #225;". I put this item just to emphasize to you that if you put the code embedded in the XML, you will need to encode it in the "EncodeHTML" format.

Remember to use Ventoy. It is the best tool to initialize an image, with response file and injected files. And without having to touch the original image!

I used an online translator. I apologize for not being fluent in the language.

发布评论

评论列表(0)

  1. 暂无评论