Question

I used this article to write my first own Powershell Cmdlet and Snapin. And it works fine.

But I return a set of objects from my own data class, which has four properties and I want Powershell to display just one of these properties by default. So I used this part of the article to create this format file:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
    <ViewDefinitions>
        <View>
            <Name>RemoteFile</Name>
            <ViewSelectedBy>
                <TypeName>MyFullNamespace.RemoteFileData</TypeName>
            </ViewSelectedBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader />
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Filename</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>

and link it in the Snapin:

    public override string[] Formats
    {
        get { return new string[] { "MyFormatFilename.ps1xml" }; }
    }

But when I install the Snapin with installutil, use Add-PSSnapin and call my Cmdlet, all Properties of the objects are displayed.

What am I doing wrong?

Was it helpful?

Solution

Everything looks correct except that I'm not sure how it behaves with no column header label defined. Try adding this node instead of your empty one:

<TableColumnHeader>
  <Label>FileName</Label>
</TableColumnHeader>

Also make sure the file MyFormatFilename.ps1xml is in the same dir with the snapin when it is being loaded via Add-PSSnapin. Also, probably a duh, but double check for typos in the type name specified in the <TypeName> element.

Update: I tried your XML as listed above and it works for me. I copied it into Notepad2 and saved it to C:\temp\test.ps1xml then executed:

1# $obj = new-object psobject
2# $obj.psobject.TypeNames.Insert(0, 'MyFullNamespace.RemoteFileData')
3# Add-Member -InputObject $obj -MemberType NoteProperty -Name Filename `
              -Value 'some-remotefile.txt'
4# Add-Member -InputObject $obj -MemberType NoteProperty -Name Dummy `
              -Value 'dummy prop'
5# $obj.psobject.TypeNames
MyFullNamespace.RemoteFileData
System.Management.Automation.PSCustomObject
System.Object
6# $obj

Filename                                                    Dummy
--------                                                    -----
some-remotefile.txt                                         dummy prop


7# Update-FormatData C:\temp\test.ps1xml
8# $obj

Filename
--------
some-remotefile.txt

I would double check the full typename instance.GetType().FullName and also double check the contents of the format file. Make sure it is in the same dir that you registered the snapin from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top