PowerShell – Run Scriptblock at End of Unattended WDS Deployment

powershellwds

During an unattended installation with WDS, I need to run a script on a share just at the end of pass 7 oobeSystem.

I use the FirstLogonCommands / SynchronousCommand option and provide this command line:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -ExecutionPolicy Bypass -WindowStyle Maximized -NoProfile -Command {New-SmbMapping -LocalPath z: -RemotePath \\10.10.10.5\Share -Persistent $false -UserName user -Password password; z:\script.ps1}

It opens the PowerShell console, but displays the scriptblock at the top of this window before displaying the command prompt, and unfortunately does not execute the scriptblock.

I tried to introduce a sleep before, but it doesn't change the result.
Here is an example:

pow

How can I fix this?

Best Answer

The FirstLogonCommands documentation shows an XML Example how to specify two commands to run after first logon. Modifying CommandLine according to your circumstances, relevant part of an unattend.xml could look as follows:

<FirstLogonCommands>
   <SynchronousCommand wcm:action="add">
      <CommandLine>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -Command "& {New-SmbMapping -LocalPath z: -RemotePath \\10.10.10.5\Share -UserName User -Password password -Persistent 0}"</CommandLine>
      <Description>Description_of_command1</Description>
      <Order>1</Order>
   </SynchronousCommand>
   <SynchronousCommand wcm:action="add">
      <CommandLine>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -File z:\script.ps1</CommandLine>
      <Description>Description_of_command2</Description>
      <Order>2</Order>
   </SynchronousCommand>
</FirstLogonCommands>

Please note that in above code snippet the -NoExit parameter is removed completely and the -WindowStyle Maximized is changed to -WindowStyle Hidden, according to your statement from our previous discussion: "I use the -NoExit parameter just to be sure if the command fails I can see what happened".