Powershell Commands in Unattend not running correctly


mowright

New member
Local time
9:27 AM
Posts
6
OS
Windows 11
Hello,

I have this unattend file I am working onn, everything works for the most part and the computer will restart once logged in but the other powershell commands must not be running because the computer name will not change, if you run the computer rename in powershell it has to be ran as admin, could this be the issue?

Code:
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <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>0c09:00000409</InputLocale>
            <SystemLocale>en-US</SystemLocale>
            <UILanguage>en-US</UILanguage>
            <UILanguageFallback>en-US</UILanguageFallback>
            <UserLocale>en-AU</UserLocale>
        </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>
                <Disk wcm:action="add">
                    <DiskID>0</DiskID>
                    <WillWipeDisk>true</WillWipeDisk>
                    <CreatePartitions>
                        <CreatePartition wcm:action="add">
                            <Order>1</Order>
                            <Type>Primary</Type>
                            <Size>300</Size>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>2</Order>
                            <Type>EFI</Type>
                            <Size>100</Size>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>3</Order>
                            <Type>MSR</Type>
                            <Size>128</Size>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>4</Order>
                            <Type>Primary</Type>
                            <Extend>true</Extend>
                        </CreatePartition>
                    </CreatePartitions>
                    <ModifyPartitions>
                        <ModifyPartition wcm:action="add">
                            <Order>1</Order>
                            <PartitionID>1</PartitionID>
                            <Label>WINRE</Label>
                            <Format>NTFS</Format>
                            <TypeID>DE94BBA4-06D1-4D40-A16A-BFD50179D6AC</TypeID>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <Order>2</Order>
                            <PartitionID>2</PartitionID>
                            <Label>System</Label>
                            <Format>FAT32</Format>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <Order>3</Order>
                            <PartitionID>3</PartitionID>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <Order>4</Order>       
                            <PartitionID>4</PartitionID>
                            <Label>OS</Label>
                            <Letter>C</Letter>
                            <Format>NTFS</Format>
                        </ModifyPartition>
                    </ModifyPartitions>
                </Disk>
            </DiskConfiguration>
            <ImageInstall>
                <OSImage>
                    <InstallTo>
                        <DiskID>0</DiskID>
                        <PartitionID>4</PartitionID>
                    </InstallTo>
                    <InstallToAvailablePartition>false</InstallToAvailablePartition>
                </OSImage>
            </ImageInstall>
            <UserData>
                <ProductKey>
                    <Key></Key>
                    <WillShowUI>Never</WillShowUI>
                </ProductKey>
                <AcceptEula>true</AcceptEula>
            </UserData>
        </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>
            <UILanguageFallback>en-US</UILanguageFallback>
            <UserLocale>en-US</UserLocale>
        </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>
                <NetworkLocation>Work</NetworkLocation>
                <HideLocalAccountScreen>true</HideLocalAccountScreen>
                <HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
                <HideOnlineAccountScreens>true</HideOnlineAccountScreens>
                <SkipMachineOOBE>true</SkipMachineOOBE>
                <SkipUserOOBE>true</SkipUserOOBE>
            </OOBE>
            <UserAccounts>
                <LocalAccounts>
                    <LocalAccount wcm:action="add">
                        <Password>
                            <Value>****</Value>
                            <PlainText>true</PlainText>
                        </Password>
                        <Description></Description>
                        <DisplayName>admin</DisplayName>
                        <Group>Administrators</Group>
                        <Name>admin</Name>
                    </LocalAccount>
                </LocalAccounts>
            </UserAccounts>
            <FirstLogonCommands>
                        <SynchronousCommand wcm:action="add">
                            <CommandLine>powershell.exe start-sleep -s 200</CommandLine>
                            <Description>Reboot after Computer Name Switch</Description>
                            <Order>49</Order>
                            <RequiresUserInput>false</RequiresUserInput>
                        </SynchronousCommand>
                        <SynchronousCommand wcm:action="add">
                            <CommandLine>powershell.exe $SerialNumber = (Get-WmiObject -class win32_bios).SerialNumber</CommandLine>
                            <Description>Get Serial Number</Description>
                            <Order>50</Order>
                            <RequiresUserInput>false</RequiresUserInput>
                        </SynchronousCommand>
                        <SynchronousCommand wcm:action="add">
                            <CommandLine>powershell.exe $computer = "EPS-$SerialNumber"</CommandLine>
                            <Description>Set Computer Name Perimeters</Description>
                            <Order>51</Order>
                            <RequiresUserInput>false</RequiresUserInput>
                        </SynchronousCommand>
                        <SynchronousCommand wcm:action="add">
                            <CommandLine>powershell.exe Rename-Computer -NewName $computer -Force</CommandLine>
                            <Description>Set Computer Name</Description>
                            <Order>52</Order>
                            <RequiresUserInput>false</RequiresUserInput>
                        </SynchronousCommand>
                        <SynchronousCommand wcm:action="add">
                            <CommandLine>C:\Windows\System32\shutdown.exe /r /f /t 0</CommandLine>
                            <Description>Reboot after Computer Name Switch</Description>
                            <Order>53</Order>
                            <RequiresUserInput>false</RequiresUserInput>
                        </SynchronousCommand>
                </FirstLogonCommands>
            </component>
        </settings>
        <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">
                <ComputerName>EPS-XXXXXX</ComputerName>
                <ShowWindowsLive>false</ShowWindowsLive>
                <TimeZone>Central Standard Time</TimeZone>
            <AutoLogon>
                <Password>
                    <Value>****</Value>
                    <PlainText>true</PlainText>
                </Password>
                <Domain>eps-enidk12.org</Domain>
                <Enabled>true</Enabled>
                <LogonCount>2</LogonCount>
                <Username>admin</Username>
            </AutoLogon>
            </component>
        </settings>
    <cpi:offlineImage cpi:source="wim:c:/users/fwadmin/desktop/install.wim" xmlns:cpi="urn:schemas-microsoft-com:cpi" />
</unattend>
 
Windows Build/Version
Windows 11

My Computer

System One

  • OS
    Windows 11
Your PS script to rename this computer is split over several lines, which is wrong. Each SynchronousCommand is executed independently, so variables don't persist to the next line.

Wrap your one-liner in quotes, and replace every double-quote with 4 double-quotes to escape them (" -> """"").

Correct format:
Code:
<CommandLine>powershell.exe "start-sleep -s 200; $SerialNumber = (Get-WmiObject -class win32_bios).SerialNumber; $computer = """"EPS-$SerialNumber""""; Rename-Computer -NewName $computer -Force"</CommandLine>

But you're not paying attention to the computer's name being possibly longer than 15 characters (at least on my PC).

While you could truncate after the first 15 characters, that's a bad assumption since the most unique S/N digits are the last ones. If your organization has purchased a batch of PC's at the same time, the first digits tend to be identical.
Code:
<CommandLine>powershell.exe "Start-Sleep -s 200; $SN = (Get-WmiObject -class Win32_BIOS).SerialNumber; $Truncated = $SN.Substring(4, $SN.Length - 4); $Computer = """"EPS-$Truncated""""; $Computer; Rename-Computer -NewName $Computer -Force"<CommandLine>
 
Last edited:

My Computer

System One

  • OS
    Windows 7
Your PS script to rename this computer is split over several lines, which is wrong. Each SynchronousCommand is executed independently, so variables don't persist to the next line.

Wrap your one-liner in quotes, and replace every double-quote with 4 double-quotes to escape them (" -> """"").

Correct format:
Code:
<CommandLine>powershell.exe "start-sleep -s 200; $SerialNumber = (Get-WmiObject -class win32_bios).SerialNumber; $computer = """"EPS-$SerialNumber""""; Rename-Computer -NewName $computer -Force"</CommandLine>

But you're not paying attention to the computer's name being possibly longer than 15 characters (at least on my PC).

While you could truncate after the first 15 characters, that's a bad assumption since the most unique S/N digits are the last ones. If your organization has purchased a batch of PC's at the same time, the first digits tend to be identical.
Code:
<CommandLine>powershell.exe "Start-Sleep -s 200; $SN = (Get-WmiObject -class Win32_BIOS).SerialNumber; $Truncated = $SN.Substring(4, $SN.Length - 4); $Computer = """"EPS-$Truncated""""; $Computer; Rename-Computer -NewName $Computer -Force"<CommandLine>
Alright sweet what you said made it work!

Another question I might add is on how would I be able to make more lines of Sync commands go on after a restart, they just stop, is there a way to work around this?
 

My Computer

System One

  • OS
    Windows 11
MS warns you never to execute a restart while running commands. The reason is it doesn't work like a checklist, where it resumes where it left off. If you want to perform a shutdown, call it as your last command.

1 -> command A
2 -> command B
3 -> command C
4 -> shutdown /r
 

My Computer

System One

  • OS
    Windows 7
MS warns you never to execute a restart while running commands. The reason is it doesn't work like a checklist, where it resumes where it left off. If you want to perform a shutdown, call it as your last command.

1 -> command A
2 -> command B
3 -> command C
4 -> shutdown /r
See but the way I am having to do this we cannot add a computer until the computer name is the EPS-%serialtag% and the computer wont rename until a restart.
 

My Computer

System One

  • OS
    Windows 11
See but the way I am having to do this we cannot add a computer until the computer name is the EPS-%serialtag% and the computer wont rename until a restart.
I should add on that our Serial tags are 7 digits so the computer names end up being EPS-XXXXXXX
 

My Computer

System One

  • OS
    Windows 11
Back
Top Bottom