You can do something like this.
But be aware that this will not report VMHost, Clusters and Datacenters that do not have any VM in them.
foreach($dc in Get-Datacenter){
foreach($cluster in Get-Cluster -Location $dc){
foreach($esx in Get-VMHost -Location $cluster){
Get-VM -Location $esx |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='VMhost';E={$esx.Name}},Name
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can do something like this.
But be aware that this will not report VMHost, Clusters and Datacenters that do not have any VM in them.
foreach($dc in Get-Datacenter){
foreach($cluster in Get-Cluster -Location $dc){
foreach($esx in Get-VMHost -Location $cluster){
Get-VM -Location $esx |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='VMhost';E={$esx.Name}},Name
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
HI LucD,
Thanks for your prompt response, You always rock
Let me try the script and let you know the output!
-Subash.
Hi LuCD,
The Script is working like a charm, However i have two concerns to complete my requirement.
1. VM information is missing to generate along with the output (datacenter, cluster, hosts)
2. Can't able to save in a CSV file
Need guidance on the above areas, Thanks in advance!
-Subash.
1. Which VM information do you want to include in the report, beside the VM name ?
2. A ForEach loop doesn't place anything in the pipeline, but you can fix that by using a call (&) operator.
Like this
&{foreach($dc in Get-Datacenter){
foreach($cluster in Get-Cluster -Location $dc){
foreach($esx in Get-VMHost -Location $cluster){
Get-VM -Location $esx |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='VMhost';E={$esx.Name}},Name
}
}
}} | Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
1. The vm name whichever reside on hosts - may be like this
Datacenter | Clsuter | Hosts | VM |
---|---|---|---|
DC1 | Cluster-1 | host1 | vm1, vm2, vm3... |
Clsuter-2 | host2 | vm4,vm5,vm6... |
2. I will do test and give you an update.
Thanks
-Subash
Try this variation
&{foreach($dc in Get-Datacenter){
foreach($cluster in Get-Cluster -Location $dc){
Get-VMHost -Location $cluster |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
Name,
@{N='VM';E={[string]::Join(',',(Get-VM -Location $_ | Select -ExpandProperty Name))}}
}
}} | Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LuCD,
Indeed! I appriciate you efforts, what i was exactly expected was done finally - The script is really amazing!!!
Million Thanks for your valuble support!
-Subash.
Hi LucD,
In addition to your script, I'm trying to incorporate the following properties:
Bonus (not a must!!)
As always, you help and support is kindly appreciated.
That would mean the report layout has to change.
Instead of 1 ESXi per line, you will then have 1 VM per line.
But that would also mean you will have a lot of repetitive info (for the ESXi node) on each VM line.
Or do you have another layout in mind?
Perhaps separate CSV or worksheets?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The requirement is to list VMs per Datacenter, Cluster, ESXi host and then provide the following:
VM O/S
VM vCPU
VM RAM (GB)
VM Power State
VM Folder
Having knocked up the below script, the cluster property does not appear in final output:
Get-Cluster | Get-VM | Select-Object Name, PowerState, NumCpu, MemoryGB, Guest, Folder |
Export-Csv -Path "c:\Scripts\VM_Config-1.csv" -NoTypeInformation -UseCulture
----------------------------------------------------------------------------------------
PS.
I'm unable to tie in cluster with Datacenter property using the above example?
The pipeline variable helps in that case.
Get-Datacenter -PipelineVariable dc |
Get-Cluster -PipelineVariable cl -ErrorAction SilentlyContinue |
Get-VM -ErrorAction SilentlyContinue |
Select-Object @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cl.Name}},
Name, PowerState, NumCpu, MemoryGB, Guest, Folder
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Cheers for that LucD,
For some strange reason, the script only outputted info against one DC in one of the two VCs cf which numerous DCs, Clusters, VMs are held.
add-pssnapin VMware.VimAutomation.Core
$Credential = Get-Credential
Connect-VIServer -Server VC1,VC2 -Credential $Credential
Get-Datacenter -PipelineVariable dc |
Get-Cluster -PipelineVariable cl -ErrorAction SilentlyContinue |
Get-VM -ErrorAction SilentlyContinue |
Select-Object @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cl.Name}},
Name, PowerState, NumCpu, MemoryGB, Guest, Folder |
Export-Csv -Path "c:\Scripts\Cluster_VMs.csv" -NoTypeInformation -UseCulture
I suspect that would require another forach loop (on the outside of the current ones), that loops through all the vCenters.
Are you connected to all vCenters (check $global:defaultviservers), or are you running in linked-mode?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
DefaultVIServerMode is set to Multiple.
Can you kindly do the honours my friend? 😉
Having added the forach loop, this didn't seem to make make any difference.
Having gone through script, piece by piece, it would appear issue centered around Datacenter property!! To get around this, I stripped out the aforementioned references and was then able to get output across vCenter Server instances, minus named property.
In any case, thanks for your input as always.
This great! Is there a way to add the IP address of the vm and last powered on date?
Try like this.
But be aware that the presence of the IP address depends on VMware Tools running.
Also note that the retrieval of events might be the script considerably slower.
foreach($dc in Get-Datacenter){
foreach($cluster in Get-Cluster -Location $dc){
foreach($esx in Get-VMHost -Location $cluster){
Get-VM -Location $esx |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='VMhost';E={$esx.Name}},Name,
@{N='IP';E={$_.Guest.IPAddress -join '|'}},
@{N='Last PowerOn';E={
Get-VIEvent -Entity $_ -MaxSamples ([int]::MaxValue) |
where{$_ -is [VMware.Vim.VmPoweredOnEvent]} |
Sort-Object -Property CreatedTime -Descending |
Select -First 1 -ExpandProperty CreatedTime
}}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD. This is great but I realized since we clear the log retention is 30 the powered on will not work for this purpose. Can the powerstate be added and then exported to a CVS?
Thanks so much!
Sure, try like this
$report = foreach($dc in Get-Datacenter){
foreach($cluster in Get-Cluster -Location $dc){
foreach($esx in Get-VMHost -Location $cluster){
Get-VM -Location $esx |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='VMhost';E={$esx.Name}},Name,PowerState,
@{N='IP';E={$_.Guest.IPAddress -join '|'}}
}
}
}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference