軟體工程師的小事:到底要不要 Close

使用 Powershell 遠端連線至 Windows Server 2016

本文章使用 Windows 10 電腦透過 Powershell 連線進入相同網段下的 Windows Server 2016 伺服器。

環境資訊
作業系統 IP位址 備註
Windows 10 192.168.0.x Client
Windows Server 2016 192.168.0.85 Server

Windows 10 電腦使用的 Powershell 版本資訊

使用 powershell 進行遠端連線

進行遠端登入前要準備 PSCredential 憑證。

$sec_password = ConvertTo-SecureString {登入密碼} -AsPlainText -Force;
$credential = New-Object System.Management.Automation.PSCredential({登入帳號},$sec_password
);

使用 Enter-PSSession 與 PSCredential 憑證進行登入。

Enter-PSSession -Computer 192.168.0.85 -Credential $credential

預設情況下登入出現錯誤訊息。

將遠端電腦加入受信任清單

透過 powershell 指令將遠端電腦加入到受信任的清單。

Set-Item wsman:\localhost\client\trustedhosts "192.168.0.85"
因為會變更到設定所以需要再次確認

可以使用 Get-Item 檢視目前受信任清單。

Get-Item wsman:\localhost\client\trustedhosts

再次使用 Enter-PSSession 指令連線到伺服器

Enter-PSSession -Computer 192.168.0.85 -Credential $credential

成功使用 powershell 進入遠端電腦!

若要指定多個 IP 位址或電腦名稱時,可使用逗號分隔:

Set-Item wsman:\localhost\client\trustedhosts "192.168.0.85,192.168.0.86"

也可以使用萬用字元:

Set-Item wsman:\localhost\client\trustedhosts "192.168.0.*"
參考資料

留言