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.