xav197
Contributor
Contributor

Getting the Datacenter information of VirtualMachine using Get-View

Jump to solution

Hi,

Is there a faster way to get the datacenter name using Get-View and the viewtype VirtualMachine.

I found the following way:

Get-View -ViewType VirtualMachine -filter @{ "name" = "mtl1fsit02" } | Select-Object -Property Name,

@{ Label = "GuestOSName"; Expression = { $_.summary.guest.guestfullname } },

@{ Label = "Datacenter"; Expression = { (Get-view (Get-View (Get-view $_.parent).parent).Parent).name } }

Thanks guys

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

I think that calculated property to get the datacenter will not work in all situations.

It assumes your VMs are 3 levels down from the datacenter, which is not always the case.

I personally would use a loop, going up through the parents, until it find a datacenter object.

something like this

@{N="Datacenter";E={

   $parentObj = Get-View $_.Parent

   while($parentObj -isnot [VMware.Vim.Datacenter]({

      $parentObj = Get-View $parentObj.Parent

   }

   $parentObj.Name

}


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

View solution in original post

6 Replies
LucD
Leadership
Leadership

I think that calculated property to get the datacenter will not work in all situations.

It assumes your VMs are 3 levels down from the datacenter, which is not always the case.

I personally would use a loop, going up through the parents, until it find a datacenter object.

something like this

@{N="Datacenter";E={

   $parentObj = Get-View $_.Parent

   while($parentObj -isnot [VMware.Vim.Datacenter]({

      $parentObj = Get-View $parentObj.Parent

   }

   $parentObj.Name

}


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

xav197
Contributor
Contributor

Thanks LucD! 🙂

0 Kudos
xav197
Contributor
Contributor

Just to confirm that the script work with the LucD tip.

This will list all the VM with the properties: Name, OS Full Name and the Datacenter Name.

Get-View -ViewType VirtualMachine |

     Select-Object -Property Name,

          @{ Label = "OSName"; Expression = { $_.summary.guest.guestfullname } },

          @{ Label = "DataCenter"; Expression = {

               $parentObj = Get-View $_.Parent

               while ($parentObj -isnot [VMware.Vim.Datacenter])

               {$parentObj = Get-View $parentObj.Parent}

               $parentObj.Name} }

0 Kudos
dajbozerozum
Contributor
Contributor

You can also try it like below.

foreach($dc in Get-View -ViewType Datacenter -Property Name,VmFolder){

    foreach($vm in Get-View -ViewType VirtualMachine -Filter @{"Summary.Config.Template"="False"} -SearchRoot $dc.VmFolder -Property Name){

        $vm.Name;

        $dc.Name;

    }

}

Probably you will anyway need to pass through datacenter so why not to use loop with it?

0 Kudos
esxi1979
Expert
Expert

Small typo you had LucD​ for the closing ')' ...

below is corrected one

@{N="Datacenter";E={

   $parentObj = Get-View $_.Parent

   while($parentObj -isnot [VMware.Vim.Datacenter]){

      $parentObj = Get-View $parentObj.Parent

   }

   $parentObj.Name

}

0 Kudos
mhops
Enthusiast
Enthusiast
It was missing one more '}' at the end. Awesome expression! Thank you all! 

@{N="Datacenter";E={
        $parentObj = Get-View $_.Parent
            while($parentObj -isnot [VMware.Vim.Datacenter]){
                $parentObj = Get-View $parentObj.Parent
            }
        $parentObj.Name
    }
}
0 Kudos