通過winrm使用powershell遠程管理服務器

原文地址:https://www.cnblogs.com/weloveshare/p/5753168.html?utm_source=itdadao&utm_medium=referral

 

在Linux中,我們可以使用安全的SSH方便的進行遠程管理。但在Windows下,除了不安全的Telnet以外,從Windows Server 2008開始提供了另外一種命令行原創管理方式,那就是基於PowerShell的WinRM。
另外一篇關於powershell的英文資料
再推薦一片博客
下面就介紹下如何使用WinRM,客戶端和服務端必須爲同一域:

1、在Windows Server 2008上配置WinRM:

C:Windowssystem32> winrm quickconfig
在此計算機上,WinRM 已設置爲接收請求。
WinRM 沒有設置成爲了管理此計算機而允許對其進行遠程訪問。
必須進行以下更改:
在 HTTP://* 上創建 WinRM 偵聽程序接受 WS-Man 對此機器上任意 IP 的請求。
進行這些更改嗎[y/n]? y
WinRM 已經進行了更新,以用於遠程管理。在 HTTP://* 上創建 WinRM 偵聽程序接受 WS-Man 對此機器上任意 IP 的請求。

 
PS C:\Users\Administrator> Enable-PSRemoting -Force
在此計算機上設置了 WinRM 以接收請求。
在此計算機上設置了 WinRM 以進行遠程管理。

2、開啓防火牆命令或者直接關閉防火牆:

 C:Windowssystem32>netsh advfirewall firewall set rule group="Windows 遠程管理" new enable=yes

3、使用PowerShell連接遠程服務器:

PS C:UsersWW-PC>Enter-PSSession -computer 服務器名或者IP
Enter-PSSession : 連接到遠程服務器失敗,錯誤消息如下: WinRM 客戶端無法處理該請求。如果身份驗證方案與 Kerberos 不同,或 
者客戶端計算機未加入到域中, 則必須使用 HTTPS 傳輸或者必須將目標計算機添加到 TrustedHosts 配置設置。 使用 winrm.cmd 配 
置 TrustedHosts。請注意,TrustedHosts 列表中的計算機可能未經過身份驗證。 通過運行以下命令可獲得有關此內容的更多信息: wi 
nrm help config。 有關詳細信息,請參閱 about_Remote_Troubleshooting 幫助主題。 
所在位置 行:1 字符: 16 
+ Enter-PSSession <<<<? 192.168.3.1 -Credential abcadministrator 
     + CategoryInfo            : InvalidArgument: (192.168.3.1:String) [Enter-PSSession], PSRemotingTransportException 
     + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

上面錯誤的解決方法

網上一般都是說要添加一個TrustedHosts表,相當於一個信任列表。
執行如下命令,將IP爲192.168.3.*的主機都加入信任列表

Set-Item wsman:localhostClientTrustedHosts -value 192.168.3.*

注意這個命令需要在 客戶端上執行 不是在服務端執行 且客戶端需要已管理員權限執行,這一點許多教程沒有說,走了不少彎路。
之後再用 Enter-PSSession 192.168.3.1 -Credential administrator 命令就可以完成連接了,沒有域的話就直接寫用戶名。

 

 

 

或者本地執行powshell命令:

$Username = 'xx'
$Password = 'xx'
$ComputerName='xx'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName $ComputerName -ScriptBlock { Restart-Computer -Force
 } -credential $Cred  
 

保存未 .ps1 ,在powshell中可以執行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章