the following code works but its a bit slow - it is slowed down by the method I use to get the cluster. Is there a way to get the cluster name of a VM using get-view . Using "$_.parent" has never worked for me. So I have to use "get-cluster" which slows things down. It takes over an hour for the script to complete with the get-cluster, without get-cluster it completes in 2 seconds.
(connect to the vcenter(s) before running)
& { foreach ($vCenterServer in $DefaultVIServers)
{
$VMHostTable= @{}
foreach ($VMHostView in (Get-View -Server $vCenterServer -ViewType HostSystem -Property Name))
{
$VMHostTable["$($VMHostView.MoRef.Value)"] = $VMHostView.Name # this is required to return the host that the VM is running on.
write-host "Processing :" $VMHostView.Name
}
Get-View -Server $vCenterServer -ViewType VirtualMachine -Filter @{"Config.Template"="False"} -Property Name, # get virtual machine (as opposed to esxi host)
parent,
Guest.HostName,
guest.net,
guest.toolsstatus,
Runtime.Host,
##runtime.cluster,
runtime.powerstate,
Guest.GuestFullName,
Config.Annotation,
config.datastoreurl,
Guest.GuestFullName,
Config.Hardware.MemoryMB,
Config.Hardware.NumCPU,
Config.Hardware.Device|
Select-Object -Property @{N="VM";E={$_.Name}},
@{N="Guest OS";E={$_.Guest.GuestFullName}},
@{N="DNS";E={$_.Guest.HostName}},
@{N="Datastore";E={$_.config.datastoreurl.name}},
@{N="vCenter";E={$vCenterServer.Name}},
@{N="VMHost";E={$VMHostTable["$($_.Runtime.Host.Value)"]}}, # works
@{N="Cluster";E={[string]$cluster=get-cluster -vm $_.name | select name;[string]$object1=$cluster.replace("@{Name=","");[string]$object2=$object1.replace("}","");$object2}} # works very slowly
}
} |
Export-Csv -Path "VMsInfo-$(Get-Date -UFormat '%Y%m%d-%H.%M.%S').csv" -delimiter "|" -NoTypeInformation
------------------------------
NetworkAdmin9845
------------------------------