Automation

 View Only
  • 1.  Unable to get correct output working with get-view

    Posted Jul 25, 2016 07:47 AM

    Hi

    Can some one help me on this below code.

    After executing this script its not showing output in correct format.

    $resourcepool = 'testresp01','testresp02'

    $respExpression = "^$($resourcepool -join '$|^')$"

    foreach($resp in (Get-View -ViewType ResourcePool  -Filter @{'Name'=$respExpression})){

    $vmsinresp=Get-View -Id $resp.Vm

    $vmtoolsoutdate=$vmsinresp|? {$_.guest.toolsversionstatus -eq "guestToolsNeedUpgrade"}

    #$vmsinresp=Get-View -ViewType VirtualMachine -Property Name,runtime.powerState,Guest.GuestFullName,Config.GuestFullName,Config.Hardware.Device,Config.version,guest.toolsVersion,guest.toolsversionstatus -SearchRoot ($resp).MoRef

    $vmtoolsoutdate| select @{N="VM";E={$vmtoolsoutdate.name}},

    @{N='powerState';E={$vmtoolsoutdate.runtime.powerState}},

    @{N='IP';E={[string]::Join(',',($vmtoolsoutdate.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}}

    }

    Sample Output:

    VM                                                      powerState                                      IP                                            

    --                                                      ----------                                      --                                            

    {test347, test353, test349,test327...} {poweredOn, poweredOn, poweredOn, poweredOn...} 153.71.2.43,153.71.2.49,153.71.2.45,153.71.2...



  • 2.  RE: Unable to get correct output working with get-view
    Best Answer

    Posted Jul 25, 2016 09:29 AM

    In the calculated properties on the Select-Object cmdlet, you need to refer to the variable in the pipeline, not the complete array.

    Like this

    $resourcepool = 'testresp01','testresp02' 

    $respExpression = "^$($resourcepool -join '$|^')$" 

    foreach($resp in (Get-View -ViewType ResourcePool  -Filter @{'Name'=$respExpression})){ 

        $vmsinresp = Get-View -Id $resp.Vm 

        $vmtoolsoutdate = $vmsinresp|? {$_.guest.toolsversionstatus -eq "guestToolsNeedUpgrade"}  

        $vmtoolsoutdate | select @{N="VM";E={$_.name}}, 

            @{N='powerState';E={$_.runtime.powerState}}, 

            @{N='IP';E={[string]::Join(',',($_.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}} 

    }  



  • 3.  RE: Unable to get correct output working with get-view

    Posted Jul 25, 2016 09:33 AM

    Thanks Guru.