VMware Cloud Community
vmpwcliuser31
Contributor
Contributor
Jump to solution

Searching multiple vcenters

Hello all,

 

I'm have a PowerCLI script that will go through our decommissioning process and it works well enough; however, the main problem is that it's hard to know which of our vCenters the VM is in (so you'd have to know that before running the script).

My original thought was to do something like this:
$servername = Read-Host -Prompt 'Enter server name'

connect-viserver xyz | out-null

Get-VM -Name $servername

Then capturing the output and saying "if the VM isn't found in xyz vCenter, then connect to abc vCenter" and so on (we have at least 6 vCenters).

 

However, after doing some searching, would it be easier to connect to multiple vCenters as a default and then searching for the VM and somehow figuring out which vCenter it's in and then continue on with the decommissioning?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sorry, my bad.
I had a typo in that code, it should say ExtensionData, not Extensionata.
I corrected the code above.


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

You can find the vCenter a VM belongs to like this.
Connect to multiple vCenters, and then do

Get-VM |
Select Name,@{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}}


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

0 Kudos
vmpwcliuser31
Contributor
Contributor
Jump to solution

Okay, thanks for that; some questions for you:

 

1.) What's the best way to connect to multiple vCenters? 

I was testing out that tidbit of code you posted and currently had 2 lines of code that connected to 2 separate vCenters and it worked great.  I had then wanted to test more so I added a 3rd line connecting to a 3rd vCenter and tried finding a VM in the 3rd vCenter and that did not work (got the "VM with name x was not found".  

Confused, I commented out the first two connect-viserver lines, saved it, and then tried it again.  This time it worked, so I uncommented the first two lines and tried finding a VM in each of the vCenters, and would now get errors finding a VM in the first vCenter.

 

2.)  How can I use the output from the Get-VM command/script tidbit to continue on with my decommissioning steps?

Meaning, I run a script that connects to vCenters A, B, & C.  Once I it finds the vCenter, I need to continue on saying, "okay if it's in vCenter B, then do x..."

0 Kudos
LucD
Leadership
Leadership
Jump to solution

1. Not sure what is happening there.
Did you check what $global:defaultviservers shows after you connected to the 3th vCenter?

2. You can use the vCenter name in for example a switch.
Each case has a code block in which you can take appropriate action depending on the vCenter.
For example

$vm = Get-VM

switch(([uri]$vm.Extensionata.Client.ServiceUrl).Host){
    'VC1' {
        Write-Host "That VM is in VC1"
    }
    'VC2' {
        Write-Host "That VM is in VC2"
    }
    'VC3' {
        Write-Host "That VM is in VC3"
    }
}




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

0 Kudos
vmpwcliuser31
Contributor
Contributor
Jump to solution

Ah, I figured out the issue with my 1st question/issue...I didn't have the powercliconfig set to multiple:

Set-PowerCLIConfiguration -DefaultVIServerMode 'Multiple' 

 Once I set that, it worked just fine. 😉  Oops!

 

Will look at what you posted/suggested for #2 in a bit; thanks again for helping me! 🙂

0 Kudos
vmpwcliuser31
Contributor
Contributor
Jump to solution

Did a little testing today...

Originally, I had this:

Get-VM -Name $vm2decom |
Select Name,@{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}}

And that did show which vCenter the VM was in; I added the code snippet you posted earlier and originally didn't comment out the Select statement, and it worked the same as it did without the switch code snippet.

 

I commented out the Select statement so that it looked like this:

Get-VM -Name $vm2decom
#Select Name,@{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}}
switch(([uri]$vm2decom.Extensionata.Client.ServiceUrl).Host){
'VC-1' {
Write-Host -ForegroundColor Yellow "That VM is in VC-1"
}
'VC-2' {
Write-Host -ForegroundColor Yellow "That VM is in VC-2"
}
'VC-3' {
Write-Host -ForegroundColor Yellow "That VM is in VC-3"
}
'VC-4' {
Write-Host -ForegroundColor Yellow "That VM is in VC-4"
}
}

 

When I run the script then, it finds the VM, but it doesn't do the "Write-Host" part; only shows the VM name, powerstate, num cpus, and MemoryGB in a table format.

 

What obvious thing am I missing? 😕

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you check what the expression ([uri]$vm2decom.Extensionata.Client.ServiceUrl).Host returns?


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

0 Kudos
vmpwcliuser31
Contributor
Contributor
Jump to solution

To be honest, not sure how to do that...

 

I have my code as:

Get-VM -Name $vm2decom
switch(([uri]$vm2decom.Extensionata.Client.ServiceUrl).Host){
'VC-1' {
Write-Host -ForegroundColor Yellow 'hi'
}
}

And when I put in an actual VM name, it returns with information about that VM (Name, PowerState, Num CPUs, & MemoryGB).

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You need to store the VM object in a variable (as I showed in my earlier sample).
Just try like this

 

$vm = Get-VM -Name $vm2decom
Write-Host "$($vm.Name) is on $(([uri]$vm.Extensionata.Client.ServiceUrl).Host)"
switch (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) {
    'VC-1' {
        Write-Host -ForegroundColor Yellow 'hi'
    }
}

 


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

0 Kudos
vmpwcliuser31
Contributor
Contributor
Jump to solution

Ah, thank you for that explanation. 

 

So, this is what I have:

$vm = Get-VM -Name $vm2decom
Write-Host "$($vm.Name) is on $(([uri]$vm.Extensionata.Client.ServiceUrl).Host)"

switch(([uri]$vm.Extensionata.Client.ServiceUrl).Host){
'VC-1' {
Write-Host -ForegroundColor Yellow 'hi'
}
}

 

When I run the script to look for a VM named VLANTESTING, it finds it and says, "VLANTESTING is on"  (blank, nothing after it):

vlantesting.PNG

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, my bad.
I had a typo in that code, it should say ExtensionData, not Extensionata.
I corrected the code above.


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

0 Kudos
vmpwcliuser31
Contributor
Contributor
Jump to solution

Ha!  We both missed that, but thanks so much!  It's now working...time to keep playing around!  

 

Again, thanks for helping this newbie out.

0 Kudos