Executing a scriptblock via startprocess
19,656
Here is somthing that is working:
PS C:\> Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `$outvar1`"}`"" -Wait
-ArgumentList is an array of strings $outvar is interpreted so I use `$outvar
Author by
Admin
Updated on June 27, 2022Comments
-
Admin over 1 year
Would any of you possibly know why this is not working?
Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit -Command & `"{$outvar1 = 4+4; `"out: $outvar1`"}`"" -Wait
The ultimate purpose for this is so that i can run a script block as another user with the addition of the -Credential option. But i can not get this simple script block to work yet. Many thanks. Chris.
-
Admin about 12 yearsGreat, and then to add some text before we just escape the special chars, now i understand. cheers Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command
"&{
$outvar1 = 4+4;"write-output
"Hello:"
$outvar1"}
"" -Wait -
Keith Hill about 12 yearsFYI, I prefer to use
$p = Start-Process ... -PassThru; Wait-Process $p.id -Timeout xxx
. Just in case the started process hangs, my script won't hang any longer than the specified timeout.