yrahman
yrahman

Reputation: 950

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

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

Upvotes: 2

Views: 666

Answers (1)

nkanani
nkanani

Reputation: 558

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>

Upvotes: 1

Related Questions