VMware Cloud Community
MCioe
Enthusiast
Enthusiast
Jump to solution

What is the best practice to handle duplicate results due to multiple sessions

I am working with PowerCLI 5.0.  Quite often I find I have a few sessions open at the same time connected to vCenter and to a server which hosts a given VM.  I ran into a problem when I tried the Add-USBController cmdlet from the VMware vSphere PowerCLI Reference book.

The following lines are (roughly) in the cmdlet from the book:

     $vmView get-vm "test" | Get-View

     $vmView.ReconfigVM_Task($spec)

I got the following error "Method invocation failed because [System.Object[]] doesn't contain a method named 'ReconfigVM_Task'" etc.

It turns out that I was getting an array of vmViews for "test" (instead of just one), because I had a connection to both vCenter and the host.  Once I deleted the host sessions, the script worked fine.

So now my question - what's the best approach to mitigate this in my scripts? Minimally, I guess I have to make sure I only received one object from the get-view. should I delete all duplicates? kill all sessions other when I start the script??

Thanks,

Maureen

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The best method is to use the Server parameter on the cmdlets where it is available, that way you can limit the returned objects to that vSphere server.

If you have multiple sessions to the same vSphere server open, you could pipe the result to a Select-Object cmdlet.

Like this

Get-VM -Name MyVM -Server $vcenter | Select -First 1


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The best method is to use the Server parameter on the cmdlets where it is available, that way you can limit the returned objects to that vSphere server.

If you have multiple sessions to the same vSphere server open, you could pipe the result to a Select-Object cmdlet.

Like this

Get-VM -Name MyVM -Server $vcenter | Select -First 1


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

0 Kudos
MCioe
Enthusiast
Enthusiast
Jump to solution


Luc,

     Thanks for the tip, I had a feeling it was something like that.  I really wanted to get that Powershell error/solution out on a messageboard as it took me a long time to figure out what the heck was going wrong.  Smiley Happy

0 Kudos