Thursday, 11 August 2011

Checks for VMs with mounted CDroms

#Description: This powershell function checks for VMs with mounted CDroms
#Purpose: If CDroms are mounted on VM, you can't VMotion the machine
#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_VMs_with_CDROM_Connected()
{
$noConnection = $true
if ($defaultVIServers) {
 foreach ($managedHost in $defaultVIServers) {
  $noConnection = $false
  Get-VM -Server $managedHost | ForEach-Object {
   $vmname = $_.Name
   $cd = $_ | Get-CDDrive | where {$_.ConnectionState.Connected -eq $true}
   if ($cd) {
    $cd.PSObject.TypeNames.Insert(0,"$($cd.PSObject.TypeNames[0])#VmwarePowerPackExtension")
    $cd `
     | Add-Member -MemberType NoteProperty -Name VM -Value $vmname -PassThru `
     | Add-Member -MemberType NoteProperty -Name Connected $cd.ConnectionState.Connected -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 VMs with CDROM Connected 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_VMs_with_CDROM_Connected | Select-Object -property 'VM', 'Name', 'Connected', 'IsoPath', 'HostDevice', 'RemoteDevice'