Windows sandbox and Powershell : how to make script executable at startup

1.3k Views Asked by At

I followed tutorial to do so :

in wsb:

<LogonCommand>
  <Command>powershell -executionpolicy unrestricted -command "start powershell {-noexit -file C:\scripts\start.ps1}"</Command>
</LogonCommand>

in start.ps1 :

Set-ExecutionPolicy Unrestricted -Force
Set-ExecutionPolicy -scope CurrentUser Unrestricted

but when I try to run a script in sandbox, it still complains script is not executable.

2

There are 2 best solutions below

1
Michel Terrisse On

I have the same problem with Unrestricted, but everything works fine with RemoteSigned. If your script is blocked because it was downloaded from the internet, you can use the cmdlet Unblock-File

0
Dennis On

You don't need to nest the commands that deep. And since it's a set and forget command, you don't need the console to stay open (except for debugging purposes).

I.e. this would start notepad twice from within PowerShell and the exit

powershell -command "notepad.exe; notepad.exe"

So setting the Executionpolicy for CurrentUser only would be

<LogonCommand>
  <Command />powershell -command "Set-ExecutionPolicy -Scope CurrentUser RemoteSigned"
</LogonCommand>