VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

get datacenter and vmhosts no output

hi

any idea why I am not getting any output from this?

&{foreach($dc in Get-Datacenter){

     foreach($esx in Get-VMHost -Location $dc){

      Select @{N='Datacenter';E={$dc.Name}}, @{N='VMhost';E={$esx.Name}}

            }}

            }

I am connected to all my vcenters

thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

And for the 2nd one, you have to output the resulting array (+ a few other minor adjustments).

$report = @()

$dcs = get-datacenter

foreach($dc in $dcs){

    $esxhosts = get-vmhost -Location $dc | ? {$_.connectedstate -ne "NotResponding"}

    foreach ($esx in $esxhosts) {

        $info = "" | select Datacenter, VMhost

        $info.Datacenter = $dc.Name

        $info.VMhost = $esx.Name

        $report += $info

    }

}

$report


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

View solution in original post

0 Kudos
4 Replies
tdubb123
Expert
Expert
Jump to solution

same with this

$report = @()

$dc = get-datacenter

$esxhosts = get-datacenter| get-vmhost | ? {$_.connectedstate -ne "NotResponding"}

foreach ($esx in $esxhosts) {

$info = "" | select Datacenter, VMhost

$info.Datacenter = $dc.Name

$info.VMhost = $esx.Name

$report += $info

}

no output

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You to feed something to the Select-Object, even an empty string will do.

&{foreach($dc in Get-Datacenter){

     foreach($esx in Get-VMHost -Location $dc){

      "" | Select @{N='Datacenter';E={$dc.Name}}, @{N='VMhost';E={$esx.Name}}

            }}

            }


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And for the 2nd one, you have to output the resulting array (+ a few other minor adjustments).

$report = @()

$dcs = get-datacenter

foreach($dc in $dcs){

    $esxhosts = get-vmhost -Location $dc | ? {$_.connectedstate -ne "NotResponding"}

    foreach ($esx in $esxhosts) {

        $info = "" | select Datacenter, VMhost

        $info.Datacenter = $dc.Name

        $info.VMhost = $esx.Name

        $report += $info

    }

}

$report


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

awesome thanks Luc

0 Kudos