VMware Cloud Community
Leffingwell
Contributor
Contributor

Connect-VIServer, strange behavior

Hey all,

Sorting out some issues on a script I worked on a while back.  The script works great except for one issue:  I loop through a list of VIServer's, connect to them, pull a bit of data, disconnect, and start the cycle over again.  What's happening is the first time through works great, the second time through it gets to the connect-viserver cmdlet, shows console output that it's connected on 443, then immediately exits the loop and jumps to the next segment of code.

Here's a snipped from the script to localize what I need to look at, I can paste more as necessary!  Thanks so much in advance for any feedback or insight into this issue.

##########################################################
# US Servers
##########################################################

foreach ($Server in $USServers)
{
    Write-Output "Entering the loop! Server = $Server"
    # Connect to a given instance
    Connect-VIServer -Server $Server -Credential $USCredential

    # Customize the LogName
    $LogName = "$Server" + "_" + $LogName
   
    # Get Snapshots that are older than a given date specified by "Snap Age"
    Get-Cluster -Name *oduction* | Get-VM |
    Get-Snapshot |
    %{
        if ($_.Created.ToLocalTime() -lt $SnapAge)
        {
            $_ | Select @{ n="VM Name"; e={ $_.VM.Name}}, @{ n="Snap Name"; e={ $_.Name}}, @{ n="Created On"; e={ $_.Created.ToLocalTime()}}, SizeMB
            $LogOutput += "Removed snap from: " + $_.VM.Name + ", named: " + $_.Name
        }
    }

    # Output Log
    $LogOutput | Out-File c:\Logs\$LogName.txt

    # Revert the LogName, reset the LogOutput buffer, disconnect from the server
    $LogName = $RestoreName
    $LogOutput = @()
    Write-Output "Disconnecting from $Server"
    Disconnect-VIServer -Server $Server -Force
}

Here is the console output:

Entering the loop! Server = sgcvmvcs01

<output omitted - successful execution of script>

Disconnecting from sgcvmvcs01
Entering the loop! Server = sgivmvcsvr01
sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
Disconnecting from sgivmvcsvr01
LogOutput =
Entering the loop! Server = lncvmvcs02
lncvmvcs02                     443   eu\uscorpsvcvcs5

Now before anyone suggests - I have also tried without the disconnect-viserver statement, because honestly in all of my other scripts that do similar things I've never had to use it, this is the first time.

Thanks again for any help Smiley Happy

Kindest Regards,

ALAN

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership

Is there a specific reason you are using the Force switch on the Disconnect-VIServer cmdlet ?

As an additional debugging step, can you display the contents of

$global:defaultviservers

before and after the Connect-VIServer and Disconnect-VIServer cmdlets ?


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

Leffingwell
Contributor
Contributor

Hey LucD,

Thanks for replying!  I used -Force because I don't have much experience with the cmdlet or with what problem was occuring in my loop, so I figured, eh - can't hurt :smileysilly:

This is as scientific a reason as I can give you!

I did as mentioned, and this is the result I get:

PS F:\> C:\Users\aleffingwell\Desktop\Script testing\SSR.ps1
Entering the loop! Server = sgcvmvcs01
WARNING: There were one or more problems with the server certificate:

* A certification chain processed correctly, but terminated in a root certificate which isn't trusted
by the trust provider.

* The X509 chain is not valid due to an invalid time value, such as a value that indicates an expired
certificate.

* The certificate's CN name does not match the passed value.



Name                           Port  User                         
----                           ----  ----                         
sgcvmvcs01                     443   corporate\uscorpsvcvcs5      
sgcvmvcs01                     443   corporate\uscorpsvcvcs5     

<output ommitted - good behavior>

Disconnecting from sgcvmvcs01
sgcvmvcs01                     443   corporate\uscorpsvcvcs5      
Entering the loop! Server = sgivmvcsvr01
WARNING: There were one or more problems with the server certificate:

* The X509 chain could not be built up to the root certificate.

* The certificate's CN name does not match the passed value.


sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
Disconnecting from sgivmvcsvr01
sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
LogOutput =
Entering the loop! Server = lncvmvcs02
WARNING: There were one or more problems with the server certificate:

* A certification chain processed correctly, but terminated in a root certificate which isn't trusted
by the trust provider.

* The certificate's CN name does not match the passed value.


