VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Get-vm but include vCenter name

Hello, 

I am connected to multiple vCenters trying to find all the VMs in my environment which have virtual USB controllers. The command is working, but how to I include which vCenter the VM resides in with my results?

 

$vms = Get-View -ViewType VirtualMachine -Property Name,'Config.Hardware' | Where-Object { $_.Config.Hardware.Device.Where({$_.gettype().name -match 'VirtualUSBController'}) } | Select-Object -ExpandProperty Name

foreach ($vm in $vms){
    get-vm -name $vm | select Name,GuestId,@{N="Folder";E={$_.Folder.Name}} | Export-Csv e:\output\usbcontrollervms.csv
}

 

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this


@dbutch1976 wrote:

Hello, 

I am connected to multiple vCenters trying to find all the VMs in my environment which have virtual USB controllers. The command is working, but how to I include which vCenter the VM resides in with my results?

 

$vms = Get-View -ViewType VirtualMachine -Property Name,'Config.Hardware' | Where-Object { $_.Config.Hardware.Device.Where({$_.gettype().name -match 'VirtualUSBController'}) } | Select-Object -ExpandProperty Name

foreach ($vm in $vms){
    get-vm -name $vm | select Name,GuestId,@{N="Folder";E={$_.Folder.Name}} | Export-Csv e:\output\usbcontrollervms.csv
}

 

 


 

Get-View -ViewType VirtualMachine -Property Name,'Config.Hardware','Config.GuestId',Parent |
Where-Object { $_.Config.Hardware.Device.Where( { $_.gettype().name -match 'VirtualUSBController' }) } |
Select-Object Name,
  @{N='GuestId';E={$_.Config.GuestId}},
  @{N = "Folder"; E = {
    (Get-View -Id $_.Parent).Name } },
  @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}} | 
  Export-Csv -Path e:\output\usbcontrollervms.csv -NoTypeInformation -UseCulture

 


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this


@dbutch1976 wrote:

Hello, 

I am connected to multiple vCenters trying to find all the VMs in my environment which have virtual USB controllers. The command is working, but how to I include which vCenter the VM resides in with my results?

 

$vms = Get-View -ViewType VirtualMachine -Property Name,'Config.Hardware' | Where-Object { $_.Config.Hardware.Device.Where({$_.gettype().name -match 'VirtualUSBController'}) } | Select-Object -ExpandProperty Name

foreach ($vm in $vms){
    get-vm -name $vm | select Name,GuestId,@{N="Folder";E={$_.Folder.Name}} | Export-Csv e:\output\usbcontrollervms.csv
}

 

 


 

Get-View -ViewType VirtualMachine -Property Name,'Config.Hardware','Config.GuestId',Parent |
Where-Object { $_.Config.Hardware.Device.Where( { $_.gettype().name -match 'VirtualUSBController' }) } |
Select-Object Name,
  @{N='GuestId';E={$_.Config.GuestId}},
  @{N = "Folder"; E = {
    (Get-View -Id $_.Parent).Name } },
  @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}} | 
  Export-Csv -Path e:\output\usbcontrollervms.csv -NoTypeInformation -UseCulture

 


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

Reply
0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Hey LucD,

Not sure what's wrong but folder is coming up blank now, was working before.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, typo in that part.
The code above is corrected


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

Reply
0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

That's got it, thanks LucD!

Reply
0 Kudos