为什么我的PowerShell脚本在通过批处理(.cmd)文件在PSEXEC中调用时挂起?

我想使用PSEXEC远程执行PowerShell脚本。 PowerShell脚本通过.cmdbatch file调用。 我们这样做的原因是更改执行策略,运行powershell脚本,然后重新执行策略:

在远程服务器上do-tasks.cmd看起来像:

 powershell -command "&{ set-executionpolicy unrestricted}" powershell DoTasks.ps1 powershell -command "&{ set-executionpolicy restricted}" 

PowerShell脚本DoTasks.ps1只是现在这样做:

 Write-Output "Hello World!" 

这两个脚本都位于c:\windows\system32 (现在),所以它们在PATH上。

在原始服务器上,我这样做:

psexec \\web1928 -u administrator -p "adminpassword" do-tasks.cmd

当这个运行时,我在命令行得到以下响应:

c:\Windows\system32>powershell -command "&{ set-executionpolicy unrestricted}"

脚本不再运行。

我不能ctrl-c打破脚本,我只看到^ C字符,我可以键入来自键盘的input,并将字符回显到控制台。

在远程服务器上,我看到PowerShell.exe和CMD.exe正在任务pipe理器的进程选项卡中运行。 如果我结束这些进程,则控制权返回到始发服务器上的命令行。

我已经试过这只是一个简单的.cmdbatch file与@echo hello world ,它工作得很好。

通过RDP会话在远程服务器上运行do-tasks.cmd也可以。

原始服务器正在运行Windows 2003 SP2,远程服务器正在运行Windows 2008 SP2。

为什么我的远程batch file在通过PSEXEC执行时卡住了?

由于服务器运行2k8我怀疑UAC是造成问题。 我也build议使用winrm而不是psexec。 Powershell远程是PowerShell 2.0最好的新function之一

这是POSH的一个常见问题。 问题是stdin挂起。 尝试这个:

 c:\Windows\system32>powershell -command "&{ set-executionpolicy unrestricted}" < NUL 

在论坛的大多数答案解决这个问题,如回声和pipe道,使PowerShell从STDIN得到一些input。

但是在PowerShell中有一个解决scheme。 刚刚启动与选项-inputformat没有像下面的PowerShell:

 powershell -inputformat none -command ... 

这通过psexec解决了Win2003和Win2008的悬挂问题。