-
1. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
LucD Apr 23, 2015 12:49 AM (in response to suboss87)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
}
}
}
-
2. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
suboss87 Apr 23, 2015 3:21 AM (in response to LucD)HI LucD,
Thanks for your prompt response, You always rock
Let me try the script and let you know the output!
-Subash.
-
3. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
suboss87 Apr 23, 2015 8:54 AM (in response to suboss87)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.
-
4. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
LucD Apr 23, 2015 9:03 AM (in response to suboss87)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
-
5. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
suboss87 Apr 23, 2015 9:15 AM (in response to LucD)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
-
6. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
LucD Apr 23, 2015 10:41 PM (in response to suboss87)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
-
7. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
suboss87 Apr 24, 2015 9:42 AM (in response to LucD)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.
-
8. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
DaddyDee Mar 13, 2017 2:51 AM (in response to LucD)Hi LucD,
In addition to your script, I'm trying to incorporate the following properties:
- VM O/S
- VM vCPU
- VM RAM (GB)
- VM Power State
- VM Folder
Bonus (not a must!!)
- ESXi Model
- VM Guest Count on each Physical Host
As always, you help and support is kindly appreciated.
-
9. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
LucD Mar 13, 2017 3:21 AM (in response to DaddyDee)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? -
10. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
DaddyDee Mar 13, 2017 3:55 AM (in response to LucD)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?
-
11. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
LucD Mar 13, 2017 4:52 AM (in response to DaddyDee)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
-
12. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
DaddyDee Mar 13, 2017 6:59 AM (in response to LucD)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
-
13. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
LucD Mar 13, 2017 7:57 AM (in response to DaddyDee)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?
-
14. Re: Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.
DaddyDee Mar 13, 2017 8:18 AM (in response to LucD)DefaultVIServerMode is set to Multiple.
Can you kindly do the honours my friend? ;-)