How to display a nested collection with .ps1xml file in powershell

狂风中的少年 提交于 2019-12-10 03:56:23

问题


I have a hierarchical object structure like this:

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; }
}

How can i create a custom .ps1xml file that will let me display Department(s) as follows:

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

The main problem i'm having is how i can define a <View> item that is selected for a Department, that is based on a TableControl, but which displays the Department.Employees in the table control.

I can display Employee(s) perfectly fine using a 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>

And i can format departments using a list format:

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

But how do i add a table of the employees after the department?


回答1:


I think you need to make use of <GroupBy>...</GroupBy and also <Control><CustomControl>...</CustomControl></Control>

Take a look at this ps1xml format file for DiscUtils module, haven't had the chance to play with it myself yet but it may put you on the right path.

See also the help about_Format.ps1xml which has some info, though a little light on examples for some aspects.



来源:https://stackoverflow.com/questions/17776048/how-to-display-a-nested-collection-with-ps1xml-file-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!