如何在powershell中使用.ps1xml文件显示嵌套集合

oɔɯ*_*ɯǝɹ 7 format powershell

我有一个像这样的分层对象结构:

public class Department
{
    public string Name { get; set; }
    public string Manager { get; set; }
    public Employee[] Employees { get; set; }
}

public class Employee
{
    public string Name { get; set;}
    public string Speciallity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如何创建自定义.ps1xml文件,让我显示Department(s)如下:

    Department : Testers
    Manager    : P.H. Boss

Name                       Speciallity
----------                 -----------------------------
Some Employee              .Net
Another Employee           BizTalk
Yet Another                PowerShell
...                        ...


    Department : Developers
    Manager    : Wally

Name                       Speciallity
----------                 -----------------------------
Some Employee              .Net
Another Employee           BizTalk
Yet Another                PowerShell
...                        ...
Run Code Online (Sandbox Code Playgroud)

我遇到的主要问题是我如何定义一个<View>为a选择的项目Department,它基于a TableControl,但Department.Employees在表格控件中显示.

我可以Employee使用以下方式完美地显示View:

<View>
    <Name>Employee</Name>
    <ViewSelectedBy>
        <TypeName>Employee</TypeName>
    </ViewSelectedBy>
   <TableControl>
        <TableHeaders>
            <TableColumnHeader>
                <Label>Name</Label>
                <Width>30</Width>
            </TableColumnHeader>
            <TableColumnHeader>
                <Label>Speciallity</Label>
                <Width>50</Width>
            </TableColumnHeader>
        </TableHeaders>
        <TableRowEntries>
            <TableRowEntry>
                <Wrap/>
                <TableColumnItems>
                    <TableColumnItem>
                        <PropertyName>Name</PropertyName>
                    </TableColumnItem>
                    <TableColumnItem>
                        <PropertyName>Speciallity</PropertyName>
                    </TableColumnItem>
                </TableColumnItems>
            </TableRowEntry>
        </TableRowEntries>
    </TableControl>
</View>
Run Code Online (Sandbox Code Playgroud)

我可以使用列表格式格式化部门:

<View>
    <Name>TestResultSet</Name>
    <ViewSelectedBy>
        <TypeName>Department</TypeName>
    </ViewSelectedBy>
    <ListControl>
        <ListEntries>
            <ListEntry>
                <ListItems>
                    <ListItem>
                        <Label>Department</Label>
                        <PropertyName>Name</PropertyName>
                    </ListItem>
                    <ListItem>
                        <PropertyName>Manager</PropertyName>
                    </ListItem>
                </ListItems>
            </ListEntry>
        </ListEntries>
    </ListControl>
</View>
Run Code Online (Sandbox Code Playgroud)

但是我如何在部门之后添加员工表?

Gra*_*old 1

<GroupBy>...</GroupBy 我认为你还需要利用 <Control><CustomControl>...</CustomControl></Control>

看一下DiscUtils模块的 ps1xml 格式文件,我自己还没有机会使用它,但它可能会让您走上正确的道路。

另请参阅help about_Format.ps1xml其中包含一些信息,但对某些方面的示例稍有了解。