lncvmvcs02                     443   eu\uscorpsvcvcs5             

__________________________________________________________________________________________________

I am in your hands sir.

Kindest Regards,

ALAN

Reply
0 Kudos
LucD
Leadership
Leadership

It looks as if 1 vCenter connection to sgivmvcsvr01 is already open when you start the script.

If you do not need that connection for the script, you could do a 'Disconnect-VIServer -Force' to close all open connections.

But I have low hopes that this causes the problem.

Btw are you running in Single or in Multiple mode ? Do a

Get-PowerCLIConfiguration

One way to avoid this is to use the Server parameter on the other PowerCLI cmdlets.

Something like

foreach ($Server in $USServers)
{ 
    Write-Output "Entering the loop! Server = $Server"
   
# Connect to a given instance     $srv = Connect-VIServer -Server $Server -Credential $USCredential
   
# Customize the LogName     $LogName = "$Server" + "_" + $LogName   
   
# Get Snapshots that are older than a given date specified by "Snap Age"     Get-Cluster -Name *oduction* -Server $srv | Get-VM -Server $srv |     Get-Snapshot -Server $srv | ...

The opening and closing of the connections seems to work correctly though.

Was the Disconnect-VIServer still with the Force switch ?

You did mention that the script jumps immediatly out of the loop on the 2nd run. Is that the outer 'foreach' loop or the inner Get-Snapshot loop ?

Did you investigate the $error variable at that point ?

In $error[0] you can find the most recent error.

One more question, from where are you running this ?

From the PowerCLI prompt ? Or from a GUI like for example PowerGUI ?

These GUIs sometimes 'eat' error messages, it could be that there is an error your are not seeing on screen.

Length answer I admit, but it's difficult to debug such a script with no hands-on :smileygrin:


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

Leffingwell
Contributor
Contributor

Hey LucD Smiley Happy

So ... from my experiences going down longer rabbit holes, even here on these boards with certain bizarre behaviors... it's always been something we weren't looking at.  I decided, you know what, instead of answering all of these questions.. lets see if I can connect-viserver manually to the one that's being goofy... sure enough, user account name/pass no longer exist or have changed!!

So - I need to get in touch with the department that handles that and get my information updated, but I think that could've been the cause to this!!

I will resolve this step first, and post my results, including the working script block in case any is ever curious in it as well.

Kindest Regards,

ALAN

Reply
0 Kudos
LucD
Leadership
Leadership

Great find, most of the time it's the obvious one that is causing the problem Smiley Wink


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

Reply
0 Kudos
Leffingwell
Contributor
Contributor

Ok!  Back and verified I can connect to the VI-Server outside of the script now, so I know it works.. script was exhibiting same behavior.  I decided to make much more verbose feedback in the script so we can see more clearly what's happening and when.  Going to post the results here, and then go and step through what you asked me in the previous post.  The results of Get-PowerCLIConfiguration return: UseSystemProxy, Single

Here's how I changed the script.

##########################################################
# US Servers
##########################################################

foreach ($Server in $USServers)
{
    Write-Output "Entering the loop! Server = $Server"
   
    # Clear the connection
    Write-Output "Global value before Disconnect-VIserver AND BEFORE Connect-VIServer is: $global:defaultviservers"
    Disconnect-VIServer -Server $Server -Force
    Write-Output "Global value AFTER Disconnect-VIserver AND BEFORE Connect-VIServer is: $global:defaultviservers"
   
    # Connect to a vCenter Server
    Connect-VIServer -Server $Server -Credential $USCredential
    Write-Output "Global value AFTER Disconnect-VIserver AND AFTER Connect-VIServer is: $global:defaultviservers"

    # Customize the LogName
    $LogName = "$Server" + "_" + $LogName
   
    # Get Snapshots that are older than a given date specified by "Snap Age"
    Get-Cluster -Name *oduction* | Get-VM |
    Get-Snapshot |
    %{
        if ($_.Created.ToLocalTime() -lt $SnapAge)
        {
            $_ | Select @{ n="VM Name"; e={ $_.VM.Name}}, @{ n="Snap Name"; e={ $_.Name}}, @{ n="Created On"; e={ $_.Created.ToLocalTime()}}, SizeMB
            $LogOutput += "Removed snap from: " + $_.VM.Name + ", named: " + $_.Name
        }
    }

    # Output Log
    $LogOutput | Out-File c:\Logs\$LogName.txt

    # Revert the LogName, reset the LogOutput buffer, disconnect from the server
    $LogName = $RestoreName
    $LogOutput = @()
    Write-Output "Global value AFTER script block has run AND BEFORE Disconnect-VIServer is: $global:defaultviservers"
    Disconnect-VIServer -Server $Server -Force
    Write-Output "Global value AFTER script block has run AND AFTER Disconnect-VIServer is: $global:defaultviservers"
    Write-Output "Restarting loop!"
}

The results are .. bizarre to say the least.

Entering the loop! Server = sgcvmvcs01
Global value before Disconnect-VIserver AND BEFORE Connect-VIServer is:

Global value AFTER Disconnect-VIserver AND BEFORE Connect-VIServer is:

Name                           Port  User                         
----                           ----  ----                         
sgcvmvcs01                     443   corporate\uscorpsvcvcs5      
Global value AFTER Disconnect-VIserver AND AFTER Connect-VIServer is: sgcvmvcs01

<output ommited>

Global value AFTER script block has run AND BEFORE Disconnect-VIServer is: sgcvmvcs01

<At this point Disconnect-VIServer executes, a pop-up box prompts me if I'm sure.. I select yes>
Global value AFTER script block has run AND AFTER Disconnect-VIServer is:
Restarting loop!

<ok - so far so good>

Entering the loop! Server = sgivmvcsvr01

<awesome, like we expect>
Global value before Disconnect-VIserver AND BEFORE Connect-VIServer is:

<Also expected, great>

<At this point Disconnect-VIServer executes, a pop-up box prompts me if I'm sure I want to disconnect from SGIVMVCSVR01?!?!?>

Global value AFTER Disconnect-VIserver AND BEFORE Connect-VIServer is:
sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
Global value AFTER Disconnect-VIserver AND AFTER Connect-VIServer is: sgivmvcsvr01
Global value AFTER script block has run AND BEFORE Disconnect-VIServer is: sgivmvcsvr01
Global value AFTER script block has run AND AFTER Disconnect-VIServer is: sgivmvcsvr01
Restarting loop!
LogOutput =
Entering the loop! Server = lncvmvcs02
lncvmvcs02                     443   eu\uscorpsvcvcs5        

So strange!  Basically at the end of the loop Disconnect-VIServer clears the $global:defaultviservers variable, we verify it's clear at the top... When the next Disconnect-VIServer executes (at the start of the loop), it should just give a warning that there are no servers of the specified name.  Instead it prompts to 'disconnect' from the next server in the list.  But how is this possible?? the pop-up to disconnect occurs before we've even gotten to the Connect-VIServer, and we've verified the contents of $global:defaultviservers is empty.

Well -- I'll let you think about that.  In the meantime I'll try some of your other suggestions.

Exciting! ALAN

Reply
0 Kudos
Leffingwell
Contributor
Contributor

Alright, I just tried out your $srv idea, no dice sir - same results, after the first iteration of the loop, it gets to the start of the next cycle then immediately skips to the end... bizarre !  Take a look my friend:

##########################################################
# US Servers
##########################################################

foreach ($Server in $USServers)
{
    Write-Output "Entering the loop! Server = $Server"
   
    # Connect to a vCenter Server
    $srv = Connect-VIServer -Server $Server -Credential $USCredential


    # Customize the LogName
    $LogName = "$Server" + "_" + $LogName
   
    # Get Snapshots that are older than a given date specified by "Snap Age"
    Get-Cluster -Name *oduction* -Server $srv | Get-VM -Server $srv |
    Get-Snapshot -Server $srv |

    %{
        if ($_.Created.ToLocalTime() -lt $SnapAge)
        {
            $_ | Select @{ n="VM Name"; e={ $_.VM.Name}}, @{ n="Snap Name"; e={ $_.Name}}, @{ n="Created On"; e={ $_.Created.ToLocalTime()}}, SizeMB
            $LogOutput += "Removed snap from: " + $_.VM.Name + ", named: " + $_.Name
        }
    }

    # Output Log
    $LogOutput | Out-File c:\Logs\$LogName.txt

    # Revert the LogName, reset the LogOutput buffer, disconnect from the server
    $LogName = $RestoreName
    $LogOutput = @()
}

Here are the answers to your other questions:

Was the Disconnect-VIServer still with the Force switch ?

Yes, I kept the rest of the script the same.

You did mention that the script jumps immediatly out of the loop on the  2nd run. Is that the outer 'foreach' loop or the inner Get-Snapshot loop  ?

Yes correct, the loops I posted is the entirety of my testing portion of the script after that, it just goes to another set of loops for the EU servers (which use different credentials)

Did you investigate the $error variable at that point ?

Yes!  Thanks for that cool tip, I use PowerShell ISE to develop.  Error output (immediately after the test script here finished) was:

PS F:\> $error[0]
Add-PSSnapin : Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core because it is already
added. Verify the name of the snap-in and try again.
At C:\Users\aleffingwell\Desktop\Script testing\LucD Suggested Changes.ps1:19 char:13
+ Add-PSSnapin <<<<  *vmware*
    + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Core:String) [Add-PSSnapin], PS
   ArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

