I’ve recently had a ton of requests for information about specific VMs. They want to know how many disks they have, CPU count, how much RAM, and which environment the VM resides in.
Instead of constantly searching vCenter, I wrote this quickly during the meeting to query multiple servers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#only need one parameter here param([string[]]$servers = "servers") #add vmware snap-in Add-PSSnapin VMware.* -erroraction silentlycontinue #typical usage statements function usage(){ Write-host -foregroundcolor green `n`t"This script is used to retreive info for all servers provided." Write-host -foregroundcolor green `n`t"It pulls VM Host & cluster info, OS provided in vCenter, number of CPUs & RAM." Write-host -foregroundcolor green `n`t"Specify -servers in singular or in an array, like this:" write-host -foregroundcolor yellow `n`t`t"Get-VMInfo.ps1 -servers (`"server1`",`"server2`",`"server3`")" Write-host -foregroundcolor green `n`t"or" write-host -foregroundcolor yellow `n`t`t"Get-VMInfo.ps1 -servers server1" `n Write-host -foregroundcolor green `n`t"or" write-host -foregroundcolor yellow `n`t`t"Get-VMInfo.ps1 server1" `n } #the main guts of the scripts function GetInfo(){ foreach($server in $servers){ #lets check and see if we're already connected somewhere if($global:DefaultVIServer){disconnect-viserver -confirm:$false} connect-viserver vcentername > $NULL 2>&1 $vm = get-vm $server -erroraction silentlycontinue if ($vm -ne $null){ $vcls = get-cluster -vm $server $vdc = get-datacenter -vm $server write-host -foregroundcolor green `n "Server $server" write-host -foregroundcolor green `n "OS is" $vm.Guest.OSFullName write-host -foregroundcolor green `n "Running on host" $vm.vmhost "in the $vcls cluster in the $vdc Datacenter" if ($vm.memorymb -gt 1024){ $ram = [math]::round($vm.MemoryMB/1024, 0) write-host -foregroundcolor green `n "It has" $VM.HardDisks.Count "virtual disks," $vm.NumCPU "CPUs, and $ram GB of RAM" `n } else{write-host -foregroundcolor green `n "It has" $VM.HardDisks.Count "virtual disks," $vm.NumCPU "CPUs, and" $vm.memorymb "MB of RAM" `n} } elseif ($vm -eq $null){write-host -foregroundcolor red `n "Cannot find server in vCenter" `n;break} if($global:DefaultVIServer){disconnect-viserver -confirm:$false -erroraction silentlycontinue} } } #making sure we have parameters If($servers -eq "servers"){ usage break } getinfo |
I hard-coded my vcenter hostname in there, so you’ll need to change that line
1 |
connect-viserver vcentername > $NULL 2>&1 |
Usage is like this:
1 |
Get-VMInfo.ps1 servername |
or
1 |
Get-VMInfo.ps1 ("server1","server2","server3") |
And expected output is like this:
Server Server1
OS is Red Hat Enterprise Linux 4 (32-bit)
Running on host esxihost1 in the cluster1 cluster in the vdatacentername Datacenter
It has 1 virtual disks, 1 CPUs, and 2 GB of RAM
Not real sophisticated, but works…
Hi Luke, the script is brilliant, thanks!
On the other hand I was thinking, if you can export details to CSV file which can give little more info related to the VMs.
VMname, VMhost, Cluster, Datacenter, vCPU, MEM, GuestOS, Disk1 Datastore, Disk1 Path, Disk1 Size…. DiskX Datastores, DiskX Path, DiskX size, VMNIC1 Mac, VMNIC1 Port group, VMNICX Mac, VMNICX Port group, Notes.
This would give all the required info to the requester.
I agree, I could go a lot deeper with it and export to CSV. I wrote this just to fill a quick void while I was in a meeting and need quick info on one VM here and there.