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 ?
You find that in the Entity property.
Select @{N='VMHost';E={$_.Entity.Name}},Value
Or you could use the PipelineVariable.
Get-AdvancedSetting -Name 'Syslog.global.logHost*' |
Select @{N='VMHost';E={$esx.Name}},Value
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You find that in the Entity property.
Select @{N='VMHost';E={$_.Entity.Name}},Value
Or you could use the PipelineVariable.
Get-AdvancedSetting -Name 'Syslog.global.logHost*' |
Select @{N='VMHost';E={$esx.Name}},Value
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 ?
You're not feeding anything to the Select.
Although you should rather use the pipeline, it could work like this
$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