如何强制format.ps1xml文件包装列?

时间:2019-04-12 11:40:28

标签: xml powershell output

我为我的一个功能创建了一个新的自定义表输出,并希望有更好经验的人可以解决我遇到的问题。

我为表中的某些项目设置了列宽,希望它们在那时会回绕,但是直到控制台窗口的宽度填满后,它们才似乎这样做。然后,它仅对最后一个离开屏幕的列执行此操作。输出中会省略任何其他列。

这是我的ps1xml摘录:

<View>
    <Name>L3Rule</Name>
    <ViewSelectedBy>
        <TypeName>Show.L3Rule</TypeName>
    </ViewSelectedBy>
    <GroupBy>
        <ScriptBlock>
            $_.Name
        </ScriptBlock>
        <CustomControlName>RuleGrouping</CustomControlName>
    </GroupBy>
    <TableControl>
        <AutoSize />
        <TableHeaders>
            <TableColumnHeader>
                <Label>ID</Label>
            </TableColumnHeader>
            <TableColumnHeader>
                <Label>Action</Label>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>30</Width>
                <Label>Source</Label>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>30</Width>
                <Label>Destination</Label>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>30</Width>
                <Label>Service</Label>
            </TableColumnHeader>
            <TableColumnHeader>
                <Label>Logged</Label>
            </TableColumnHeader>
            <TableColumnHeader>
                <Label>Tag</Label>
            </TableColumnHeader>
        </TableHeaders>
        <TableRowEntries>
            <TableRowEntry>
                <Wrap/>
                <TableColumnItems>
                    <TableColumnItem>
                        <PropertyName>ID</PropertyName>
                    </TableColumnItem>
                    <TableColumnItem>
                        <PropertyName>Action</PropertyName>
                    </TableColumnItem>
                    <TableColumnItem>
                        <ScriptBlock>
                            if ($_.Source -is [System.String]) { $_.Source }
                            else { $_.Source.Name -join "; " }
                        </ScriptBlock>
                    </TableColumnItem>
                    <TableColumnItem>
                        <ScriptBlock>
                            if ($_.Destination -is [System.String]) { $_.Destination }
                            else { $_.Destination.Name -join "; " }
                        </ScriptBlock>
                    </TableColumnItem>
                    <TableColumnItem>
                        <ScriptBlock>
                            if ($_.Service -is [System.String]) { $_.Service }
                            else { $_.Service.Name -join "; " }
                        </ScriptBlock>
                    </TableColumnItem>
                    <TableColumnItem>
                        <PropertyName>Logged</PropertyName>
                    </TableColumnItem>
                    <TableColumnItem>
                        <PropertyName>Tag</PropertyName>
                    </TableColumnItem>
                </TableColumnItems>
            </TableRowEntry>
        </TableRowEntries>
    </TableControl>
</View>

以下是输出示例:

     Rule: Inbound to blah


ID   Action Source      Destination Service             Logged Tag
--   ------ ------      ----------- -------             ------ ---
1111 allow  Somewhere   ANY         Service; Service    true   N/A


     Rule: Outbound to blah


ID   Action Source      Destination                             Service
--   ------ ------      -----------                             -------
2222 allow  Item1       Object1; AnotherObject1; MoreObjects    Service; Service (TCP); Service (TCP); Another
                                                                Service (TCP); This Service

如您所见,它省略了最后两列,并且某些列大于指定的30个字符。如果它们也按我期望的方式进行包装,我会很希望,但是我怀疑问题在于它们只允许在行入口级别进行包装。

理想情况下,我可以在输出的项目上使用Out-String。我尝试过显示这样的项目,但我认为这迫使列的默认宽度。 (我在DotNetTypes.format.ps1xml文件-第3420行中看到了类似的内容)

     Rule: Inbound to blah


ID   Action Source      Destination Service             Logged Tag
--   ------ ------      ----------- -------             ------ ---
1111 allow  Somewhere   ANY         Service             true   N/A
                                    Service


     Rule: Outbound to blah


ID   Action Source Destination    Service               Logged Tag
--   ------ ------ -----------    -------               ------ ---
2222 allow  Item1  Object1        Service               true   N/A      
                   AnotherObject1 Service (TCP)
                   MoreObjects    Service (TCP)
                                  Another Service (TCP)
                                  This Service

希望我提供了足够的信息,但是如果您需要了解更多信息,请告诉我。请告诉我我在做蠢事。

谢谢

1 个答案:

答案 0 :(得分:0)

弄清楚了。结合使用Out-String,设置所有列大小和删除 AutoSize 标记,对您大有帮助。

如果您希望实现相似的目标并且发现此页面同样令人沮丧,请按照以下方法操作。

<View>
    <Name>L3Rule</Name>
    <ViewSelectedBy>
        <TypeName>Show.L3Rule</TypeName>
    </ViewSelectedBy>
    <GroupBy>
        <ScriptBlock>
            $_.Name
        </ScriptBlock>
        <CustomControlName>RuleGrouping</CustomControlName>
    </GroupBy>
    <TableControl>
        <TableHeaders>
            <TableColumnHeader>
                <Width>6</Width>
                <Label>ID</Label>
                <Alignment>Left</Alignment>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>8</Width>
                <Label>Action</Label>
                <Alignment>Left</Alignment>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>35</Width>
                <Label>Source</Label>
                <Alignment>Left</Alignment>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>35</Width>
                <Label>Destination</Label>
                <Alignment>Left</Alignment>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>18</Width>
                <Label>Service</Label>
                <Alignment>Left</Alignment>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>6</Width>
                <Label>Logged</Label>
                <Alignment>Left</Alignment>
            </TableColumnHeader>
            <TableColumnHeader>
                <Width>20</Width>
                <Label>Tag</Label>
                <Alignment>Left</Alignment>
            </TableColumnHeader>
        </TableHeaders>
        <TableRowEntries>
            <TableRowEntry>
                <Wrap/>
                <TableColumnItems>
                    <TableColumnItem>
                        <PropertyName>ID</PropertyName>
                    </TableColumnItem>
                    <TableColumnItem>
                        <PropertyName>Action</PropertyName>
                    </TableColumnItem>
                    <TableColumnItem>
                        <ScriptBlock>
                            if ($_.Source -is [System.String]) { $_.Source }
                            else { $_.Source.Name | Out-String }
                        </ScriptBlock>
                    </TableColumnItem>
                    <TableColumnItem>
                        <ScriptBlock>
                            if ($_.Destination -is [System.String]) { $_.Destination }
                            else { $_.Destination.Name | Out-String }
                        </ScriptBlock>
                    </TableColumnItem>
                    <TableColumnItem>
                        <ScriptBlock>
                            if ($_.Service -is [System.String]) { $_.Service }
                            else { $_.Service.Name | Out-String }
                        </ScriptBlock>
                    </TableColumnItem>
                    <TableColumnItem>
                        <PropertyName>Logged</PropertyName>
                    </TableColumnItem>
                    <TableColumnItem>
                        <PropertyName>Tag</PropertyName>
                    </TableColumnItem>
                </TableColumnItems>
            </TableRowEntry>
        </TableRowEntries>
    </TableControl>
</View>