PowerShell 3.0 實踐(四)開發(fā)自定義 PSSnapin
PowerShell通過Snapin提供了一個可擴(kuò)展的體系結(jié)構(gòu),以便用戶添加自定義的功能。Snapin是一組.NET程序集,說白了就是一個個類。
任何一個.NET程序集,只要實現(xiàn)了Snapin的安裝程序類,就可以成為一個Snapin。
注:PowerShell 3.0 CTP2已經(jīng)發(fā)布,可以在這里下載
自從 CTP1 以來的改動有:
- 修復(fù)了大量用戶在 CTP1 中找到的問題。具體的問題列表可以在發(fā)布注釋還有 Connect 中找到。
- Windows PowerShell ISE 中可以開啟單獨的命令行窗口。
- 可以通過 Update-Help 來更新本地的幫助。所有的幫助將從 Internet 獲得。
- 在 Windows PowerShell Workflows 的支持上本次發(fā)布包含了大量的改進(jìn),包括: Parallel, Sequence 和 Inlinescript。
- Get-Module 現(xiàn)在支持從任何遠(yuǎn)程 Session 或者 CIM Session 獲得模塊。
具體的改動請參看WMF CTP2 Release Notes.docx (發(fā)布注釋)
PowerShell 3.0提供了如下Snapin:
Get-PSSnapin
下面我們動手來開發(fā)一個Snapin。
1、我使用的Visual Studio 11 Developer Preview,新建一個Class Library項目:
2、添加System.Management.Automation引用,System.Management.Automation是PowerShell SDK的一部分,安裝PowerShell 3.0時已經(jīng)安裝在GAC中。
3、繼承PSSnapIn 類,這是一個抽象類,自定義的Snapin需要繼承它。Name是自定義的Snapin的名稱,Verdor是作者,Description是對該Snapin的簡短描述。
2 public class Test : PSSnapIn
3 {
4 // Name for the PowerShell snap-in.
5 public override string Name
6 {
7 get
8 {
9 return "Brooks.Scripts.PowerShell.Test";
10 }
11 }
12
13 // Vendor information for the PowerShell snap-in.
14 public override string Vendor
15 {
16 get
17 {
18 return "Brooks";
19 }
20 }
21
22 // Description of the PowerShell snap-in
23 public override string Description
24 {
25 get
26 {
27 return "This is a sample PowerShell snap-in";
28 }
29 }
30 }
4、添加自己的Cmdlet,實現(xiàn)一些功能。我準(zhǔn)備開發(fā)一個Excel的幫助命令,在指定位置生成一個Excel文檔。
為此,先添加Office PIA的引用:Microsoft.Office.Interop.Excel
5、編寫一個類繼承Cmdlet,參數(shù)以屬性的形式體現(xiàn),一般需要重寫BeginProcessing()、ProcessRecord()、EndProcessing()三個方法。
BeginProcessing():執(zhí)行初始化操作,如驗證參數(shù)、準(zhǔn)備數(shù)據(jù)等。
ProcessRecord():執(zhí)行核心命令邏輯。
EndProcessing():收尾工作,如資源釋放、記錄日志等。
PowerShell的命令采用的是 動詞-名詞 的結(jié)構(gòu),這里我使用的New-Excel命令。
參數(shù)聲明中,Mandatory = true表示是必須參數(shù),Position = 1 表示是位置參數(shù),這樣就可以根據(jù)位置來自動推斷參數(shù)。
2 public class AddTest : Cmdlet
3 {
4 private string __Path = string.Empty;
5 private Excel.XlFileFormat __Format = Excel.XlFileFormat.xlOpenXMLWorkbook;
6
7 [Parameter(Mandatory = true, Position = 1)]
8 public string Path
9 {
10 get
11 {
12 return this.__Path;
13 }
14 set
15 {
16 this.__Path = value;
17 }
18 }
19
20 [Parameter(Mandatory = true, Position = 2)]
21 public Excel.XlFileFormat Format
22 {
23 get
24 {
25 return this.__Format;
26 }
27 set
28 {
29 this.__Format = value;
30 }
31 }
32
33 protected override void BeginProcessing()
34 {
35 base.BeginProcessing();
36 }
37
38 protected override void ProcessRecord()
39 {
40 Excel.Application __app = new Excel.Application();
41 __app.DisplayAlerts = false;
42 Excel.Workbook __book = __app.Workbooks.Add();
43 try
44 {
45 __book.SaveAs(this.Path, this.Format);
46 }
47 catch
48 { }
49 finally
50 {
51 __app.Quit();
52 System.Runtime.InteropServices.Marshal.ReleaseComObject(__book);
53 System.Runtime.InteropServices.Marshal.ReleaseComObject(__app);
54 __book = null;
55 __app = null;
56 }
57 }
58
59 protected override void EndProcessing()
60 {
61 base.EndProcessing();
62 }
63 }
6、這樣一個Snapin就開發(fā)完成了,為了在腳本中使用,需要注冊該Snapin并執(zhí)行導(dǎo)入。
以管理員身份運行PowerShell ISE,執(zhí)行以下腳本,將路徑更改為你本機的目錄。
Set-Location E:\Work\Project\BrooksCom\Brooks.Scripts.PowerShell\Brooks.Scripts.PowerShell\bin\Debug
Set-Alias installutil $env:windir\microsoft.net\framework64\v4.0.30319\installutil
installutil -i Brooks.Scripts.PowerShell.dll
installutil是.NET自帶的安裝工具,默認(rèn)位于:
%windir% \Microsoft.NET\Framework64\v4.0.30319 --- Windows x64
%windir% \Microsoft.NET\Framework\v4.0.30319 --- Windows x86
注意64位和32位操作系統(tǒng)分別使用對應(yīng)的installutil。
下面導(dǎo)入該Snapin:
Add-PSSnapin -Name Brooks.Scripts.PowerShell.Test
驗證是否導(dǎo)入成功:
Get-PSSnapin –Registered
自定義的Snapin位于注冊表的如下位置:
7、最后來執(zhí)行以下我們的命令:New-Excel
New-Excel -Path E:\Test.xlsx -Format xlOpenXMLWorkbook
可以看到,順利生成了Excel文檔:
小結(jié):
PowerShell 3.0包含了眾多的命令,幾乎可以管理所有的方面,尤其在Windows Server 8中,PowerShell 已經(jīng)處于核心地位。
公司的CTO是一位批處理、匯編高手,但是當(dāng)我向他推薦PowerShell時,他卻不屑一顧,覺得太不倫不類,原話是"什么鳥語法" J
這也從側(cè)面驗證了PowerShell現(xiàn)在的處境以及被高級管理人員所誤解的程度。本次從一個自定義Snapin的開發(fā)展示了PowerShell的可擴(kuò)展性,與開發(fā)一個普通的組件沒有什么區(qū)別。我個人表示看好PowerShell在企業(yè)級管理中的前景,PowerShell也可以擴(kuò)展為腳本引擎嵌入ERP等系統(tǒng)中,執(zhí)行部署、流程控制、自動化操作等。


浙公網(wǎng)安備 33010602011771號