【问题标题】:How can I force format.ps1xml files to wrap columns?如何强制 format.ps1xml 文件换行?
【发布时间】:2019-04-12 11:40:28
【问题描述】:

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

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

这是我的 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

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

谢谢

【问题讨论】:

    标签: xml powershell output


    【解决方案1】:

    想通了。使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-03
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多