在Powershell SDK中添加自定义格式,不使用SnapIn

时间:2023-01-26 22:02:19

I am developing a .NET/C# 2.0 application which uses the PowerShell SDK for script execution. I am not using SnapIns. I am setting everything directly through PS's RunspaceConfiguration.

我正在开发一个.NET / C#2.0应用程序,它使用PowerShell SDK来执行脚本。我没有使用SnapIns。我通过PS的RunspaceConfiguration直接设置所有内容。

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

所以我的问题是我无法为我在应用程序中实现的类型Plux.ExtensionTypeInfo添加自定义格式。

( Plux.ExtensionTypeInfo has a property called Name )

(Plux.ExtensionTypeInfo有一个名为Name的属性)

That is what I try:

这就是我尝试的:

...
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 executing a Cmdlet, which returns a few ExtensionTypeInfo objects, the output will never be formatted.

执行返回一些ExtensionTypeInfo对象的Cmdlet后,将永远不会格式化输出。

With the built in Cmdlets and Types, the formatting works perfectly in my PS Host application. The Cmdlet registration works also fine through the config object. When launching update-formatdata on plux.format.ps1xml, with powershell.exe or my hosting application, no errors are thrown.

使用内置的Cmdlet和类型,格式在我的PS主机应用程序中完美运行。 Cmdlet注册在配置对象中也可以正常工作。使用powershell.exe或我的托管应用程序在plux.format.ps1xml上启动update-formatdata时,不会引发任何错误。

Still, the code above has no effect on formatting.

不过,上面的代码对格式化没有影响。

1 个解决方案

#1


I haven't tried hosting the PowerShell runtime environment. But I am pretty sure that your issue is that the output formatting isn't happening because you are capturing the pipeline in your application, rather than in the PowerShell host.

我还没有尝试托管PowerShell运行时环境。但我很确定您的问题是输出格式没有发生,因为您正在应用程序中捕获管道,而不是在PowerShell主机中。

Output formatting happens in the Out-Default cmdlet in the PowerShell host, or by calling Format-Table or Format-List to specify a format.

输出格式在PowerShell主机的Out-Default cmdlet中进行,或者通过调用Format-Table或Format-List指定格式。

EDIT:

My suggestion would be to run this in the runspace.

我的建议是在运行空间中运行它。

YourCommand | Format-Table Name | Out-String

Also, I hope you aren't trying to parse this output.

另外,我希望你不要试图解析这个输出。

#1


I haven't tried hosting the PowerShell runtime environment. But I am pretty sure that your issue is that the output formatting isn't happening because you are capturing the pipeline in your application, rather than in the PowerShell host.

我还没有尝试托管PowerShell运行时环境。但我很确定您的问题是输出格式没有发生,因为您正在应用程序中捕获管道,而不是在PowerShell主机中。

Output formatting happens in the Out-Default cmdlet in the PowerShell host, or by calling Format-Table or Format-List to specify a format.

输出格式在PowerShell主机的Out-Default cmdlet中进行,或者通过调用Format-Table或Format-List指定格式。

EDIT:

My suggestion would be to run this in the runspace.

我的建议是在运行空间中运行它。

YourCommand | Format-Table Name | Out-String

Also, I hope you aren't trying to parse this output.

另外,我希望你不要试图解析这个输出。