VMware Cloud Community
MitchOVM
Contributor
Contributor

Needing help with finding VM's that are missing Virtual USB Controller

I am needing help with a script that I am modifying from vCheck.  In my environment, I have to have a virtual usb controller in every vm.  So, I am trying to modify the script to look for missing virtual usb controllers.  Below is the script:

# Start of Settings

# Find wanted virtual hardware

$wantedHardware = "VirtualUSBController"

# End of Settings

$vWantedHw = @()

foreach ($vmguest in $FullVM) {

    $vmguest.Config.Hardware.Device | where {$_.GetType().Name -eq $wantedHardware} | %{

        $myObj = "" | select Name,Label

        $myObj.Name = $vmguest.name

        $myObj.Label = $_.DeviceInfo.Label

        $vWantedHw += $myObj

    }

}

$vWantedHw | Sort Name

Where I am getting stuck is at the Config.Hardware.Device -eq part.  Running it gives me all VM's that have the usb controller.  If I set it to -noteq it gives me everything that is attached to the VM.  All I want to do is find and list any VM's that do not have the virtual usb controller.  Any help is greatly appreciated.  Also, does anyone know where I can find any documentation on these sub-commands (-eq) on the cmdlets?

0 Kudos
4 Replies
jpsider
Expert
Expert

I think what you might need to do is  "-ne" instead of  "-eq".

Here is a link to the powershell operators: Comparison operators | PowerShell | SS64.com

Let me know if this helps.

0 Kudos
LucD
Leadership
Leadership

The -is operator allows you to test on the type of an object.

You can do this

$vmguest.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualUSBController]} | %{


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MitchOVM
Contributor
Contributor

LucD,

Using your example the script did return my VM's that have the usb controller attached.  However, what I am looking for is any VM that does not have the usb controller attached.  I do like how you streamlined the line.

Thanks

0 Kudos
LucD
Leadership
Leadership

Got it, try something like this.

It will list the name of each VM that doesn't have an USB Controller.

It use the fact the $null evaluates as $false in a Where-clause, and the ! (-not) operator negates this.

In other words, let through all VM that do not have a device that is a USB Controller

Get-VM |

where{!($_.ExtensionData.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualUSBController]})} |

Select Name


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos