Thursday 11 August 2011

Checks VMs for RDMs (raw device mappings)

#Description: This function checks VMs for Raw device mappings
#Purpose: Raw device mappings limit the vmotion functionality

#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_Raw_Device_Mappings_(RDMs)()
{
$noConnection = $true
if ($defaultVIServers) {
 foreach ($managedHost in $defaultVIServers) {
  $noConnection = $false
  Get-VM -Server $managedHost | Get-View | ForEach-Object {
   $vmname = $_.Name
   $_.Config.Hardware.Device | Where {($_.GetType()).Name -eq "VirtualDisk"} | ForEach-Object {
    if(($_.Backing.CompatibilityMode -eq "physicalMode") -or ($_.Backing.CompatibilityMode -eq "virtualMode")){
     $_.PSObject.TypeNames.Insert(0,"$($_.PSObject.TypeNames[0])_RDM#VmwarePowerPackExtension")
     $_ `
      | Add-Member -MemberType NoteProperty -Name RDMType -Value $_.Backing.CompatibilityMode -PassThru `
      | Add-Member -MemberType NoteProperty -Name StubFile -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 ManagedHost -Value $managedHost.Name -PassThru
    }
   }
  }
 }
}
if ($noConnection){
 Show-MessageBox -Text 'You must connect to one or more vSphere servers before you can run the Raw Device Mappings (RDMs) 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_Raw_Device_Mappings_(RDMs) | Select-Object -property 'VMName', 'DeviceName', 'RDMType', 'StubFile', 'CapacityInKB'