PowerCLI scripts to count the “VCPU and memory” and “to find VM” in VMware

Today as Part of practice in VSphere Power CLI  created two scripts.

Script 1 : This script is used to find the total number of virtual CPU and memory which is allocated to VM in environment. It will calculate them at whole cluster level.

#Create By Vikrant Aggarwal
foreach ($cluster in get-cluster)
{
write-host “Cluster:$($cluster.name)”
foreach ($esxhost in ($cluster | get-vmhost | sort name))
{
write-host “Host:$($esxhost.name)”
foreach ($vms in ($esxhost | get-vm | sort name))
{
$HostvCPU+=$vms.Numcpu
$HostMemory+=$vms.memorygb
}
write-host “Number of vCPU on host $($HostvCPU)”
$TotalNumvCPU+=$HostvCPU
$TotalMemory+=$HostMemory
$HostvCPU=0
$HostMemory=0
}
“———————————-“
write-host ” Total vCPU in cluster is $($TotalNumvCPU) “
write-host ” Total Memory in cluster is $($TotalMemory) GB “
}

Output of script look like below

PowerCLI C:\Windows\system32> C:\PowerShellscripts\cluster-name.ps1
Cluster:Shiv
Host:192.168.111.132
Number of vCPU on host 3
Host:192.168.111.133
Number of vCPU on host 0
———————————-
Total vCPU in cluster is 3
Total Memory in cluster is 3.015625 GB

 

Script 2 : In our VM environment we are facing difficulties to search the VM using search tab. Hence extending the concept of first script I created below script.

# Created By Vikrant Aggarwal

$vmlook=read-host “Enter the name of the VM”
write-host “VM name entered is $vmlook”
foreach ($cluster in (get-cluster|sort name))
{
foreach ($hostname1 in ($cluster|get-vmhost|sort name))
{
foreach ($vms in ($hostname1|get-vm|sort name))
{
if ($vms.name -eq $vmlook)
{
” ——————————————————“
” ” + $vmlook + ” has been located present on ” + $hostname1 + ” Cluster Name is ” + $cluster
}
}
}
}

 

Output of script look like below

PowerCLI C:\Windows\system32> C:\PowerShellscripts\vm_search.ps1
Enter the name of the VM: ttylinux-1
VM name entered is ttylinux-1
——————————————————
ttylinux-1 has been located present on 192.168.111.132 Cluster Name is Shiv

 

In next post I will try to cover how to calculate the statistics from VCenter Server 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s