So nothing more than associating the snapin again.

One more question, from where are you running this ?

I am in a Main office, the vCenter servers are in a DataCenter, as previously mentioned I am running via PowerShell ISE, built in stock with Win 7.

Looking forward to figuring this beast out!

ALAN

Had to add the other question responses.

Reply
0 Kudos
LucD
Leadership
Leadership

Clutching at straws here; can you try adding the -Confirm:$false parameter on the Disconnect-VIServer cmdlet ?

I have the impression the prompts comes late and possibly screws up the logic of the script


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

Reply
0 Kudos
Leffingwell
Contributor
Contributor

Same results, check it out:

Entering the loop! Server = sgcvmvcs01
Global value before Disconnect-VIserver AND BEFORE Connect-VIServer is:

Global value AFTER Disconnect-VIserver AND BEFORE Connect-VIServer is:

Name                           Port  User                         
----                           ----  ----                         
sgcvmvcs01                     443   corporate\uscorpsvcvcs5      
Global value AFTER Disconnect-VIserver AND AFTER Connect-VIServer is: sgcvmvcs01

<output ommited>

Global value AFTER script block has run AND BEFORE Disconnect-VIServer is: sgcvmvcs01
Global value AFTER script block has run AND AFTER Disconnect-VIServer is:
Restarting loop!
Entering the loop! Server = sgivmvcsvr01
Global value before Disconnect-VIserver AND BEFORE Connect-VIServer is:
Global value AFTER Disconnect-VIserver AND BEFORE Connect-VIServer is:
sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
Global value AFTER Disconnect-VIserver AND AFTER Connect-VIServer is: sgivmvcsvr01
Global value AFTER script block has run AND BEFORE Disconnect-VIServer is: sgivmvcsvr01
Global value AFTER script block has run AND AFTER Disconnect-VIServer is:
Restarting loop!
LogOutput =
Entering the loop! Server = lncvmvcs02

