我想通过使用这个命令从CMD启动PowerShell。如何在PowerShell会话中访问powershell.exe命令的结果?
powershell.exe -NoProfile -NoExit -command "& {$someVar = 'test'}"
如何访问变量someVar在PowerShell中。 MSDN说:“脚本的结果作为反序列化的XML对象返回到父shell,而不是活动对象。” 。
我想通过使用这个命令从CMD启动PowerShell。如何在PowerShell会话中访问powershell.exe命令的结果?
powershell.exe -NoProfile -NoExit -command "& {$someVar = 'test'}"
如何访问变量someVar在PowerShell中。 MSDN说:“脚本的结果作为反序列化的XML对象返回到父shell,而不是活动对象。” 。
& {$someVar = 'test'}"
在本地范围内执行脚本块,该脚本块在执行脚本块时被删除。
您需要创建在$global
-scope变量(会话范围)
CMD> powershell.exe -NoProfile -NoExit -command "& {$global:someVar = 'test'}"
PS> $someVar
test
,或者使用点采购. <script/scriptblock>
到excecute的脚本块,它运行在当前范围内的一切(会话在这种情况下, )
CMD> powershell.exe -NoProfile -NoExit -command ". {$someVar = 'test'}"
PS> $someVar
test
了解更多关于变量的作用域here
谢谢Frode F 。我对PowerShell非常陌生,所以我不知道这些命令在本地范围内运行。我现在要去看范围。再次感谢。 – Rakesh
“我试图从CMD启动PowerShell的” - 为什么? –
你的代码的其余部分在做什么?一行代码没有任何进一步的上下文对我们没有多大帮助。 –
Hello Bill,James:我们有一个应用程序可以对身份管理系统进行REST调用,并获取一些值(例如:基于运行应用程序的用户的资格,某些应用程序的用户名,密码)。我们正试图让用户,特别是像管理员这样的人可以点击一个按钮,启动一个PowerShell会话,其中包含所有已有的变量。 – Rakesh