zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

方法执行 解决 报错 Command Invoke
2023-09-14 09:16:40 时间

脚本:

$Username = 'administrator'
$Password = 'xielong.cn'
$Pass = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Pass
# 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、 切到远程管理的client目录下

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

2、查看子目录,其中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

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

 一、什么是winRM服务

Windows远程管理(WinRM)服务是Windows Server 2003 R2以上版本中一种新式的方便远程管理的服务。通过WinRM服务,便于在日常工作中远程管理服务器,或通过脚本同时管理多台服务器,来提高工作效率。

我们在运维Windows服务器时,通常是用RDP协议来访问服务器,由于RDP是一种图形化协议,导致运维人员无法像SSH一样通过命令行终端的方式来对Windows服务器进行运维操作。RDP协议的这一缺点,使得Windows无法通过远程来执行一些自动化批量命令,例如传输文件、远程执行脚本等。

WinRM服务基于HTTP(HTTPS)协议,使用5985端口(HTTPS使用5986端口)。按照微软官方的介绍:Windows2008 R2以上版本的操作系统,默认开启WinRM服务,因此,在使用时只需将防火墙相应的5985(或5986)端口开放即可;

 (1)如何确认WinRM服务是否开放?

以管理员身份在CMD中执行以下命令:

winrm e winrm/config/listener

如下出现如下界面,就是没有开启 

WSManFault
    Message = 客户端无法连接到请求中指定的目标。 请验证该目标上的服务是否正在运行以及是否正在接受请求。 有关目标(通常是 IIS 或 WinRM)上运行的 WS 管理服务,请查阅日志和文档。 如果目标是 WinRM 服务,则在目标上运行以下命令来分析和配置 WinRM 服务: "winrm quickconfig"。

错误编号: -2144108526 0x80338012
客户端无法连接到请求中指定的目标。 请验证该目标上的服务是否正在运行以及是否正在接受请求。 有关目标(通常是 IIS 或 WinRM) 上运行的 WS 管理服务,请查阅日志和文档。 如果目标是 WinRM 服务,则在目标上运行以下命令来分析和配置 WinRM 服务: "winrm quickconfig"。

 

(2)如何开启WinRM服务?

若WinRM未启用,以管理员权限运行PowerShell,然后输入以下命令:

enable-psremoting