VMware Cloud Community
dmalhot1
Contributor
Contributor
Jump to solution

Print Parent object property

This is a pretty basic query I believe but somehow I can't get my head around it.  I want to extract the list of syslog servers configured on Esxi hosts . The powercli command I used is :

Get-VMHost | Get-AdvancedSetting | ?{$_.Name -Like "Syslog.global.logHost*"}  | Select-Object -Property Value

Query 1  :  How do i reference the parent object property ? I intend to Print the Esxi Hostname in one column and the list of syslog servers configured in 2nd column. I tried using calculated properties but it didn't work . So this is what I used :

Get-VMHost | Get-AdvancedSetting | ?{$_.Name -Like "Syslog.global.logHost*"}  | Select-Object -Property Value , @{Name = 'WithParens';Expression = {"( $($_.Name) )"}}

Some Progress .....

I was able to refer the immediate parent object but not the outermost parent ( not sure if that is the correct lingo )

Get-VMHost | Get-AdvancedSetting | ?{$_.Name -Like "Syslog.global.logHost*"}  | Select-Object -Property Value , @{Name = 'Parent';Expression = {"( $($_.Name) )"}}

Also tried another approach with a foreach block but it just hangs :

foreach($ho in Get-VMHost){ Select @{$_.Name},@{N='syslog';E={$ho.Name | Get-AdvancedSetting | ?{$_.Name -Like "Syslog.global.logHost*"} | Select-Object -Property Value}}

What am I doing wrong ? What is the gold standard way of referencing nested properties ?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You find that in the Entity property.

Get-VMHost |
Get-AdvancedSetting -Name 'Syslog.global.logHost*' |

Select @{N='VMHost';E={$_.Entity.Name}},Value


Or you could use the PipelineVariable.

Get-VMHost -PipelineVariable esx |

Get-AdvancedSetting -Name 'Syslog.global.logHost*' |

Select @{N='VMHost';E={$esx.Name}},Value


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

You find that in the Entity property.

Get-VMHost |
Get-AdvancedSetting -Name 'Syslog.global.logHost*' |

Select @{N='VMHost';E={$_.Entity.Name}},Value


Or you could use the PipelineVariable.

Get-VMHost -PipelineVariable esx |

Get-AdvancedSetting -Name 'Syslog.global.logHost*' |

Select @{N='VMHost';E={$esx.Name}},Value


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

dmalhot1
Contributor
Contributor
Jump to solution

LucD​  thanks for the prompt reply . It seems i over-complicated things.  pipeline variable looks a cool way of referring parent objects.  I will try out more nested use cases .  Would you mind checking on what was wrong with my foreach approach ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're not feeding anything to the Select.

Although you should rather use the pipeline, it could work like this

foreach($ho in Get-VMHost){

    $ho |Select Name,

        @{N='syslog';E={Get-AdvancedSetting -Entity $ho -Name "Syslog.global.logHost*" | Select-Object -ExpandProperty Value}}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos