0

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?

9
  • 1
    From powershell -?: To write a string that runs a Windows PowerShell command, use the format: "& {<command>}" – JosefZ Aug 10 '19 at 12:15
  • I already tried that but I will try again. Give some time... – Luke Aug 10 '19 at 12:39
  • Please note that angle brackets < and > (or Less-Than Sign and Greater-Than Sign) here are metasymbols, not a part of resultant code. Hence, you can use something like the following: … -Command "& {Start-Sleep 30; Get-Childitem .}". – JosefZ Aug 10 '19 at 13:45
  • It appears that "& {Get-Childitem -Path C:\}" works fine, but "& {New-SmbMapping -LocalPath z: -RemotePath \\10.10.10.5\Share; z:\script.ps1}"doesn't even display a PowerShell console at all... – Luke Aug 10 '19 at 16:52
  • My bad for the former partial copy/paste because I though some part was irrelevant... "& {New-SmbMapping -LocalPath z: -RemotePath \\10.10.10.5\Share -UserName User -Password password}" worsk fine. However, "& {New-SmbMapping -LocalPath z: -RemotePath \\10.10.10.5\Share -Persistent $false -UserName User -Password password}" does not open any PowerShell window. Maybe the $ sign is the problem here... Trying some more dichotomy... – Luke Aug 10 '19 at 18:53
2

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.