PowerShell小技巧之使用New-Module命令動(dòng)態(tài)創(chuàng)建對(duì)象
還記得當(dāng)年怎樣在PowerShell中動(dòng)態(tài)創(chuàng)建對(duì)象吧?今天要分享的方法不敢自詡高大上,但也足以讓New-Object感到汗顏。
背景
在System Center Operation Manager中有個(gè)Management Pack,叫做:“Microsoft.SystemCenter.OperationsManager.SummaryDashboard”。在該MP中有個(gè)Discovery叫做:“Collect agent configurations”。該工作流中用到了一段腳本,其中使用了New-Module命令。
New-Module就是在內(nèi)存中動(dòng)態(tài)生成一個(gè)Module組件。用它來自定義對(duì)象有點(diǎn)大材小用了。
演習(xí)
$PLA = New-Module { $名稱 = ‘中國人民解放軍' $軍區(qū) = @('沈陽軍區(qū)','北京軍區(qū)','濟(jì)南軍區(qū)','南京軍區(qū)','廣州軍區(qū)','成都軍區(qū)','蘭州軍區(qū)') $兵種 = @('海軍','空軍','第二炮兵') function 保衛(wèi)黨 { return $true } function 保衛(wèi)人民 { return $null } function 抗洪搶險(xiǎn) { return $true } function 抗震救災(zāi) { return $true } function 確認(rèn)兵種 { param($某兵種) if ($this.兵種.Contains($某兵種)){ return $true } return $false } Export-ModuleMember -Variable * -Function * } -AsCustomObject
PS> $PLA 兵種 軍區(qū) 名稱 -- -- -- {海軍, 空軍, 第二炮兵} {沈陽軍區(qū), 北京軍區(qū), 濟(jì)南軍區(qū), 南京軍區(qū)...} 中國人民解放軍 PS> $PLA.確認(rèn)兵種(‘陸軍') False PS> $PLA | Get-Member TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() 兵種 NoteProperty System.Object[] 兵種=System.Object[] 軍區(qū) NoteProperty System.Object[] 軍區(qū)=System.Object[] 名稱 NoteProperty System.String 名稱=中國人民解放軍 保衛(wèi)人民 ScriptMethod System.Object 保衛(wèi)人民(); 保衛(wèi)黨 ScriptMethod System.Object 保衛(wèi)黨(); 抗洪搶險(xiǎn) ScriptMethod System.Object 抗洪搶險(xiǎn)(); 抗震救災(zāi) ScriptMethod System.Object 抗震救災(zāi)();
- MySQL mysqldump命令使用詳解
- Oracle常用dump命令,記錄一下備查。
- mysql數(shù)據(jù)庫備份及恢復(fù)命令 mysqldump,source的用法
- Mysql導(dǎo)入導(dǎo)出工具M(jìn)ysqldump和Source命令用法詳解
- Linux tcpdump操作命令詳解
- Linux tcpdump命令的用法詳細(xì)解析
- linux使用tcpdump命令監(jiān)視指定網(wǎng)絡(luò)數(shù)據(jù)包的方法
- 使用MySQL MySqldump命令導(dǎo)出數(shù)據(jù)時(shí)的注意事項(xiàng)
- PHP使用mysqldump命令導(dǎo)出數(shù)據(jù)庫
- mysqldump命令導(dǎo)入導(dǎo)出數(shù)據(jù)庫方法與實(shí)例匯總
- Linux下使用tcpdump抓包的實(shí)現(xiàn)方法
- Linux 命令每天必學(xué)(34)之du命令
相關(guān)文章
PowerShell實(shí)現(xiàn)參數(shù)互斥示例
這篇文章主要介紹了PowerShell實(shí)現(xiàn)參數(shù)互斥示例,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06Windows Powershell 執(zhí)行外部命令
Windows PowerShell 在使用方面與 Cmd.exe 并無多大不同,只是 Windows PowerShell 的功能更為強(qiáng)大。與 Cmd.exe 一樣,Windows PowerShell 具有內(nèi)置的腳本編寫語言,不過它比 Cmd.exe 原始的批處理語言更為靈活。Cmd.exe 做到的事情,Windows PowerShell 幾乎都能做到。2014-08-08PowerShell實(shí)現(xiàn)在多個(gè)文件中檢索關(guān)鍵字功能
這篇文章主要介紹了PowerShell實(shí)現(xiàn)在多個(gè)文件中檢索關(guān)鍵字功能,本文使用Select-String解決這了個(gè)需求,需要的朋友可以參考下2015-01-01PowerShell實(shí)現(xiàn)統(tǒng)計(jì)函數(shù)嵌套深度
這篇文章主要介紹了PowerShell實(shí)現(xiàn)統(tǒng)計(jì)函數(shù)嵌套深度,本文分享一個(gè)函數(shù),可以實(shí)現(xiàn)統(tǒng)計(jì)腳本執(zhí)行的嵌套層次,需要的朋友可以參考下2015-06-06