views:

49

answers:

1

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?

+2  A: 

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.

Keith Hill
I tried it with and without <Label> but it didn't change the outcome ... also checked <TypeName> and ps1xml in the Snapin dir. The TypeName is the type of the displayed data, not the type of the cmdlet, right?
Hinek
Hi Keith, the TypeName is the type of the displayed data, right? So if I return the data in my cmdlet by executing WriteObject(new RemoteFileData(...)); the TypeName must be the namespace and name of RemoteFileData, right?
Hinek
Yes, that's correct. After your snapin is loaded, do a `Get-FormatData -TypeName <full type name>` on your type and see if it is making it into the format data. Also, run the output of your cmdlet through `Get-Member` and double-check the full typename.
Keith Hill
Funny thing: when I tried your suggestions (thanks by the way), I found out, that I get the correctly formatted output, when I call just my cmdlet. But if I call "Get-Help My-Cmdlet" first and then My-Cmdlet it still returns the output without following my view ... crazy!?
Hinek
Yeah that is pretty crazy. If you call `Get-Help My-Cmdlet` first and then `Get-FormatData -Type <full type name>` does your format data still show up?
Keith Hill