Executing a scriptblock via startprocess

家住魔仙堡 提交于 2021-01-27 14:23:30

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/7318903/executing-a-scriptblock-via-startprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!