craig
craig

Reputation: 26272

PowerShell custom format

I want to display/customize a subset of properties from a SharePoint list (via the OData API), which have been converted from JSON to a PsCustomObject.

Towards that end, I created a custom-format file (PsFoobar.Format.ps1xml) that I'd like to use with a PowerShell module:

<?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>

I referenced it in the module's manifest (.psd1):

TypesToProcess = @('PsFoobar.Format.ps1xml')

When I attempt to load the module, I get an error:

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

What am I missing?

Upvotes: 1

Views: 621

Answers (1)

user4003407
user4003407

Reputation: 22132

You using a wrong key in module manifest. For format data you should use FormatsToProcess instead of TypesToProcess.

By the way, System.Management.Automation.PSCustomObject is a common type name for all custom objects. For example: objects returned by Select-Object cmdlet have that type name. By adding your format definition you likely break displaying of all that objects. I recommend you to add custom tag to type name:

<TypeName>System.Management.Automation.PSCustomObject#MySharePointData</TypeName>

And add this type name to your objects:

ConvertFrom-Json ...|Add-Member -TypeName System.Management.Automation.PSCustomObject#MySharePointData -PassThru

Upvotes: 3

Related Questions