It clearly shows that it connects to sgivmvcsvr01, but it just immediately drops down to the disconnect-VIserver statement... does VMware officially support these commandlets?? Is there a way for me to escalate this if for example you don't know?  I always just assume you will know lol, never been in this situation and in this case this is an assignment I can't bypass!!

Thanks in advance LucD,

ALAN

Reply
0 Kudos
LucD
Leadership
Leadership

If you have a valid SnS contract you can raise a call for PowerCLI.

We recently had another thread asking about this and there it was confirmed that you can open a PowerCLI related call.


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

Reply
0 Kudos
LucD
Leadership
Leadership

Did you ever try this with a minimal code block.

This is what I mean

foreach ($Server in $USServers)
{ 
    Disconnect-VIServer -Server $Server -Force
   
Connect-VIServer -Server $Server -Credential $USCredential
    Write-Host "Between Connect and Disconnect"
    Sleep 5
    Disconnect-VIServer -Server $Server -Confirm:$false
}

If this shows the same behaviour then the 2nd loop should not execute the Write-Host and the Sleep command.


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

Reply
0 Kudos
Leffingwell
Contributor
Contributor

So .. I did that with a bit more output .. the results are ... again bizarre!  I feel like it's taking the entire list of servers in the first time around or something.. I'm going to include the ENTIRE console output of this exact script executing because I think it's very telling and since you have a lot more experience than me with powershell I think you'll be able to identify a possible cause here.  I'm also going to include the full script include variable assignment etc..

Script:

