2013-07-21 22 views
5

與.ps1xml文件顯示嵌套集合我有一個層級對象結構是這樣的:如何在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; } 
} 

如何創建一個自定義.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 
...      ... 

時遇到的主要問題是如何我可以定義被選擇用於一個Department一個<View>項,即基於TableControl,但在表格控件中顯示Department.Employees

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

,我可以使用列表格式來格式化部門:

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

但如何添加的表

我可以Employee(S)完全正常使用View顯示部門後的員工?

+0

可能的重複[如何通過c#cmdlet將.ps1xml文件嵌入對象顯示爲powershell](http://stackoverflow.com/questions/10102864/how-to-display-nested-objects-with-ps1xml-file通過c-sharp-cmdlet-into-powe) –

+0

@GrahamGold:我注意到了這個問題,但是並沒有處理嵌套的'集合'。我認爲這種差異很重要,足以證明一個單獨的問題。我做了一些示例代碼從這個問題,雖然:-) –

+1

我最終添加一個PSParentPath NoteProperty到重複的項目,並分組在該領域的輸出。似乎沒有更好的辦法。我將在這裏爲其他用戶更新一些演示代碼。 –

回答

1

我認爲你需要做的<GroupBy>...</GroupBy使用,也<Control><CustomControl>...</CustomControl></Control>

看看爲DiscUtils模塊this PS1XML格式文件,還沒有與它自己上場的機會還沒有,但它可能把你正確的道路。

另請參閱help about_Format.ps1xml有一些信息,雖然有些方面的例子有點亮。

+1

我用''構造解決了這個問題,但我必須在我的Employee對象中添加一個'PSParentPath' NoteProperty來完成這個工作。我猜測沒有其他辦法可以做到這一點。 –

+0

不是我知道,但這並不意味着我是對的! :-) –