Thursday 11 August 2011

Checks VMs with thin-provisioned disks

#Description: This function checks VMs with thin provisioned disks
#Purpose: Thin provisioned disks might over-subscribe DS usage, need to be monitored
#Designed to be added as function into $profile for quick-admin tasks, run from commandline


#Requires VCLI snapin -
www.vmware.com/support/developer/vcli/
#Author: Kirk Munro, extracted from VMware powerpack: http://www.powergui.org/entry.jspa?externalID=1802&categoryID=290
#Tested with: ESX4.1
#Last edit by: MsMiller on 11/08/2011
function func_Thin_Provisioned_VMDKs()
{
$noConnection = $true
if ($defaultVIServers) {
 foreach ($managedHost in $defaultVIServers) {
  $noConnection = $false
  Get-VM -Server $managedHost | Get-View | ForEach-Object {
   $vmname = $_.Name
   $uncommittedKB = ($_.Summary.Storage.Uncommitted/1024)
   $_.Config.Hardware.Device | Where {($_.GetType()).Name -eq "VirtualDisk"} | ForEach-Object {
    if($_.Backing.ThinProvisioned -eq $true){
     $_.PSObject.TypeNames.Insert(0,"$($_.PSObject.TypeNames[0])_Thin#VmwarePowerPackExtension")
     $_ `
      | Add-Member -MemberType NoteProperty -Name ThinProvisioned -Value $_.Backing.ThinProvisioned -PassThru `
      | Add-Member -MemberType NoteProperty -Name FileName -Value $_.Backing.FileName -PassThru `
      | Add-Member -MemberType NoteProperty -Name DeviceName -Value $_.DeviceInfo.Label -PassThru `
      | Add-Member -MemberType NoteProperty -Name VMName -Value $vmname -PassThru `
      | Add-Member -MemberType NoteProperty -Name UncommittedKB -value $uncommittedKB -PassThru `
      | Add-Member -MemberType NoteProperty -Name ManagedHost -Value $managedHost.Name -PassThru
    }
   }
  }
 }
}
if ($noConnection){
 Show-MessageBox -Text 'You must connect to one or more vSphere servers before you can run the Thin Provisioned VMDKs best practice query on those servers. Please click on the ''Managed Hosts'' node, connect to one or more of the vSphere servers you have configured there, and then try again.' -Caption 'Connection not established' -Buttons 'OK' -Icon 'Information' | Out-Null
}
}


 func_Thin_Provisioned_VMDKs | Select-Object -property 'VMName', 'FileName', 'DeviceName', 'ThinProvisioned', 'CapacityInKB', 'UncommittedKB'