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.

#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

connect-viserver vcentername > $NULL 2>&1

Usage is like this:

Get-VMInfo.ps1 servername

or

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…