1

我已经成功地让 winrm 工作,我能够Enter-PSSession my-machine在 shell 中运行并随后输入命令。但是,当我尝试运行启动远程会话的脚本时,所有后续调用都在本地计算机上运行。例如:

PS> test.ps1

test.ps1 的内容

Enter-PSSession remote-pcname
gc env:computername

打印出来local-pcname而不是remote-pcname知道为什么脚本文件不尊重远程会话?它肯定是成功连接,因为当脚本完成时,我返回到远程机器的 shell 提示符。

4

2 回答 2

6

简短的回答是:Enter-PSSession 旨在用于交互式使用。如果要通过脚本在远程系统上执行命令,请使用 invoke-command。

Technet 论坛上有一个类似的帖子:http: //social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/90e92d4e-716b-4b4d-956f-d38645e5c035

于 2011-05-25T03:33:03.890 回答
4

对我来说,它按记录工作。Enter-PSSession启动一个交互式会话,它是用于交互式使用的。

因此,要执行脚本,您可以使用New-PSSession您使用 New-PSSession 创建的远程会话来创建会话和 Invoke-Command。

于 2011-05-25T03:36:41.717 回答