Tuesday 29 March 2016

Automation of resource pool allocation

Operations teams are struggling to manage the resource pool. We have been either migrating or decommissioning lots of VM's everyday. We need to adjust the resource pool memory and CPU after every migration and decommission of VM's. 

I have written the below powerCLI script to automate this task. This script will identify only discrepancy resource pool by comparing the resource pool memory and CPU share values with vCPU and memory count of VM's  inside the resource pool.

By this way you can use an unused memory and CPU shares to another resources. 

I have asked you to type the details of $VIserver, $SMTPserver, $mailFrom and $mailTo in the below script. Please add the requested details before running. You will get the discrepancy report of resource pool to  your given "$mailTo" address after the completion of running script.  



#######################################################

# Created By = Sankar Munirathinam


########################################################


[string]$head = @'
<style type = "text/css">
p {font-family: Calibri;}
h1,h2,h3 {font-family: Calibri;}
ol {font-family: Calibri;}
li {font-family: Calibri;}
table {width: 50%;}
table, th, td {border: 1px solid black;border-collapse: collapse; font-size:10pt}
th {background-color:Orange; color:black}
td, th {padding: 3px; text-align:center;}
</style>
'@



$VIserver = "Type your datacenter here"

Connect-VIserver $VIserver
 $report = @()
 
 $datacenters = get-datacenter 

 
 foreach($datacenter in $datacenters)
 {
 
 $clusters = get-datacenter $datacenter | get-cluster
 
 foreach($cluster in $clusters)
 {
 $Resourcepool = get-cluster $cluster | Get-ResourcePool | select -ExpandProperty Name
 
 foreach($resource in $Resourcepool)
 {
 
  $numvcpus = 0
    $totalmemory = 0
    $numvms = 0
 $pool = get-resourcepool $resource -location $cluster
  $cpuShares = $pool.Numcpushares
$MemoryShares = $pool.Nummemshares
 
 [int]$ClusterCPUShares = $cpuShares/1000
 [int]$clusterMemShares = $MemoryShares/1000
 
$VMs = $pool | get-vm

 
 foreach($vm in $VMs)
 {
    #count number of CPUs
        $NumvCPUs += ($VM).NumCpu
        #count how much memory is allocated
        $TotalMemory += ($vm).MemoryGB
        #count number of VMs
        $NumVMs += 1
 
 }

  if((($numvcpus -lt $ClusterCPUShares) -or ($numvcpus -gt $ClusterCPUShares)) -or (($totalmemory -lt $clusterMemShares) -or ($totalmemory -gt $clusterMemShares)))
 {
  $totalmemory = "{0:N2}" -f $totalmemory
 
 $object = New-Object -TypeName Psobject
 $object | Add-Member -MemberType NoteProperty -Name "DataCenter" -Value $datacenter
 
$object | Add-Member -MemberType NoteProperty -Name "Cluster" -Value $cluster
$object | Add-Member -MemberType NoteProperty -Name "ResourcePool" -Value $resource



Write-Host "--------------------------------------------"
Write-Host "Cluster Name : $cluster" -ForegroundColor White
Write-Host "Resource Pool Name : $resource " -ForegroundColor White
Write-Host "Resource Pool CPU Shares : $cpushares" -ForegroundColor White
Write-Host "Total VM's vCPU Count : $numvcpus" -ForegroundColor White
Write-Host "--------------------------------------------"

$object | Add-Member -MemberType NoteProperty -Name "ResourcePool Cpu Shares" -Value $cpuShares
$object | Add-Member -MemberType NoteProperty -Name "Total VM's vCpu Count" -Value $numvcpus

Write-Host "--------------------------------------------"
Write-Host "Cluster Name : $cluster" -ForegroundColor green
Write-Host "Resource Pool Name : $pool" -ForegroundColor green
Write-Host "Resource Pool Memory Shares : $Memoryshares" -ForegroundColor green
Write-Host "Total VM's Memory : $TotalMemory GB" -ForegroundColor green
Write-Host "--------------------------------------------"
$object | Add-Member -MemberType NoteProperty -Name "ResourcePool Memory Shares" -Value $MemoryShares
$object | Add-Member -MemberType NoteProperty -Name "Total VM's Memory in GB" -Value $totalmemory
$report += $object
}

}
}
}


$final = $report | ConvertTo-Html -Head $head


$SMTPServer = "Type your smtp address"
$mailto = ""
$mailfrom = ""
$subject = "Report of ResourcePool $VIserver"

[string]$Message = @"
<!DOCTYPE html>
<html>
<body style = "font-family: Calibri"; font-size:8px>
<font size = "3">

<p><center>$final</center></p>
<p>Regards</p>
<p><strong>Operation Team</strong></p>
</font>
</body>
</html>
"@

send-mailmessage -to $mailto -Subject $subject  -from $mailfrom  -smtpserver $SMTPServer -body $Message -BodyAsHtml
 ##############################################