PowerShell远程连接问题

标签 powershell powershell-remoting winrm

我正在尝试实现 PowerShell 远程处理(用于在远程服务器上执行 PowerShell 脚本)。 我的远程服务器正在运行 Windows Server 2008 和 PowerShell v2。

从提升权限的 PS 控制台,我执行了以下 cmdlet:

Enable-PSRemoting

返回控制台:

WinRM already is set up to receive requests on this machine.

WinRM already is set up for remote management on this machine.

从客户端计算机上提升的 PowerShell session 中,我执行了以下 cmdlet:

Enter-PSSession –ComputerName [remote_server_name]

这一次,返回到控制台:

Enter-PSSession : Connecting to remote server [remote_server_name] failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: “winrm quickconfig”. For more information, see the about_Remote_Troubleshooting Help topic.

At line:1 char:1

+ Enter_PSSession –ComputerName [remote_server_name]

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : Invalid/argument: ([remote_server_name]:string) [Enter-PSSession], PSRemotingTransportException

+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed

我按照错误消息建议执行了“winrm Quickconfig”。再次返回控制台:

WinRM already is set up to receive requests on this machine.

WinRM already is set up for remote management on this machine.

重试

Enter-PSSession –ComputerName [remote_server_name]

从客户端计算机返回相同的错误。

我仔细检查了 WinRM 服务是否正在远程服务器上运行。 Windows 远程管理 (HTTP-In) 防火墙规则配置为 TCP 协议(protocol)、本地端口 5985 和所有远程端口。

即使我执行

Get-Service WinRM –ComputerName [remote_server_name]

从客户端计算机,我们看到返回到控制台,确认 WinRM 服务确实在远程服务器上运行:

Status Name DisplayName

------ ---- -----------

Running WinRM Windows Remote Management (WS-Manag…

在远程机器上执行以下命令

WinRM Enumerate WinRM/Config/Listener

返回控制台,证明正确的端口已打开:

Listener [Source=”GPO”]

Address = *

Port = 5985

Hostname Enabled = true

URLPrefix = wsman

CertificateThumbprint ListeningOn = null

但是,执行

$remoteServer = [test_server_name]
$port = 5985
$connection = New-Object System.Net.Sockets.TcpClient($remoteServer, $port)
if ($connection.Connected) {
    Write-Host "Success"
}
else {
    Write-Host "Failed"
}

返回以下错误消息:

New-Object : Exception calling ".ctor" with "2" argument(s): "No connection could be made because the target machine actively refused it [remote_server_ipaddress]:5985"

At line:4 char:15

+ ... onnection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException

+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

我有另一台远程服务器,运行带有 PowerShell v4 的 Windows Server 2012 R2。我已在此机器上启用 PS 远程处理,没有出现任何问题,并且可以从同一客户端远程执行 PS 脚本。

那么,我错过了什么? PowerShell版本之间是否有可能存在兼容性问题?

我的客户端 PC 正在运行 PowerShell 5,我的正常运行的远程服务器正在运行 PowerShell 4,我的故障远程服务器正在运行 PowerShell 2。

非常欢迎见解、指导、建议。感谢您的浏览。

更新:

此后,我已将测试虚拟机上的 .Net Framework 更新为 .Net 4.7.2,并在受影响的虚拟机上安装了 PowerShell 4(与正常运行的机器相同)。

在比较我的工作虚拟机和受影响的计算机之间的 WinRM 相关注册表项时,我找到了 key :

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Service

在工作虚拟机上,我发现了一个 32 位“allow-remote-requests”,其值为 1。受影响计算机的注册表中缺少此条目。我添加了它,重新启动了机器,重试了连接请求。

同样的问题仍然存在。

最佳答案

您的主机似乎一直拒绝客户端计算机。

尝试通过执行以下操作来获取主机的凭据:

    $c = Get-Credential        
    New-CimSession -ComputerName [name] -Credential $c        

然后,输入您的主机用户名和密码。

如果仍然不起作用,请尝试手动将您的客户端添加为主机上的授权用户。

关于PowerShell远程连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52103068/

相关文章:

服务器还原后 Powershell 远程处理失败

ssl - WinRm - 由于 SSL 证书不正确,无法在 HTTPS 上创建 WinRM 监听器

powershell - Psake 和 robocopy 失败

sql-server - 使用Powershell获取SQL备份数据

powershell - 使用Powershell脚本将新文件夹文件重命名为下一个增量编号

powershell - 如何使 PowerShell V2 远程处理与 Windows Server 2003 一起工作?

powershell - 如何使用 PowerShell 进行远程注册表更改?

windows - WinRM 连接问题?

powershell - New-PSSession 在本地不起作用

Powershell CSV 变量到 Out-GridView