神剑山庄资源网 Design By www.hcban.com
还记得当年怎样在PowerShell中动态创建对象吧?今天要分享的方法不敢自诩高大上,但也足以让New-Object感到汗颜。
背景
在System Center Operation Manager中有个Management Pack,叫做:“Microsoft.SystemCenter.OperationsManager.SummaryDashboard”。在该MP中有个Discovery叫做:“Collect agent configurations”。该工作流中用到了一段脚本,其中使用了New-Module命令。
New-Module就是在内存中动态生成一个Module组件。用它来自定义对象有点大材小用了。
演习
$PLA = New-Module { $名称 = ‘中国人民解放军' $军区 = @('沈阳军区','北京军区','济南军区','南京军区','广州军区','成都军区','兰州军区') $兵种 = @('海军','空军','第二炮兵') function 保卫党 { return $true } function 保卫人民 { return $null } function 抗洪抢险 { return $true } function 抗震救灾 { return $true } function 确认兵种 { param($某兵种) if ($this.兵种.Contains($某兵种)){ return $true } return $false } Export-ModuleMember -Variable * -Function * } -AsCustomObject
PS> $PLA 兵种 军区 名称 -- -- -- {海军, 空军, 第二炮兵} {沈阳军区, 北京军区, 济南军区, 南京军区...} 中国人民解放军 PS> $PLA.确认兵种(‘陆军') 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[] 军区 NoteProperty System.Object[] 军区=System.Object[] 名称 NoteProperty System.String 名称=中国人民解放军 保卫人民 ScriptMethod System.Object 保卫人民(); 保卫党 ScriptMethod System.Object 保卫党(); 抗洪抢险 ScriptMethod System.Object 抗洪抢险(); 抗震救灾 ScriptMethod System.Object 抗震救灾();
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无PowerShell小技巧之使用New-Module命令动态创建对象的评论...
更新日志
2024年10月04日
2024年10月04日
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】