foreach ($Server in $USServers)
{
    Write-Output "Entering loop"
    Write-Output "Executing Disconnect-VIServer.  Current Global: $global:defaultviservers"
    Disconnect-VIServer -Server $Server -Force
    Write-Output "Done disconnecting.  current Global: $global:defaultviservers"
    Write-Output "Connecting to $Server"
    Connect-VIServer -Server $Server -Credential $USCredential
    Write-Host "Done connecting.  current Global: $global:defaultviservers"
    Sleep 5
    Write-Host "Random activity is occuring!"
    Write-Output "Disconnecting again!.  current Global: $global:defaultviservers"
    Disconnect-VIServer -Server $Server -Force -Confirm:$False
    Write-Output "Done disconnecting.  current Global: $global:defaultviservers"
    Write-Output "Exiting loop"
}

PS F:\> C:\Users\aleffingwell\Desktop\Script testing\LucD Suggested Changes.ps1
Add-PSSnapin : Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core because it is already
added. Verify the name of the snap-in and try again.
At C:\Users\aleffingwell\Desktop\Script testing\LucD Suggested Changes.ps1:19 char:13
+ Add-PSSnapin <<<<  *vmware*
    + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Core:String) [Add-PSSnapin], PS
   ArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

Entering loop
Executing Disconnect-VIServer.  Current Global:
Disconnect-VIServer : 1/9/2013 1:14:22 PM    Disconnect-VIServer        Could not find VIServer with
name 'sgcvmvcs01'.   
At C:\Users\aleffingwell\Desktop\Script testing\LucD Suggested Changes.ps1:29 char:24
+     Disconnect-VIServer <<<<  -Server $Server -Force
    + CategoryInfo          : ObjectNotFound: (sgcvmvcs01:String) [Disconnect-VIServer], VimExceptio
   n
    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DisconnectVIServer

Disconnect-VIServer : 1/9/2013 1:14:22 PM    Disconnect-VIServer        Could not find any of the ser
vers specified by name.   
At C:\Users\aleffingwell\Desktop\Script testing\LucD Suggested Changes.ps1:29 char:24
+     Disconnect-VIServer <<<<  -Server $Server -Force
    + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...Server[] Server:RuntimePropertyInfo
   ) [Disconnect-VIServer], ServerObnFailureException
    + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ServerSpecifiedButNotFound,VMwar
   e.VimAutomation.ViCore.Cmdlets.Commands.DisconnectVIServer

Done disconnecting.  current Global:
Connecting to sgcvmvcs01
WARNING: There were one or more problems with the server certificate:

* A certification chain processed correctly, but terminated in a root certificate which isn't trusted
by the trust provider.

* The X509 chain is not valid due to an invalid time value, such as a value that indicates an expired
certificate.

* The certificate's CN name does not match the passed value.



Name                           Port  User                         
----                           ----  ----                         
sgcvmvcs01                     443   corporate\uscorpsvcvcs5      
Done connecting.  current Global: sgcvmvcs01
Random activity is occuring!
Disconnecting again!.  current Global: sgcvmvcs01
Done disconnecting.  current Global:
Exiting loop
Entering loop
Executing Disconnect-VIServer.  Current Global:
Disconnect-VIServer : 1/9/2013 1:14:28 PM    Disconnect-VIServer        Could not find VIServer with
name 'sgivmvcsvr01'.
  

At C:\Users\aleffingwell\Desktop\Script testing\LucD Suggested Changes.ps1:29 char:24
+     Disconnect-VIServer <<<<  -Server $Server -Force
    + CategoryInfo          : ObjectNotFound: (sgivmvcsvr01:String) [Disconnect-VIServer], VimExcept
   ion
    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DisconnectVIServer

Disconnect-VIServer : 1/9/2013 1:14:28 PM    Disconnect-VIServer        Could not find any of the ser
vers specified by name.   
At C:\Users\aleffingwell\Desktop\Script testing\LucD Suggested Changes.ps1:29 char:24
+     Disconnect-VIServer <<<<  -Server $Server -Force
    + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...Server[] Server:RuntimePropertyInfo
   ) [Disconnect-VIServer], ServerObnFailureException
    + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ServerSpecifiedButNotFound,VMwar
   e.VimAutomation.ViCore.Cmdlets.Commands.DisconnectVIServer

