VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Remove virtual USB controller

Hello,

The script below worked in my lab but doesn't seem to be working in my other environment. The goal is to remove all virtual USB controllers from all VMs:

 

$vms = Get-VM | Where {$_.ExtensionData.Config.Hardware.Device.DeviceInfo.Label -match "usb"} # | Select Name, PowerState, Folder
foreach ($vm in $vms) {
Get-VM | Get-USBDevice | Remove-USBDevice
}

However when I go to confirm that the virtual USB controller has been removed using this command I'm still seeing the same VMs:

Get-VM | Where {$_.ExtensionData.Config.Hardware.Device.DeviceInfo.Label -match "usb"} # | Select Name, PowerState, Folder

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

No, not afaik.

To remove all USB devices, including the controllers, you can just update the Where-clause.
Like the one you already used to check.

$vm.Config.Hardware.Device.Where({$_.DeviceInfo.Label -match "usb"}) |


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

Are you sure those are not the USB controllers that you are seeing?
What kind of devices are actually returned?


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

0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

I see now it's not working in my test lab either.  It appears:

Get-VM | Get-USBDevice | Remove-USBDevice

This command is only going to eject any USB devices. My actual goal is to remove the USB controller entirely, looks like you provided a similar solution here:
Workarounds for VMSA-2020-0026.1 - Multiple removi... - VMware Technology Network VMTN

My preference is to remove the virtual USB controller just because it seems a bit cleaner and simpler than only removing XHCI USB  as described in the post.

Is there any additional danger to removing all virtual USB controllers? I can't see any reason not to remove them all but would appreciate your insight.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, not afaik.

To remove all USB devices, including the controllers, you can just update the Where-clause.
Like the one you already used to check.

$vm.Config.Hardware.Device.Where({$_.DeviceInfo.Label -match "usb"}) |


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

mtnbkr0918
Enthusiast
Enthusiast
Jump to solution

I'm trying to REMOVE the USB controller but I'm getting this error. 

$vms = Get-VM | $vm.Config.Hardware.Device.Where({$_.DeviceInfo.Label -match "usb"}) | Select Name, PowerState, Folder
At line:1 char:17
+ ... = Get-VM | $vm.Config.Hardware.Device.Where({$_.DeviceInfo.Label -ma ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

foreach ($vm in $vms) {
Get-VM | Get-USBDevice | Remove-USBDevice
}




I tried the original command but it will not remove the controller since it is only removing the USB. 

$vms = Get-VM | Where {$_.ExtensionData.Config.Hardware.Device.DeviceInfo.Label -match "usb"} # | Select Name, PowerState, Folder
foreach ($vm in $vms) {
Get-VM | Get-USBDevice | Remove-USBDevice
}

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to use the script from the thread in the link, combined with the where-clause.

Get-View -ViewType VirtualMachine -Property Name,'Config.Hardware.Device' -PipelineVariable vm | 
ForEach-Object -Process {
    $spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec

    $vm.Config.Hardware.Device.Where({$_.DeviceInfo.Label -match "usb"}) |
    ForEach-Object -Process {
        $devSpec = New-Object -TypeName VMware.Vim.VirtualDeviceConfigSpec
        $devSpec.Device = $_
        $devSpec.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove
        $spec.DeviceChange += $devSpec
    }
    if($spec.DeviceChange -ne $null){
        $vm.ReconfigVM($spec)
    }
}


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