abarakat
Contributor
Contributor

Hello need to dump all my hosts in what top level folder they are in then datacetner then number of vm's per host.

Jump to solution

Got this far

Get-VMHost | Select @{N="Datacenter";E={datacenter -VMHost $_}}, Name, @{N="NumVM";E={($_ | Get-VM).Count}} | Sort datacenter, NumVM

but not able to get it to dump the top level folder.

I have VC, Folders, Datacenters, hosts then VM's

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Ok, got it.

Try like this

Get-VMHost |

Select @{N="Datacenter";E={Get-Datacenter -VMHost $_}},

    Name,

    @{N="NumVM";E={($_ | Get-VM).Count}},

    @{N='Folder';E={

        $location = Get-View -Id $_.ExtensionData.Parent

        $folder = ''

        do{

            if($location -is [VMware.Vim.Folder] -and $location.Name -ne 'host'){

                $folder = $location

            }

            $location = Get-View -Id $location.Parent

        }

        until($location.Name -eq 'Datacenters')

        $folder.Name

    }} |

Sort datacenter, NumVM


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership

Try something like this

Get-VMHost |

Select @{N="Datacenter";E={Get-Datacenter -VMHost $_}},

    Name,

    @{N="NumVM";E={($_ | Get-VM).Count}},

    @{N='Folder';E={

        $location = Get-View -Id $_.ExtensionData.Parent

        $names = @()

        do{

            if($location -is [VMware.Vim.Folder] -and $location.Name -ne 'host'){

                $names += $location.Name

            }

            $location = Get-View -Id $location.Parent

        }

        until(-not $location.Parent)

        $names[-1]

    }} |

Sort datacenter, NumVM 


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

0 Kudos
abarakat
Contributor
Contributor

This did not work  it returned nothing.

0 Kudos
LucD
Leadership
Leadership

It works for me.

Do you actually have a layout Folder - Datacenter - Cluster - VMHost?

If not, it will indeed return nothing for the Folder property.

Perhaps provide a screenshot of the layout?


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

0 Kudos
abarakat
Contributor
Contributor

No cluster just folder, datacenter,host then VM's some vm's are behind resource_pools. In some cases there are clusters

pastedImage_0.png

Here is the output from your code  no out put for folder

Datacenter Name                         NumVM Folder

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

GLO        nyesx67.englab.juniper.net      77

GLO        nyinfra01.englab.juniper.net     8

GLO        nyinfra02.englab.juniper.net     5

fwlab-pdt  pdtbl-esx03.englab.junipe...    13

fwlab-pdt  pdtbl-esx02.englab.junipe...    22

fwlab-pdt  nyesx07.spglab.juniper.net      55

fwlab-perf nyesx39.spglab.juniper.net       4

fwlab      vsrxdevesx01.spglab.junip...    99

fwlab      vsrxdevesx02.spglab.junip...    60

fwlab      vsrxdevesx04.spglab.junip...    14

fwlab      vsrxdevesx08.spglab.junip...    22

fwlab      srx-forge-08.spglab.junip...     2

0 Kudos
LucD
Leadership
Leadership

Ok, got it.

Try like this

Get-VMHost |

Select @{N="Datacenter";E={Get-Datacenter -VMHost $_}},

    Name,

    @{N="NumVM";E={($_ | Get-VM).Count}},

    @{N='Folder';E={

        $location = Get-View -Id $_.ExtensionData.Parent

        $folder = ''

        do{

            if($location -is [VMware.Vim.Folder] -and $location.Name -ne 'host'){

                $folder = $location

            }

            $location = Get-View -Id $location.Parent

        }

        until($location.Name -eq 'Datacenters')

        $folder.Name

    }} |

Sort datacenter, NumVM


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

View solution in original post

0 Kudos
abarakat
Contributor
Contributor

Yup that did it thanks

0 Kudos