Invoke-Command 执行报错:解决方法

远程执行服务器命令,脚本如下:

$Username = 'administrator'
$Password = 'xielong.cn'
$SecurePwd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$SecurePwd 
# Start-Process powershell.exe -Credential $Credential
$Server = "10.3.0.227"
Invoke-Command -Credential $Credential -FilePath d:\ipconfig.ps1 -ComputerName $Server

报错如下:

[10.3.0.227] 连接到远程服务器 10.3.0.227 失败,并显示以下错误消息: WinRM 客户端无法处理该请求。如果身份验证方案与 Kerberos 不同,或者客户端计算机未加入到域中, 则必须使用 HTTPS 传输或者必须将目标计算机添加到 TrustedHosts 配置设置。 使用 winrm.cmd 配置 TrustedHosts。请注意,TrustedHosts 列表中的计算机可能未经过身份验证。 通过运行以下命令可获得有关此内容的更多信息: winrm help config。 有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。
    + CategoryInfo          : OpenError: (10.3.0.227:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : ServerNotTrusted,PSSessionStateBroke

原因:

出现该问题是由于本机没有将要远程登录主机的IP:10.3.0.227加入到本机的可信主机列表中

解决方法: 

1、开启 winRM 服务

# 以管理员方式运行
enable-psremoting

2、 切到client目录下

PS C:\Users\Administrator> cd WSMan::localhost\Client

3、查看子目录,其中TrustedHosts会列出本机已添加的可信主机IP

# 查看
PS Microsoft.WSMan.Management\WSMan::localhost\Client> Get-ChildItem

# 显示
   WSManConfig:Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value                                                                                                                                                                                                                                                  
----            ----                           -------------   -----                                                                                                                                                                                                                                                  
System.String   NetworkDelayms                                 5000                                                                                                                                                                                                                                                   
System.String   URLPrefix                                      wsman                                                                                                                                                                                                                                                  
System.String   AllowUnencrypted                               false                                                                                                                                                                                                                                                  
Container       Auth                                                                                                                                                                                                                                                                                                  
Container       DefaultPorts                                                                                                                                                                                                                                                                                          
System.String   TrustedHosts

3、把IP加入可信主机

PS Microsoft.WSMan.Management\WSMan::localhost\Client> Set-Item TrustedHosts 10.3.0.227

# 输入 Y 回车
WinRM 安全配置。
此命令修改 WinRM 客户端的 TrustedHosts 列表。TrustedHosts
列表中的计算机可能不会经过身份验证。该客户端可能会向这些计算机发送凭据信息。是否确实要修改此列表?
[Y] 是(Y)  [N] 否(N)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): Y

4、再次查看子目录,成功加入


PS Microsoft.WSMan.Management\WSMan::localhost\Client> Get-ChildItem


   WSManConfig:Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value                                                                                                                                                                                                                                                  
----            ----                           -------------   -----                                                                                                                                                                                                                                                  
System.String   NetworkDelayms                                 5000                                                                                                                                                                                                                                                   
System.String   URLPrefix                                      wsman                                                                                                                                                                                                                                                  
System.String   AllowUnencrypted                               false                                                                                                                                                                                                                                                  
Container       Auth                                                                                                                                                                                                                                                                                                  
Container       DefaultPorts                                                                                                                                                                                                                                                                                          
System.String   TrustedHosts                                   10.3.0.227

5、也可以一条命令搞定,这里信任所有IP

Set-Item WSMan:\localhost\Client\TrustedHosts -Value * -Force
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值