Add custom format to Powershell SDK without using SnapIn

I am developing a .NET / C # 2.0 application that uses the PowerShell SDK to execute a script. I am not using SnapIns. I configure everything directly through PS RunspaceConfiguration.

So my problem is that I cannot add a custom format for my Plux.ExtensionTypeInfo type implemented in the application.

(Plux.ExtensionTypeInfo has a Name property)

Here's what I'm trying:

...
RunspaceConfiguration config = RunspaceConfiguration.Create();

config.Formats.Prepend(
    new FormatConfigurationEntry("plux.format.ps1xml")
    );

config.Formats.Update();
...

      

plux.format.ps1xml:

<Configuration>
  <ViewDefinitions>
  <View>
       <Name>Plux.ExtensionTypeInfo</Name>
            <ViewSelectedBy>
                <TypeName>Plux.ExtensionTypeInfo</TypeName>
            </ViewSelectedBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader>
                        <Width>30</Width>
                    </TableColumnHeader>
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Name</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
</ViewDefinitions>
</Configuration>

      

After running a cmdlet that returns multiple ExtensionTypeInfo objects, the output will never be formatted.

With built-in cmdlets and types, the formatting works fine in my PS Host app. Registering the Cmdlet also works fine through the config object. No error occurs when running update-formatdata on plux.format.ps1xml using powershell.exe or my hosting app.

However, the code above does not affect the formatting.

0


a source to share


1 answer


I haven't tried using the PowerShell runtime. But I'm sure your problem is that the output formatting is not happening because you are capturing the pipeline in your application and not in the PowerShell host.

Formatting the output happens in the Out-Default cmdlet on the PowerShell host or by calling Format-Table or Format-List to specify the format.

EDIT:



My suggestion was to run this in a workspace.

YourCommand | Format-Table Name | Out-String

      

Also, I hope you are not trying to parse this output.

0


a source







All Articles