我正在尝试通过powershell在远程服务器上运行批处理脚本。非常直截了当:
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("domain\user", $password)
$sesh= new-pssession -computername "MSSCA" -credential $cred
invoke-command -session $sesh -scriptblock {
cmd.exe /C "C:\install.bat"
}
Remove-PSSession $sesh这似乎是随机失败的,错误如下

在远程机器上,运行powershell命令winrm quickconfig来配置远程管理服务,这告诉我它已经设置为接收请求/远程管理。
只有在运行此命令后,invoke-command才能正常运行。多么?我甚至没有配置任何东西。我该如何解决这个问题呢?
发布于 2013-08-14 23:32:11
查看winrm的帮助(顺便说一句,这是一个Windows exe,不是Powershell命令):
> winrm help quickconfig
Windows Remote Management Command Line Tool
winrm quickconfig [-quiet] [-transport:VALUE] [-force]
Performs configuration actions to enable this machine for remote management.
Includes:
1. Start the WinRM service
2. Set the WinRM service type to auto start
3. Create a listener to accept request on any IP address
4. Enable firewall exception for WS-Management traffic (for http only)也许您的远程计算机启用了这4个步骤中的某个子集,但在您运行该实用程序之前,并不是所有步骤都同时启用。特别是,侦听器配置在过去给我带来了麻烦。您可以在使用以下命令之前/之后检查侦听器配置(以管理员身份在远程计算机上运行):
dir wsman:\localhost\listener您还可以尝试运行Enable-PSRemoting (必须以管理员身份运行),这将包含额外的日志记录。
发布于 2013-08-15 00:11:03
我以前遇到过这种情况,结果是脚本执行策略。在受影响的主机上使用Set-ExecutionPolicy cmdlet。出于测试目的,请使用以下命令参数:
Set-ExecutionPolicy Unrestricted我强烈建议您不要将该设置保留为无限制,但它对于确定可能的原因很有用。
供参考:
https://stackoverflow.com/questions/18235513
复制相似问题