function Get-CachedInstances { <# .SYNOPSIS Get the cached instance data. Defaults to dev data .PARAMETER ProfileName A valid ProfileName #> [CmdletBinding()] [OutputType([object[]])] param ( [Parameter(Mandatory = $false)] [string]$ProfileName = (Get-LocalCachedAWSProfile), [Parameter(Mandatory = $false)] [ValidateScript({ $ProfileName = Get-Coalesce $PSCmdlet.MyInvocation.BoundParameters["ProfileName"], (Get-LocalCachedAWSProfile) (Get-Designations -ProfileName $ProfileName) -contains $_ })] [string]$Designation, [Parameter()] [switch]$MIC, [Parameter()] [switch]$WEB, [Parameter()] [switch]$APP, [Parameter()] [switch]$TeamCity, [Parameter()] [switch]$IncludeDR, [Parameter()] [switch]$OnlyDR ) $logLead = (Get-LogLeadName) # if no server types were selected, then we want all server types $noServerTypesSelected = $false if (-not $WEB -and -not $MIC -and -not $APP -and -not $TeamCity) { $noServerTypesSelected = $true } # true up selection as required $WEB = $WEB -or $noServerTypesSelected $MIC = $MIC -or $noServerTypesSelected $APP = $APP -or $noServerTypesSelected $TeamCity = $TeamCity -or $noServerTypesSelected if ($OnlyDR -and $IncludeDR) { Write-Warning "$logLead : You can not select OnlyDR and IncludeDR. Opting for IncludeDR." $OnlyDR = $false } $designationSpecified = -not [string]::IsNullOrWhiteSpace($Designation) Assert-ValidAWSProfileName -ProfileName $ProfileName $cachePath = Get-CachePathEC2Instance -ProfileName $ProfileName if (!(Test-Path -Path $cachePath)) { Write-Warning "$logLead : No file found at [$cachePath]. You probably should call Checkpoint-EC2Instances -ProfileName $ProfileName" return @() } if (Test-WasFileModifiedWithin -Path $cachePath -Last Week) { if (Test-IsCurrentAWSUserSessionValid -ProfileName $ProfileName) { Write-Information "$logLead : Instance information is more than 7 days out of date, and you have a valid session token. Updating EC2 instance information." Checkpoint-EC2Instances -ProfileName $ProfileName } else { Write-Warning "$logLead : Your $ProfileName designations file is more than 7 days out of date. Do you need to run [Checkpoint-EC2Instances -ProfileName $ProfileName] again?" } } $instances = Get-Json -Path $cachePath | Where-Object { $instance = $_ $result = $false if ($WEB) { $result = $result -or "$($instance.Hostname)".ToLower().StartsWith('web') } if ($MIC) { $result = $result -or "$($instance.Hostname)".ToLower().StartsWith('mic') } if ($APP) { $result = $result -or "$($instance.Hostname)".ToLower().StartsWith('app') } if ($TeamCity) { $result = $result -or "$($instance.Hostname)".ToLower().StartsWith('tea') } if ($noServerTypesSelected) { $result = $true } $result } | Where-Object { $instance = $_ if ($OnlyDR) { $instance.tags."alk:env" -eq "dr" } else { if ($IncludeDR) { $true } else { $instance.tags."alk:env" -ne "dr" } } } | Where-Object { $instance = $_ if ($designationSpecified) { $instance.Designation -eq $Designation } else { $true } } return ($instances | ConvertTo-Instance) }