xSyteck
Enthusiast
Enthusiast

DefaultVIServer -match -notmatch bug ?

Jump to solution

Hello Community,

I recreate my topic after some research. First of all I'm working on a CentOS7 with a PowerShell 6.7 package & a PowerCli 11.0.0 module.

I've been through an issue with my powercli script that check if I'm already connected to the vSphere I want to use. I can connect to multiple vSphere so this change some things.

So the problem is that sometimes I need to use $Global:DefaultVIServer(s) or $Global:global:DefaultVIServer(s) to match or not match with a variable.

This is a table with on the left the command executed (in a script or manually) and on the right the result or a '-' if there no output.

Note: PowerCli-Connection is a personnal script.

Script / Manual executionResult

'1 ';$Global:global:DefaultVIServer

'2 ';$Global:global:DefaultVIServers

'3 ';$Global:DefaultVIServer

'4 ';$Global:DefaultVIServers

'-----------------'

'1 ';if ($Global:global:DefaultVIServer -notmatch $vSphere){'ok'}

'2 ';if ($Global:global:DefaultVIServers -notmatch $vSphere){'ok'}

'3 ';if ($Global:DefaultVIServer -notmatch $vSphere){'ok'}

'4 ';if ($Global:DefaultVIServers -notmatch $vSphere){'ok'}

'-----------------'

'1 ';if ($Global:global:DefaultVIServer -match $vSphere){'ok'}

'2 ';if ($Global:global:DefaultVIServers -match $vSphere){'ok'}

'3 ';if ($Global:DefaultVIServer -match $vSphere){'ok'}

'4 ';if ($Global:DefaultVIServers -match $vSphere){'ok'}

'----------------'

PowerCli-Connection -Server $vSphere

-----------------

'1 ';$Global:global:DefaultVIServer

'2 ';$Global:global:DefaultVIServers

'3 ';$Global:DefaultVIServer

'4 ';$Global:DefaultVIServers

'-----------------'

'1 ';if ($Global:global:DefaultVIServer -notmatch $vSphere){'ok'}

'2 ';if ($Global:global:DefaultVIServers -notmatch $vSphere){'ok'}

'3 ';if ($Global:DefaultVIServer -notmatch $vSphere){'ok'}

'4 ';if ($Global:DefaultVIServers -notmatch $vSphere){'ok'}

'-----------------'

'1 ';if ($Global:global:DefaultVIServer -match $vSphere){'ok'}

'2 ';if ($Global:global:DefaultVIServers -match $vSphere){'ok'}

'3 ';if ($Global:DefaultVIServer -match $vSphere){'ok'}

'4 ';if ($Global:DefaultVIServers -match $vSphere){'ok'}

1. -
2. -
3. -
4. -
-----------------
1. ok
2. ok
3. ok
4. -
-----------------
1. -
2. -
3. -
4. -
-----------------
Connected
-----------------
1. -
2. -
3. Name
4. Name
-----------------
1. ok
2. ok
3. -
4. -
-----------------
1. -
2. -
3. ok
4. ok

So this not an issue, I found a workaround to this but can someone explain this ? Is this a bug ?

-------------

For those who want the workaround, use reverse result of working comparaison operator:

if (!($Global:DefaultVIServers -match $vSphere)){

        PowerCli-Connection -Server $vSphere

}

Check LucD reply for a clean way to do it.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

I'm afraid I don't really agree with your workaround.

The better solution would be

if ($Global:DefaultVIServers.Name -contains $vSphere){

  PowerCli-Connection -Server $vSphere

}

But there is a flaw, even in this one.
The $global:defaultviserver variable is only created after the first Connect-VIServer, so in reality it is comparing $null with the FQDN of the server.

Which will make the comparison still work, but be aware what you are doing!


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership

The $global:global:defaultviserver is a "feature" that has been around as long as I can remember.
It looks as if those variables are defined, after the first Connect-VIServer in your PS session, in a funny way.
The "global" is for one reason or the other part of the variable name (which it shouldn't be afaik).

For comparison look at other Global variables.

global.jpg

I'm not sure in which circumstances you would ever need to use $global:global:defaultviserver?

I never used that incorrect variable specification, and I never had an issue with it in my scripts or functions.

From your table I deduce that you have the FQDN to a vSphere server in the $vSphere variable.
Be aware that you are comparing an object ($global:defaultViServer) with a string ($vSphere).

That means that you rely on the casting features of PowerShell to make the comparison work (as you intend it to work).


The correct comparison ([string] to [string]) should be, imho, $global:defaultviserver.Name -eq $vSphere


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

LucD
Leadership
Leadership

I'm afraid I don't really agree with your workaround.

The better solution would be

if ($Global:DefaultVIServers.Name -contains $vSphere){

  PowerCli-Connection -Server $vSphere

}

But there is a flaw, even in this one.
The $global:defaultviserver variable is only created after the first Connect-VIServer, so in reality it is comparing $null with the FQDN of the server.

Which will make the comparison still work, but be aware what you are doing!


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

View solution in original post

0 Kudos
xSyteck
Enthusiast
Enthusiast

Thanks for you quick reply. Your answer is way better than my workaround. My way to match/notmatch worked as I wanted until now.

At least I had a reply for the $Global:global:* weird form.

0 Kudos
LucD
Leadership
Leadership

I think I'll raise an Idea to fix that "feature" finally :smileygrin:


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

0 Kudos
xSyteck
Enthusiast
Enthusiast

One last thing, the test:

if ($Global:DefaultVIServers.Name -match $vSphere)

Only do the test for the first object, it's the same thing that:

if ($Global:DefaultVIServer.Name -match $vSphere)

(without an S at the end, for those who doesn't understand, $Global:DefaultVIServer is the object of the default VI Server, $Global:DefaultVIServers is every VI Server you're connected to)

So the final clean code is:

$NotConnected=$True

$Global:DefaultVIServers | %{if ($_.Name -match $vSphere){$NotConnected=$False}}

if ($NotConnected -eq $True){

        PowerCli-Connection -Server $vSphere

}

0 Kudos
LucD
Leadership
Leadership

That was the reason why I used the -contains operator


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

0 Kudos
xSyteck
Enthusiast
Enthusiast

My bad, I didn't paid attention, but I learned one more thing :smileygrin: (I've never used -contains operator)

Thank you again !

0 Kudos