How to display nested objects with .ps1xml file through c# cmdlet into powershell

不羁的心 提交于 2019-12-11 11:44:12

问题


hi i have heirarchial object structure like

   public class Department
   {
      public Guid ID {get; set;}
      public string Name{get; set;}
   }

   public class Employee
   {
      public Guid ID {get; set;}
      public string Name{get; set;}
      public Department Department {get;set;}
   }

i have created a formatedoutput.ps1xml file to show the employee object in powershell

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
 <ViewDefinitions>
  <View>
        <Name>EmpView</Name>
    <ViewSelectedBy>
      <TypeName>Employee</TypeName>
    </ViewSelectedBy>

  <TableControl>
    <TableHeaders>
      <TableColumnHeader>
        <Width>16</Width>
      </TableColumnHeader>
      <TableColumnHeader>
        <Width>16</Width>
      </TableColumnHeader>
      <TableColumnHeader/>
    </TableHeaders>
    <TableRowEntries>
      <TableRowEntry>
        <TableColumnItems>
          <TableColumnItem>
            <PropertyName>ID</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>Name</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>Department.Name</PropertyName>
          </TableColumnItem>
        </TableColumnItems>
      </TableRowEntry>
    </TableRowEntries>
  </TableControl>
 </View>
 </ViewDefinitions>
</Configuration>

i have registered my xml file using following cmdlet in PS

update-formatdata -prependpath c:\formatedoutput.ps1xml

it is showing tabular out put and it is displaying employee id and name but its not displaying department name.

i think its because it is in nested object

can anyone please help me in displaying department name


回答1:


I faced similar problem and came to this blog. For me situation got resolved by a simple change like below - to use ScriptBlock instead of PropertyName tag, and ofcourse, ensuring that the value is not null in Debug window:

<TableColumnItem>
    <ScriptBlock>$_.Department.Name</ScriptBlock>
</TableColumnItem>


来源:https://stackoverflow.com/questions/10102864/how-to-display-nested-objects-with-ps1xml-file-through-c-sharp-cmdlet-into-powe

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