屬于圖形化界面的朋友們 對于重新啟動計算機不會很陌生,但是如果要是使用powershell如何做到呢?
比起早期版本的PowerShell 2.0中重新啟動計算機重新啟動計算機,新的3.0版本powershell cmdlet将提供更好的靈活性和控制的管理。
PowerShell腳本,需要間歇性重新開機遠端計算機之間執行一個腳本來處理此cmdlet在新版本中有更好的控制。
- 在PowerShell 3.0,重新啟動計算機有15個參數,除常見的參數
<a target="_blank" href="http://blog.51cto.com/attachment/201306/105716110.png"></a>
- PowerShell 2.0中總共有9個參數重新啟動計算機指令(win7的是自帶powershell2.0哦)
<a target="_blank" href="http://blog.51cto.com/attachment/201306/105717242.png"></a>
1
2
3
4
5
6
7
8
9
<code>PS C:\Users\Administrator> </code><code>get-help</code> <code>Restart-Computer</code> <code>| Select</code> <code>-ExpandProperty</code> <code>parameters |</code>
<code>>> select</code> <code>-ExpandProperty</code> <code>parameter</code> <code>| </code><code>measure-object</code>
<code>>></code>
<code>Count : 9</code>
<code>Average :</code>
<code>Sum :</code>
<code>Maximum :</code>
<code>Minimum :</code>
<code>Property :</code>
CANTGIS已經做好了一個小的Excel工作表來比較重新啟動計算機新的和舊的參數指令
<a target="_blank" href="http://blog.51cto.com/attachment/201306/105717526.jpg"></a>
從cantgis這個小表我們可以看出PowerShell 3.0中有6個重新啟動計算機的身份驗證參數更名為DcomAuthentication
重新啟動計算機指令 cmdlet,允許我們作為背景作業運作重新啟動操作。
此cmdlet的Windows PowerShell 3.0中的輝煌的功能之一,我們可以等待重新開機完成後,再運作下一個指令,指定一個等待逾時和查詢間隔,等待特定的服務,可在重新啟動計算機。
這一特性使得它的實際使用重新啟動計算機需要重新啟動電腦之間執行的腳本。
我們也可以使用WSMAN協定,重新啟動計算機,DCOM調用被阻塞的情況下通過防火牆規則或企業政策。
PowerShell 2.0中,此功能不可用。現在,讓我們來談談一些很酷的功能,可用的PowerShell 3.0中引入的新的參數設定。
示例 重新啟動計算機
我們可以在腳本中使用此參數,重新啟動計算機,然後繼續處理完成後重新啟動。
等待參數預設情況下,會無限期地等待電腦重新啟動,但我們可以使用Timeout參數指定持續時間的等待和延遲參數等特定服務。
3.0新參數之-Wait
<code>Restart-Computer</code><code>-ComputerNameServer01</code> <code>-Wait</code>
此指令說明 我現在要重新啟動的是 遠端計算機Server01,并等待。
預設情況下,它會檢查WMI,WinRM的,和PowerShell連接配接移動到下一行腳本。
下面我貼出這個cantgis的遠端計算機示例圖他會預設進行WMI,WinRM的檢查,PowerShell連接配接,建立傳回我的PowerShell提示符
<a target="_blank" href="http://blog.51cto.com/attachment/201306/110812570.png"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/110812572.png"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/110812744.png"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/110813731.png"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/110813358.png"></a>
3.0新參數之-For
總結上面這個參數是有效的隻有等待參數。有效值為:
預設:等待重新啟動Windows PowerShell的計算機
PowerShell的:可以運作在電腦上的Windows PowerShell遠端會話指令。
WMI:接收一個的Win32_ComputerSystem查詢的計算機答複。
WinRM的:建立一個遠端會話的計算機使用WS-Management
現在新推出的powershell ISE在PowerShell 3.0具有智能感覺自動填充這些值
<a target="_blank" href="http://blog.51cto.com/attachment/201306/112527825.png"></a>
此指令重新啟動遠端計算機Server01和等待,直到WinRM服務是在遠端伺服器上運作起來。
<code>Restart-Computer</code><code>-ComputerNameServer01</code> <code>-Wait</code><code>-ForWinRM</code>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/110951265.png"></a>
指定的等待時間的持續時間,以秒為機關。當逾時過後,重新啟動計算機傳回指令提示符下,即使不重新啟動計算機。預設值為-1,表示無限期逾時。Timeout參數是有效的隻有等待參數。
我指定逾時時間為10秒,重新啟動電腦,我的電腦在10秒内沒有重新啟動,并花了更長的時間,我立刻傳回到PowerShell提示符:
<code>Restart-Computer</code><code>-ComputerNameServer01</code> <code>-Wait</code><code>-ForWinRM</code> <code>-Timeout10</code>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/111301888.png"></a>
這個參數往往決定所指定的參數,以确定它是否可以重新啟動計算機後Windows PowerShell的查詢服務。預設值是5(秒),這個參數是有效的隻有等待和參數。
下面的PowerShell示例我已經說明相同兩個截圖,相當于重新啟動過程的進度。我已經指定了一個6秒的延遲,是以延遲後每6秒PowerShell的查詢為WinRM連接配接到伺服器,直到它能夠驗證的連接配接已經成功建立。
<a target="_blank" href="http://blog.51cto.com/attachment/201306/111417531.png"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/111418666.png"></a>
指定要使用的協定重新啟動計算機。有效值為WSMAN和DCOM。預設值是DCOM。這些設定旨在為企業基于DCOM重新啟動失敗,因為被封鎖DCOM,如防火牆規則等。
<code>Restart-Computer</code><code>-ComputerNameServer01</code> <code>-ProtocolWSMan</code>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/111955346.png"></a>
此指令重新啟動遠端計算機Server01和使用WSMAN協定。
指定的機制,是用來驗證使用者的憑據當使用WSMAN協定。有效值基本CredSSP的,預設情況下,Digest,Kerberos身份,協商。預設值是預設。
<a target="_blank" href="http://blog.51cto.com/attachment/201306/112238194.png"></a>
<code>Restart-Computer</code><code>-ComputerNameServer01</code> <code>-WSManAuthenticationKerberos</code>
此指令重新啟動遠端計算機Server01和使用Kerberos身份驗證。如果使用者不具有的權限重新啟動遠端伺服器,它會報出一個拒絕通路錯誤。
好了! 今天 主要學習了 新的參數關于重新啟動計算機,相信大家已經了解并且掌握了相關技術操作。
本文轉自cantgis 51CTO部落格,原文連結:http://blog.51cto.com/cantgis/1222976,如需轉載請自行聯系原作者