Find VMs with unwanted devices

Posted by

Introduction:

As an administrator for a vSphere environment you may come across situation where you have a lot of  VMs configured with unnecessary hardware like USB device, Floppy disk or a serial  device.

The presence of such devices can impact the efficiency of DRS leading to the imbalanced cluster. In a large-scale environment, it can be difficult to find and fix such VMs. The code below provides an ability to create a csv report for the device specified ($hwtype)

Code:

Start-Transcript -Append -Path "$env:USERPROFILE\Documents\usbfinder.log"
$vCenterIP = Read-Host "Enter vCenter IP or Name where the operation needs to be executed"
Connect-VIServer $vCenterIP
echo "VM Name,HW Label,HW Summary" | out-file "$env:USERPROFILE\Documents\VMsWithUsb.csv"
$VMs = Get-VM
#update this variable to change the type of hardware “floppy" or "serial" or "usb”
$hwtype = “usb”
foreach ($VM in $vms)
{
$VMname = $VM.Name
$hardware=(Get-VM $VMname | Get-View).Config.Hardware.Device | Select -Expand DeviceInfo | Select Label, Summary | where {$_.Label –match $hwtype}
$Lable = $hardware.Label
$Summary = $hardware.Summary
if ($Lable -and $Summary){
echo "$VMname,$Lable,$Summary" | out-file -Append "$env:USERPROFILE\Documents\VMsWithUsb.csv"
}
}
Disconnect-VIServer -Server $vCenterIP -Confirm:$false
stop-Transcript