2015-07-10 65 views
0

我想显示/定制从SharePoint列表(通过OData API),已从JSON转换为PsCustomObject的属性的子集。PowerShell自定义格式

为此目的,我创建了一个自定义格式文件(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 

我错过了什么?

+3

@PetSerAI - 你应该作出这样的回答, –

回答

1

在模块清单中使用了错误的键。对于格式数据,您应该使用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 
+0

我沿着这些线路的思考阅读完毕后[Essential PowerShell:为自定义对象类型命名](http://poshoholic.com/2008/07/03/essential-powershell-name-your-custom-object-types/)和[Essential PowerShell:定义默认属性对于自定义对象](http://poshoholic.com/2008/07/05/essential-powershell-define-default-properties-for-custom-objects/) – craig