Automation

 View Only
  • 1.  Print Parent object property

    Posted Sep 09, 2020 09:43 AM

    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 ?



  • 2.  RE: Print Parent object property
    Best Answer

    Posted Sep 09, 2020 09:53 AM

    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



  • 3.  RE: Print Parent object property

    Posted Sep 09, 2020 10:23 AM

    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 ?



  • 4.  RE: Print Parent object property

    Posted Sep 09, 2020 10:47 AM

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

    }