Windows 遠端管理 (WinRM) 是 WS-Management 協定的 Microsoft 實作。該協定是基于簡單對象通路協定 (SOAP) 的、防火牆友好的标準協定,使來自不同供應商的硬體和作業系統能夠互操作。WS-Management 協定由硬體和軟體制造商群體開發,作為一種公共标準,可用于與實作該協定的任何計算機裝置遠端交換管理資料。
使用PowerShell對伺服器遠端管理,要在遠端伺服器上啟用WinRM。
一、知識儲備:
1、WinRM相關操作
開啟WinRM服務:
Enable-PSRemoting –Force
阻止本地計算機接收遠端指令(不會停止WinRM服務):
Disable-psremoting –Force
檢視WinRM服務監聽資訊:
winrm enumerate winrm/config/Listener
WinRM2.0預設端口5985(HTTP端口)或5986(HTTPS端口)。
删除WinRM HTTP偵聽:
winrm delete winrm/config/listener?Address=*+Transport=HTTP
重建立立HTTP偵聽:
winrm create winrm/config/listener?Address=*+Transport=HTTP
WinRM服務更改監聽端口:
set-item -force wsman:\localhost\listener\listener*\port 5985
檢視WinRM的配置:
winrm get winrm/config
檢視端口監聽狀态:
netstat -nao | findstr "5985"
<a href="https://s1.51cto.com/wyfs02/M02/96/19/wKioL1kdBBzCxE20AAGJt42HbX0069.png-wh_500x0-wm_3-wmp_4-s_2958921877.png" target="_blank"></a>
2、PowerShell腳本儲存憑據方法
使用Get-Credential指令來互動式輸入憑據(使用者名+密碼),可以先将憑據儲存到一個變量中,如:
$cred = get-credential
<a href="https://s5.51cto.com/wyfs02/M01/96/19/wKioL1kdBCjSkN7xAABGBTheQDA354.png-wh_500x0-wm_3-wmp_4-s_439647423.png" target="_blank"></a>
$cred的對象類型名稱為:System.Management.Automation.PSCredential,其中的Password和UserName都是其屬性,是以可以建立一個該類型的對象。在此處需要注意的是Password類型為SecureString,UserName類型為String,是以需要将明文的Password轉換為安全字元串,可以使用ConvertTo-SecureString指令。
非互動式儲存憑據代碼示例:
<code>$account</code> <code>= </code><code>"administrator"</code>
<code>$password</code> <code>= </code><code>'123456'</code>
<code>$secpwd</code> <code>= </code><code>convertto-securestring</code> <code>$password</code> <code>-asplaintext -force </code>
<code>$cred</code> <code>= </code><code>new-object</code> <code>System.Management.Automation.PSCredential -argumentlist </code><code>$account</code><code>,</code><code>$secpwd</code>
3、Invoke-Command
Invoke-Command在本地和遠端計算機上運作指令,并從指令傳回所有輸出,包括錯誤。使用一個Invoke-Command指令,可以在多台計算機上運作指令。部分參數介紹:
-Port <int>
指定遠端計算機上用于此指令的網絡端口。用于WinRM預設監聽端口被更改的情況下,指定新的端口。
-ComputerName <string[]>
指定運作此指令的計算機。預設值為本地計算機。
使用 ComputerName 參數時,Windows PowerShell 會建立一個臨時連接配接,此連接配接僅用于運作指定的指令,之後将關閉。如果需要持續性連接配接,請使用 Session 參數。
在一個逗号分隔清單中鍵入一台或多台計算機的 NETBIOS 名稱、IP 位址或完全限定的域名。要指定本地計算機,請鍵入計算機名稱、“localhost”或句點 (.)。
若要在 ComputerName 參數的值中使用 IP 位址,指令必須包括 Credential 參數。另外,必須為計算機配置 HTTPS 傳輸,或者必須在本地計算機上的 WinRM TrustedHosts 清單中包含遠端計算機的 IP 位址。
-Session <PSSession[]>
在指定的 Windows PowerShell 會話 (PSSession) 中運作此指令。
通過建立 PSSession,Windows PowerShell 可以建立與遠端計算機的持續性連接配接。
-Credential <PSCredential>
指定有權執行此操作的使用者帳戶。預設值為目前使用者。
-command/-ScriptBlock <scriptblock>
指定要運作的指令。用大括号 ({ }) 括起指令以形成腳本塊。
-FilePath <string>
在一台或多台遠端計算機上運作指定的本地腳本。
-AsJob
在遠端計算機上将指令作為背景作業運作。使用此參數可運作需要較長時間才能完成的指令。
使用 AsJob 時,此指令傳回代表作業的對象,然後顯示指令提示符。要管理作業,請使用 Job cmdlet。要擷取作業結果,請使用 Receive-Job。
-ThrottleLimit <int>
指定為運作此指令可建立的并發連接配接的最大數目。如果省略此參數或輸入 0 值,則使用預設值 32。
4、PSSession
PSSession即Windows PowerShell會話。當你需要與遠端計算機的持續連接配接時,則使用PSSession。PSSession相關指令:
Enter-PSSession
啟動與遠端計算機間的互動式會話。
New-PSSession
建立PSSession,并傳回一個表示PSSession的對象。你可以将對象儲存在變量中。
Get-PSSession
擷取目前會話中建立的PSSession。Get-PSSession傳回與New-PSSession傳回的對象相同類型的對象。
Remove-PSSession
删除 PSSession 并釋放其正在使用的資源。
5、将計算機名稱添加到TrustedHosts清單
若要将所有計算機添加到受信任主機清單,請使用以下指令 :
set-item wsman:localhost\client\trustedhosts -Force -value *
還可以使用通配符 (*) 将特定域中的所有計算機添加到受信任主機清單。
例如,以下指令将Fabrikam域中的所有計算機添加到受信任主機清單。
set-item wsman:localhost\client\trustedhosts *.fabrikam.com
若要将特定計算機的 IP 位址添加到受信任主機清單,請使用以下指令格式(IP支援通配符*):
set-item wsman:\localhost\Client\TrustedHosts -value "<IP Address>[,<IP Address>]"
若要檢視受信任主機清單,請使用以下指令:
get-item wsman:\localhost\Client\TrustedHosts
<a href="https://s4.51cto.com/wyfs02/M00/96/19/wKioL1kdBDbBLJikAABZOEO_R5Q583.png-wh_500x0-wm_3-wmp_4-s_3331756484.png" target="_blank"></a>
二、遠端操作常見場景
場景一:遠端互動式會話
這個場景一般用于手動進行遠端操作,輸入指令,檢視結果。方法很簡單。進入互動式會話的指令是Enter-PSSession,退出時鍵入Exit-PSSession或者exit都可以。遠端互動式操作期間,輸入的指令在遠端計算機上運作,就像直接在遠端計算機上輸入并執行這些指令一樣。期間所定義的變量和指令的執行結果在退出互動式會話之後不再可用。
<code>場景一例子:</code>
<code># 使用者輸入憑據(使用者名+密碼)</code>
<code>$cred</code><code>=</code><code>get-Credential</code>
<code># 建立遠端互動式會話</code>
<code>Enter-PSSession</code> <code>-computername 192.168.21.1 -Credential </code><code>$cred</code>
<a href="https://s5.51cto.com/wyfs02/M00/96/19/wKiom1kdBF-zRbLbAAApoTzX7cE469.png-wh_500x0-wm_3-wmp_4-s_654629191.png" target="_blank"></a>
場景二:腳本塊、腳本檔案的一次性執行
這種場景,是在本地計算機與遠端計算機上建立一個臨時會話。将腳本塊或者腳本檔案的内容發送到遠端計算機執行,并将結果發回本地計算機。這種方法執行效率很高,是PowerShell推薦的執行遠端指令的方法。除非需要在會話中共享資料,否則建議使用該方法。
<code>場景二例子、</code>
<code># 遠端執行指令</code>
<code>invoke-command</code> <code>-computername 192.168.21.1 -Credential </code><code>$cred</code> <code>-command {dir C:/}</code>
<code>invoke-command</code> <code>-computername 192.168.21.1 -Credential </code><code>$cred</code> <code>-ScriptBlock {dir c:\}</code>
<code># 遠端執行腳本</code>
<code>echo </code><code>"dir c:\"</code> <code>> dirDriveC.ps1</code>
<code>invoke-command</code> <code>-computername 192.168.21.1 -Credential </code><code>$cred</code> <code>-FilePath .\dirDriveC.ps1</code>
<a href="https://s5.51cto.com/wyfs02/M02/96/19/wKioL1kdBEOgG1zGAAAlVesM1jw732.png-wh_500x0-wm_3-wmp_4-s_400029066.png" target="_blank"></a>
<code>場景二例子(多個遠端主機)、</code>
<code># 對多個遠端主機批量執行指令</code>
<code>invoke-command</code> <code>-computername 192.168.21.1,192.168.21.4,192.168.21.7 -Credential </code><code>$cred</code> <code>-ScriptBlock {dir c:\}</code>
<code># 設定并發連接配接數設定為1的情況</code>
<code>invoke-command</code> <code>-computername 192.168.21.1,192.168.21.4,192.168.21.7 -Credential </code><code>$cred</code> <code>-ThrottleLimit 1 -ScriptBlock {dir c:\}</code>
場景三:腳本塊、腳本檔案在命名會話中執行
1、定義會話: 請使用new-pssession指令定義會話,如$session1 = new-pssession -computername server1。(如果必要請使用Credential參數。)
2、在會話中遠端執行腳本(或者腳本檔案): 請使用Invoke-Command指令執行遠端腳本,如Invoke-Command -Session $session1 -ScriptBlock {dir c:\}或者Invoke-Command -Session $session1 -FilePath .\dirDriveC.ps1
3、獲得結果: 可以将執行結果賦于變量,如$sub = Invoke-Command -Session $session1 -ScriptBlock {dir c:\}或者$sub = Invoke-Command -Session $session1 -FilePath .\dirDriveC.ps1
後續指令可參照步驟2或者3繼續執行,所有執行的指令就好像在同一個上下文中執行一樣。
<code>場景三例子、</code>
<code># 建立PSSession,并指派給變量</code>
<code>$session1</code> <code>= </code><code>new-pssession</code> <code>-computername 192.168.21.1 -Credential </code><code>$cred</code>
<code># 遠端執行指令(持續性)</code>
<code>Invoke-Command</code> <code>-Session </code><code>$session1</code> <code>-ScriptBlock {</code><code>$a</code><code>=</code><code>"hello world"</code><code>}</code>
<code>Invoke-Command</code> <code>-Session </code><code>$session1</code> <code>-ScriptBlock {</code><code>$a</code><code>}</code>
<a href="https://s3.51cto.com/wyfs02/M00/96/19/wKioL1kdBGzz--vqAAB_7WeKz-4673.png-wh_500x0-wm_3-wmp_4-s_261082102.png" target="_blank"></a>
<code>場景三例子(多個遠端主機)、</code>
<code># 對多個遠端主機建立PSSession,并指派給變量</code>
<code>$session_many</code> <code>= </code><code>new-pssession</code> <code>-computername 192.168.21.1,192.168.21.4,192.168.21.7 -Credential </code><code>$cred</code>
<code>Invoke-Command</code> <code>-Session </code><code>$session_many</code> <code>-ThrottleLimit 1 -ScriptBlock {dir c:\}</code>
<code>場景三例子(腳本方式)、</code>
<code>$account</code> <code>= </code><code>"administrator"</code>
<code>$password</code> <code>= </code><code>'123456'</code>
<code># 把密碼轉換為SecureString</code>
<code>$secpwd</code> <code>= </code><code>convertto-securestring</code> <code>$password</code> <code>-asplaintext -force</code>
<code># 建立PSCredential對象</code>
<code># 遠端主機清單指派給變量</code>
<code>[string[]]</code><code>$computername</code><code>=</code><code>"192.168.21.1"</code><code>,</code><code>"192.168.21.4"</code><code>,</code><code>"192.168.21.7"</code>
<code># 建立PSSession</code>
<code>$session_many</code> <code>= </code><code>new-pssession</code> <code>-computername </code><code>$computername</code> <code>-Credential </code><code>$cred</code>
<code># 批量遠端執行指令</code>
參考資料:
https://technet.microsoft.com/zh-cn/library/dd347578.aspx
https://technet.microsoft.com/zh-cn/library/hh847839.aspx
https://technet.microsoft.com/zh-cn/library/dd347642.aspx
http://www.cnblogs.com/ceachy/archive/2013/02/20/PowerShell_Remoting.html
本文轉自 張斌_青島 51CTO部落格,原文連結:http://blog.51cto.com/qicheng0211/1926913