function Reset-ASInstanceHealth { <# .SYNOPSIS Method to reset instance health status as reported to its auto scale group. .DESCRIPTION Will attempt to set instance health to "Healthy" for all servers passed in. Will sleep for the defined wait period (2 minutes default) and query again the status. If any are still reporting as "Unhealthy" then this will Write-Error. .PARAMETER Servers List of servers to work with. Usually grouped by App, Web. Assumes all servers passed in are in the same ASG. Assumes all servers have "".fh.local" appended to the hostname. .PARAMETER SkipHealthCheck Switch to skip the 2 minute wait period and return immediately following the set operation. .PARAMETER WaitSeconds Number of seconds to wait. Default if not provided is 120 seconds (2 minutes.) #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [Alias('Computers')] [string[]]$Servers, [Parameter(Mandatory = $true)] [string]$ProfileName, [Parameter(Mandatory = $false)] [switch]$SkipHealthCheck = $false, [Parameter(Mandatory = $false)] [Alias('Sleep')] [int]$WaitSeconds = 120 ) $allHealthy = $true $logLead = (Get-LogLeadName) $instanceHash = @{} Import-AWSModule # AS # Test each server for "Healthy" ASG status and set to "Healthy" if it's not. foreach ($computerName in $servers) { Write-Verbose "$logLead : Attempting to Get Computer $computerName ASG status." # $getHealthStatusScriptBlock $region = Get-AwsRegionByHostname -ComputerName $computerName $currentInstance = Get-EC2InstancesByHostname -Servers $computerName -ProfileName $ProfileName $instanceHash += @{$computerName = $currentInstance } $asInstance = Get-ASAutoScalingInstance -InstanceId $currentInstance.InstanceId -Region $region -ProfileName $ProfileName Write-Verbose "$logLead : $computerName status is $($asInstance.HealthStatus)" if ($asInstance.HealthStatus -ne "HEALTHY") { Write-Verbose "$logLead : $computerName is not well. Setting to Healthy status." Set-ASInstanceHealth -InstanceId $currentInstance.InstanceId -HealthStatus "Healthy" -ProfileName $ProfileName -Region $region -Force $allHealthy = $false } } # Take a nap if not all healthy. This allows the ASG to re-report any that switch back to "Unhealthy". if (!$allHealthy -and !$SkipHealthCheck) { Write-Host "$logLead : Not all instances are reporting Healthy. Setting to Healthy status and sleeping for $waitSeconds seconds." Start-Sleep -Seconds $WaitSeconds Write-Host "$logLead : Checking again for health status." # Array to hold any errors. $errors = @() # Check again for "Healthy" ASG status on all servers. foreach ($computerName in $servers) { Write-Verbose "$logLead : Attempting to Get Computer $computerName ASG status." $currentInstance = $instanceHash.$computerName $actionResult = Get-ASAutoScalingInstance -InstanceId $currentInstance.InstanceId -Region $region -ProfileName $ProfileName Write-Verbose "$logLead : $computerName status is $($actionResult.HealthStatus)" if ($actionResult.HealthStatus -ne "HEALTHY") { # All-stop when one switches back to "Unhealthy". $errors += "$logLead : Computer $computerName is not in a Healthy status as reported to the ASG." } Write-Verbose "$logLead : $computerName status is $actionResult" } if (!(Test-IsCollectionNullOrEmpty $errors)) { Write-Warning "Error(s) found" $allErrors = $errors -Join "," throw $allErrors } } }