4

I'm trying to use Windows Sandbox with a PowerShell logon command. This is the LogonCommand section of my WSB file:

  <LogonCommand>
    <Command>C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -executionpolicy unrestricted -file "C:\\Users\\WDAGUtilityAccount\\Desktop\\boot.ps1" -noexit</Command>
  </LogonCommand>

The Windows Sandbox instance loads up okay suggesting no syntactic/validation issues with the WSB file content, but the PowerShell window is not shown. Adding -windowstyle normal has no effect.

I suspect the LogonCommand content is run in a command prompt which is not made visible so running the command to open PowerShell from it somehow "inherits" the terminal window not being visible.

Is it possible to force the PowerShell terminal window to reveal itself in such a case? I want to do this so that I can see the errors that I get because the PowerShell script is not executing as expected and I'm blind to any output/progress indication.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125

1 Answers1

10

Found an answer (doesn't look like the cleanest option, but works):

<Command>powershell -executionpolicy unrestricted -command "start powershell {-noexit -file C:\Users\WDAGUtilityAccount\Desktop\boot.ps1}"</Command>
  • powershell switches from CMD to PowerShell
    • -windowstyle normal won't work to make this PowerShell window visible
    • -executionpolicy unrestricted allows the nested PowerShell to run from file
  • start powershell runs another PowerShell with visible window
    • Running this directly for LogonCommand will not work
    • -noexit tells the nested PowerShell to remain visible
      • This is not necessary but it is useful for debugging the script errors
    • -file C:\Users\WDAGUtilityAccount\Desktop\boot.ps1 runs the given script
      • Share it with the machine by using a MappedFolder in the WSB configuration
Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
  • 3
    Thank you for sharing this. I was struggling too. – Hasan Hasanov Nov 23 '20 at 17:58
  • 1
    You can simplify `C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe` to `powershell.exe` as the rest is in the environment variable `%path%`. Thanks though, this was a pain to get working. – Alex Kwitny Nov 24 '21 at 18:21
  • @AlexKwitny surprised that it worked for you as it did not work for me - but it has been a while since I tried so I am glad to hear it works now. – Tomáš Hübelbauer Nov 24 '21 at 22:00
  • 1
    It has probably since been fixed and I believe you that it didn't work previously. The moment sandbox came out I was playing with it and it had all sorts of `.net` errors. They've improved it I think a good amount since then. – Alex Kwitny Nov 25 '21 at 00:12
  • @AlexKwitny can you confirm leaving out the `.exe` works? It should right? – Tomáš Hübelbauer Nov 25 '21 at 07:37
  • 1
    Confirmed, dropping the `.exe` works as well as the double "\\" can be "\". This worked just fine `powershell -executionpolicy unrestricted -command "start powershell {-noexit -file C:\Users\WDAGUtilityAccount\Desktop\ReadOnly\StartupScripts\MainScript.ps1}"` – Alex Kwitny Nov 28 '21 at 01:21
  • 1
    Can you explain the curly braces? While it looks like a syntax error, it actually works though. IMO they shouldn't be necessary so I suspect some strange command parsing things going on. – zett42 Aug 06 '22 at 16:57