Done disconnecting.  current Global:
Connecting to sgivmvcsvr01
WARNING: There were one or more problems with the server certificate:

* The X509 chain could not be built up to the root certificate.

* The certificate's CN name does not match the passed value.


sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
Done connecting.  current Global: sgivmvcsvr01
Random activity is occuring!
Disconnecting again!.  current Global: sgivmvcsvr01
Done disconnecting.  current Global:
Exiting loop

It shows it's executing the write-output statement before exiting the second time... this got me curious so I marked up the script at every step of the way in the real deal.. as follows:

foreach ($Server in $USServers)
{
    Write-Output "Entering the loop! Server = $Server"
   
    # Clear the connection
    Write-Output "Global value before Disconnect-VIserver AND BEFORE Connect-VIServer is: $global:defaultviservers"
    Disconnect-VIServer -Server $Server -Force -Confirm:$False
    Write-Output "Global value AFTER Disconnect-VIserver AND BEFORE Connect-VIServer is: $global:defaultviservers"
   
    # Connect to a vCenter Server
    Connect-VIServer -Server $Server -Credential $USCredential
    Write-Output "Global value AFTER Disconnect-VIserver AND AFTER Connect-VIServer is: $global:defaultviservers"

    Write-Output "Customizing Logname"
    # Customize the LogName
    $LogName = "$Server" + "_" + $LogName
    Write-Output "Customizing Logname done"
   
    Write-Output "Getting cluster, vm and snaps"
    # Get Snapshots that are older than a given date specified by "Snap Age"
    Get-Cluster -Name *oduction* | Get-VM |
    Get-Snapshot |
    %{
       Write-Output "I've entered the Get-Snapshot loop"
        if ($_.Created.ToLocalTime() -lt $SnapAge)
        {
            $_ | Select @{ n="VM Name"; e={ $_.VM.Name}}, @{ n="Snap Name"; e={ $_.Name}}, @{ n="Created On"; e={ $_.Created.ToLocalTime()}}, SizeMB
            $LogOutput += "Removed snap from: " + $_.VM.Name + ", named: " + $_.Name
        }
    }

    Write-Output "Done getting cluster, vm and snaps"
    Write-Output "Writing to Log"
    # Output Log
    $LogOutput | Out-File c:\Logs\$LogName.txt
    Write-Output "Done writing to log"

    # Revert the LogName, reset the LogOutput buffer, disconnect from the server
    $LogName = $RestoreName
    $LogOutput = @()
    Write-Output "Global value AFTER script block has run AND BEFORE Disconnect-VIServer is: $global:defaultviservers"
    Disconnect-VIServer -Server $Server -Force -Confirm:$False
    Write-Output "Global value AFTER script block has run AND AFTER Disconnect-VIServer is: $global:defaultviservers"
    Write-Output "Restarting loop!"
}

Console:

sgivmvcsvr01                   443   corporate\uscorpsvcvcs5      
Global value AFTER Disconnect-VIserver AND AFTER Connect-VIServer is: sgivmvcsvr01
Customizing Logname
Customizing Logname done
Getting cluster, vm and snaps
Done getting cluster, vm and snaps

Writing to Log
Done writing to log
Global value AFTER script block has run AND BEFORE Disconnect-VIServer is: sgivmvcsvr01
Global value AFTER script block has run AND AFTER Disconnect-VIServer is:
Restarting loop!
LogOutput =
Entering the loop! Server = lncvmvcs02

So it looks like it never ONCE enters the loop for snaps, so I decided to step through each get- cmdlet and see what it returns me outside of the scope of the script/loop.  I found the answer !  I did get-cluster, and it returns 4 bizarrely named clusters... looks like they decided to change the names of the clusters so my initial get-cluster was returning nothing so it had nothing to do which is why it zipped through.

You know man... we covered the KISS solutions in the beginning, looks like it still got us !

Thank you SO much for your extreme patience and help in this.

Reply
0 Kudos
LucD
Leadership
Leadership

Great, glad you found the solution.

Like Sherlock usually says "When you have eliminated the impossible, whatever remains, however improbable, must be the truth" :smileygrin:


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

Reply
0 Kudos
Leffingwell
Contributor
Contributor

Haha, that's an awesome quote !! I will absolutely work that one into my troubleshooting memory banks Smiley Happy -ALAN

Reply
0 Kudos