powershell使用格式定义文件(
Format.ps1xml
)定义对象的默认显示。
若要在代码中实现此功能,请通过替换在返回的对象中包含类型;
[pscustomobject][ordered]@{
PropertyName = $_
InputObject = $InputObject."$_"
CompareObject = $CompareObject."$_"
}
用:
$result = [pscustomobject][ordered]@{
PropertyName = $_
InputObject = $InputObject."$_"
CompareObject = $CompareObject."$_"
}
$result.PSObject.TypeNames.Insert(0,'JohnLBevan.CompareObjectPropertiesResult')
$result
然后使用适当的属性名和设置为给定类型/创建定义文件:
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Default</Name>
<ViewSelectedBy>
<TypeName>JohnLBevan.CompareObjectPropertiesResult</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>20</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Width>90</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Width>90</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>PropertyName</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>InputObject</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>CompareObject</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
将此文件另存为
YourTypeName.format.ps1xml
.
最后,将此定义导入脚本:
Update-FormatData -AppendPath '.\JohnLBevan.CompareObjectPropertiesResult.Format.ps1xml'
现在,当调用函数时,输出将由定义文件中的信息决定。