将计算机添加到TrustedHosts后,远程命令失败

时间:2017-05-31 09:52:29

标签: powershell powershell-remoting

我将本地和远程计算机都添加到TrustedHosts:

winrm set winrm/config/client '@{TrustedHosts="WIN-XXXXX,Windows7-1"}'

执行远程命令后现在失败

Invoke-Command -ComputerName "Windows7-1" -Credential $cred -ScriptBlock {Rename-Computer -ComputerName 'Windows7-1' -NewName 'XXXX' -Force -PassThru -Restart }

如果我等待2分钟并运行相同的代码,它将正常工作......

1 个答案:

答案 0 :(得分:0)

$hostName="DomainName" 
$winrmPort = "5986"
$username = "Username"
$secpasswd = ConvertTo-SecureString “Password” -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (“$username”, $secpasswd)
$soptions = New-PSSessionOption -SkipCACheck
Invoke-Command -ComputerName $hostName -ScriptBlock { Command Session } -Port $winrmPort -Credential $mycreds -SessionOption $soptions -UseSSL