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
Kindest Regards,
ALAN