偶尔,需要批量完善对象的属性。例如,假设你查询了进程对象,需要在创建报告的基础的属性上增加自定义标题:
filter Rename-Property ([Hashtable]$PropertyMapping) { Foreach ($key in $PropertyMapping.Keys) { $_ = $_ | Add-Member -MemberType AliasProperty -Name $PropertyMapping.$key -Value $key -PassThru } $_ } $newProps = @{ Company = 'Manufacturer' Description = 'Purpose' MainWindowTitle = 'TitlebarText' } # get raw data Get-Process | # add alias properties as specified in $newProps Rename-Property $newProps | # select the properties you want to display # can be original properties and/or newly added alias properties Select-Object -Property Name, Manufacturer, Purpose,
Rename-Property函数自动追加了$newprops哈希表中的自定义属性。新的对象中增加”Manufacturer”, “Purpose”,和”TitlebarText”属性,现在你可以使用Select-Object获取你自定义表格的属性了,可选择的属性来自原始的和新建的。
原文地址:Bulk Renaming Object Properties
本文链接: https://www.pstips.net/bulk-renaming-object-properties.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!