Powershell 遠程連接ARM Windows Server VM 並關閉 Firewall


 

准備環境:Azure ARM Windows Server 2008 R2 機器

 

1.登陸機器查看防火牆,是開着的

 

2.查看機器的監聽端口,發現沒有Powershell遠程管理對應的端口5985或5986  【5985(http) / 5986(https) 】

 

 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已經被監聽了

 

 

5.在Portal界面添加允許5985端口的NSG規則,並測試該端口的連通性

 

 

 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.再次查看防火牆,已經關閉

 

備注:Windows Server 2012 R2的機器開啟Powershell遠程連接的方法,與上面的操作大同小異,需要注意的是Windows Server 2012 R2機器默認情況下5985的端口是監聽着的

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM