ps/Modules/Alkami.DevOps.TeamCity/Public/Get-EC2InstanceState.ps1

66 lines
2.2 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-EC2InstanceState {
<#
.SYNOPSIS
Returns an AWS instance status object
.PARAMETER ComputerName
ComputerName(s) of a machine that is located in AWS
.PARAMETER InstanceId
AWS owned ID(s) of a machine
.PARAMETER ProfileName
AWS environment credential
.PARAMETER Type
Optional Parameter. Type of TeamCity host(s) to return: All, Server(s), or Agent(s)
#>
[CmdletBinding()]
[OutputType([Object])]
param(
[Parameter(Mandatory = $false)]
[string[]]$ComputerName,
[Parameter(Mandatory = $false)]
[string[]]$InstanceId,
[Parameter(Mandatory = $true)]
[string]$ProfileName,
[ValidateSet("All", "Server", "Agent")]
[Parameter(Mandatory = $false)]
[string]$Type
)
$endResults = @()
if (!(Test-StringIsNullOrWhitespace -Value $Type)) {
$ComputerName = Get-TeamCityHostnames -Type $Type -ProfileName $ProfileName
}
if ((Test-IsCollectionNullOrEmpty -Collection $ComputerName) -and (Test-IsCollectionNullOrEmpty -Collection $InstanceId)) {
Write-Warning "$loglead : A ComputerName or InstanceId was not provided, exiting..."
Return
}
if (Test-IsCollectionNullOrEmpty -Collection $ComputerName) {
$ComputerName = $InstanceId
}
foreach ($name in $ComputerName) {
if (Test-IsCollectionNullOrEmpty -Collection $InstanceId) {
$hostnameInstanceId = (Get-EC2InstancesByHostname -ProfileName $ProfileName -Servers $name).InstanceId
}else {
$hostnameInstanceId = $name
}
try {
Write-Verbose "$loglead : Attempting Region : us-east-1"
$results = Get-EC2InstanceStatus -InstanceId $hostnameInstanceId -ProfileName $ProfileName -Region "us-east-1"
$endResults += $results
} catch [InvalidOperationException] {
Write-Verbose "$loglead : Attempting Region : us-west-2"
$results = Get-EC2InstanceStatus -InstanceId $hostnameInstanceId -ProfileName $ProfileName -Region "us-west-2"
$endResults += $results
} catch {
Write-Host "$($_.Exception.Message)"
}
}
Return $endResults
}