Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在Powershell SDK中添加自定义格式而不使用管理单元_.net_Powershell_Sdk_Cmdlets - Fatal编程技术网

.net 在Powershell SDK中添加自定义格式而不使用管理单元

.net 在Powershell SDK中添加自定义格式而不使用管理单元,.net,powershell,sdk,cmdlets,.net,Powershell,Sdk,Cmdlets,我正在开发一个.NET/C#2.0应用程序,它使用PowerShell SDK执行脚本。我没有使用SnapIns。我直接通过PS的RunspaceConfiguration设置所有内容 因此,我的问题是无法为应用程序中实现的类型Plux.ExtensionTypeInfo添加自定义格式 (Plux.ExtensionTypeInfo有一个名为Name的属性) 这就是我所尝试的: ... RunspaceConfiguration config = RunspaceConfiguration.Cr

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

因此,我的问题是无法为应用程序中实现的类型Plux.ExtensionTypeInfo添加自定义格式

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

这就是我所尝试的:

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

Plux.ExtensionTypeInfo
Plux.ExtensionTypeInfo
30
名称
执行Cmdlet(返回几个ExtensionTypeInfo对象)后,将永远不会格式化输出

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


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

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

输出格式化在PowerShell主机中的Out Default cmdlet中进行,或者通过调用格式表或格式列表来指定格式

编辑:

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

YourCommand | Format-Table Name | Out-String

另外,我希望您不要试图解析此输出。

(很抱歉,我没有及时回答您的问题)是的,我确实在执行之前将输出字符串附加到每个管道中。因此,默认格式非常有效。