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
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
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
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.
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
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
}
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