Powershell 远程连接ARM Windows Server VM 并关闭 Firewall

时间:2023-01-01 03:54:41

准备环境:Azure ARM Windows Server 2008 R2 机器

1.登陆机器查看防火墙,是开着的

Powershell 远程连接ARM Windows Server VM 并关闭 Firewall

2.查看机器的监听端口,发现没有Powershell远程管理对应的端口5985或5986  【5985(http) / 5986(https) 】

Powershell 远程连接ARM Windows Server VM 并关闭 Firewall

3.需要在Server机器上 Enable-PSRemoting

PS C:\Users\stone> Enable-PSRemoting
WinRM 快速配置
正在运行命令“Set-WSManQuickConfig”,以使该计算机能够通过 WinRM 服务进行远程管理。
其中包括:
1. 启动或重新启动(如果已启动) WinRM 服务
2. 将 WinRM 服务类型设置为自动启动
3. 创建一个侦听器以接受任意 IP 地址上的请求
4. 对 WS-Management 流量启用防火墙例外(仅适用于 http)。
是否继续?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 挂起(S) [?] 帮助 (默认值为“Y”):
在此计算机上,WinRM 已设置为接收请求。
WinRM 已经进行了更新,以用于远程管理。
在 HTTP://* 上创建 WinRM 侦听程序接受 WS-Man 对此机器上任意 IP 的请求。
WinRM 防火墙异常已启用。
已配置 LocalAccountTokenFilterPolicy 以远程向本地用户授予管理权限。
确认
是否确实要执行此操作?
对目标“未找到会话配置“Microsoft.PowerShell32”。正在运行命令“Register-PSSessionConfiguration Microsoft.PowerShell32
-processorarchitecture x86 -force”以创建“Microsoft.PowerShell32”会话配置。这将会重新启动 WinRM
服务。”执行操作“正在注册会话配置”。
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 挂起(S) [?] 帮助 (默认值为“Y”):

4.再次查看机器的监听端口,发现Powershell远程管理对应的端口5985已经被监听了

Powershell 远程连接ARM Windows Server VM 并关闭 Firewall

5.在Portal界面添加允许5985端口的NSG规则,并测试该端口的连通性

Powershell 远程连接ARM Windows Server VM 并关闭 Firewall

Powershell 远程连接ARM Windows Server VM 并关闭 Firewall

6.尝试Powershell远程登陆虚拟机,发现会出现如下报错:

PS C:\Users\he.liming> Enter-Pssession -ComputerName "40.*.*.*" -port 5985 -Credential VMAccountName
Enter-Pssession : Connecting to remote server 40.*.*.* failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set Trusted
Hosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ Enter-Pssession -ComputerName "40.*.*.*" -port 5985 -Credential stone
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (40.125.160.63:String) [Enter-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed

7.需要将Server端IP地址添加到Client机器的TrustedHosts列表下

PS C:\Windows\system32> Set-Item wsman:\localhost\Client\TrustedHosts -value 40.125.160.63
WinRM Security Configuration.
This command modifies the TrustedHosts list for the WinRM client. The computers in the TrustedHosts list might not be
authenticated. The client might send credential information to these computers. Are you sure that you want to modify
this list?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):

8.重新使用Powershell连接虚拟机,登陆成功

PS C:\Users\he.liming> Enter-Pssession -ComputerName "40.125.160.63" -port 5985 -Credential VMAccountName
 [40.125.160.63]: PS C:\Users\stone\Documents>

9.使用如下命令关闭防火墙

PS C:\Users\stone\Documents> netsh advfirewall set allprofiles state off

10.再次查看防火墙,已经关闭

Powershell 远程连接ARM Windows Server VM 并关闭 Firewall

备注:Windows Server 2012 R2的机器开启Powershell远程连接的方法,与上面的操作大同小异,需要注意的是Windows Server 2012 R2机器默认情况下5985的端口是监听着的

Powershell 远程连接ARM Windows Server VM 并关闭 Firewall