PowerShell 自定义格式
PowerShell custom format
我想 display/customize SharePoint 列表中的属性子集(通过 OData API),这些属性已从 JSON 转换为 PsCustomObject。
为此,我创建了一个自定义格式文件 (PsFoobar.Format.ps1xml
),我想将其与 PowerShell 模块一起使用:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>RequestView</Name>
<ViewSelectedBy>
<TypeName>System.Management.Automation.PsCustomObject</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>4</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>25</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Title</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
我在模块的清单中引用了它 (.psd1
):
TypesToProcess = @('PsFoobar.Format.ps1xml')
当我尝试加载模块时,出现错误:
import-module : The following error occurred while loading the extended type data file:
, C:\Users\...\WindowsPowerShell\Modules\PsFoobar\PsFoobar.Format.ps1xml(0) : Error:
The node Configuration cannot be present. Nodes allowed are: Types.
, C:\Users\...\WindowsPowerShell\Modules\PsFoobar\PsFoobar.Format.ps1xml(0) : Error:
Node "Types" was not found. It should be present once under "Document". The parent node "Document" will be ignored.
At line:1 char:1
+ import-module PsFoobar -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
我错过了什么?
您在模块清单中使用了错误的密钥。对于格式数据,您应该使用 FormatsToProcess
而不是 TypesToProcess
.
顺便说一下,System.Management.Automation.PSCustomObject
是所有自定义对象的通用类型名称。例如:Select-Object
cmdlet 返回的对象具有该类型名称。通过添加格式定义,您可能会中断所有对象的显示。我建议您添加自定义标签以键入名称:
<TypeName>System.Management.Automation.PSCustomObject#MySharePointData</TypeName>
并将此类型名称添加到您的对象中:
ConvertFrom-Json ...|Add-Member -TypeName System.Management.Automation.PSCustomObject#MySharePointData -PassThru
我想 display/customize SharePoint 列表中的属性子集(通过 OData API),这些属性已从 JSON 转换为 PsCustomObject。
为此,我创建了一个自定义格式文件 (PsFoobar.Format.ps1xml
),我想将其与 PowerShell 模块一起使用:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>RequestView</Name>
<ViewSelectedBy>
<TypeName>System.Management.Automation.PsCustomObject</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>4</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>25</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Title</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
我在模块的清单中引用了它 (.psd1
):
TypesToProcess = @('PsFoobar.Format.ps1xml')
当我尝试加载模块时,出现错误:
import-module : The following error occurred while loading the extended type data file:
, C:\Users\...\WindowsPowerShell\Modules\PsFoobar\PsFoobar.Format.ps1xml(0) : Error:
The node Configuration cannot be present. Nodes allowed are: Types.
, C:\Users\...\WindowsPowerShell\Modules\PsFoobar\PsFoobar.Format.ps1xml(0) : Error:
Node "Types" was not found. It should be present once under "Document". The parent node "Document" will be ignored.
At line:1 char:1
+ import-module PsFoobar -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
我错过了什么?
您在模块清单中使用了错误的密钥。对于格式数据,您应该使用 FormatsToProcess
而不是 TypesToProcess
.
顺便说一下,System.Management.Automation.PSCustomObject
是所有自定义对象的通用类型名称。例如:Select-Object
cmdlet 返回的对象具有该类型名称。通过添加格式定义,您可能会中断所有对象的显示。我建议您添加自定义标签以键入名称:
<TypeName>System.Management.Automation.PSCustomObject#MySharePointData</TypeName>
并将此类型名称添加到您的对象中:
ConvertFrom-Json ...|Add-Member -TypeName System.Management.Automation.PSCustomObject#MySharePointData -PassThru