ps/Modules/Cole.PowerShell.Developer/Public/Checkpoint-EC2Instances.ps1
2023-05-30 22:51:22 -07:00

52 lines
2.0 KiB
PowerShell

function Checkpoint-EC2Instances {
<#
.SYNOPSIS
Get the EC2 instances for the specified profile and save them in the cached files
.PARAMETER ProfileName
A valid ProfileName
#>
[CmdletBinding()]
[OutputType([object[]])]
param(
[Parameter(Mandatory = $true)]
[string]$ProfileName
)
$activityLabel = "Gathering instances"
Assert-ValidAWSProfileName -ProfileName $ProfileName
if (!(Test-IsCurrentAWSUserSessionValid -ProfileName $ProfileName)) {
throw "User session is invalid. Please run Update-AWSProfile $($ProfileName.Replace('temp-',''))"
}
$PercentComplete = 1
Write-ProgressHelper -Activity $activityLabel -Status "Starting" -PercentComplete $PercentComplete
$PercentComplete += 1
$allInstances = @()
foreach ($region in (Get-AWSRegions)) {
Write-ProgressHelper -Activity $activityLabel -Status $Region -PercentComplete $PercentComplete
$instances = Get-EC2Instance -Region $region -ProfileName $ProfileName
$PercentComplete += 20
Write-ProgressHelper -Activity $activityLabel -Status $Region -PercentComplete $PercentComplete
$allInstances += ConvertFrom-EC2Instance -Instances $instances.Instances
$PercentComplete += 4
Write-ProgressHelper -Activity $activityLabel -Status $Region -PercentComplete $PercentComplete
}
$ProfileName = $ProfileName.Replace("temp-","").ToLower()
Write-ProgressHelper -Activity $activityLabel -Status "Saving instance file" -PercentComplete 98
$cacheFile = Get-CachePathEC2Instance -ProfileName $ProfileName
ConvertTo-Json $allInstances -Depth 10 | Set-Content -Path $cacheFile
Write-ProgressHelper -Activity $activityLabel -Status "Saving designation file" -PercentComplete 99
$cacheFile = Get-CachePathDesignations -ProfileName $ProfileName
$allInstances.Designation | Sort-Object | Get-Unique | Set-Content -Path $cacheFile
Write-Progress -Activity $activityLabel -Completed
}