Total disk usage for a group of VM’s

The Inventory view in vCenter allow us to organize our VM in Folders according to project names, Machine roles, etc. It should be a good practice in order to keep our Virtual Machines classified. Another reason is I can take advantage of PoweCLI to perform a specific action on some VM that belong to a specific Folder.

Example : Let’s say I want to know how much storage Production VMs are using. Assuming they all cling to one or more folder named  ‘Production’,  it will take long to check each VM in vCenter so here’s a snippet that can make it easier and faster :

########################################################
# Name: StorageChecker
# Desc: Checks the amount of disk used for a specific machine Container
# Date: 02/05/2010
# Auth: Alberto Ruiz-Zorrilla
########################################################
# Check Parameter
Param($PARAM1,$PARAM2)
If ($PARAM1 -eq $Null ) {
CLS
Write-Host “Usage: ”
Write-Host “StorageChecker <FolderName>”
Exit     }

# PowerCLI Header CmdLets
$VMwareLoaded = $(Get-PSSnapin | ? {$_.Name -like “*VMware*”} ) -ne $null
If  (!$VMwareLoaded)  { Add-PSSnapin -Name “VMware.VimAutomation.Core” }

# Connect VI Server
Connect-VIserver -server | OUT-NULL

# Main Module
cls
$DiskSizeKB=0
$RAMSizeMB=0

Get-Folder | ForEach-Object {$FolderList += $_.Name}

if ($FolderList.Contains($PARAM1) -eq “True” )  {

$VMList =  Get-VM -location (Get-Folder -name “$PARAM1″)

Get-HardDisk -vm $VMList | ForEach-Object { $DiskSizeKB += $_.CapacityKB }
Get-VM $VMList | ForEach-Object { $RAMSizeMB += $_.MemoryMB }

$DiskSizeMB = $DiskSizeKB / 1024
$TotalDiskUsageInGB = [math]::Round((( $DiskSizeMB + $RAMSizeMB ) /1024))

Write-Host ”  ” $VMList.count ” Virtual Machine(s) Found”
Foreach ($V in $VMLIST) {Write-Host -ForeGroundColor White ”      + ” $V.Name}

Write-Host -ForegroundColor Green “The Total Disk usage for $PARAM1 is ……………… ” $TotalDiskUsageInGB  ” Gb.”
Write-host -ForeGroundColor Green “Including swap files for a Total RAM  of …………..” $($RAMSizeMB/1024)   ” Gb.”
}
else {
Write-Host -foregroundcolor Red ”    + ERROR: Folder “[$PARAM1]” does NOT Exist !”
}
# End Main

#  Close VI Session
Disconnect-VIServer -Confirm:$False

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

And most of that… it Works ! 🙂

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