Da PowerShell die EC2 Instanzen nur pro Region anzeigt, habe ich ein Skript geschrieben, das alle Regionen durchgeht und die darin enthaltenen Instanzen zur Auswahl anzeigt, sodass eine davon gewählt werden kann.
Es nutzt wiederum folgendes Skripte:
AWS AccessKey automatisch auslesen
Diese am besten herunterladen und die Pfade in folgendem Skript entsprechend anpassen.
####################################################################
####################################################################
###### Hier die Parameter definieren. Den Rest in Ruhe lassen ######
###### Außer dem Pfad zum Skript ######
param(
$AccessKeyCSVPath = "C:\_Privat\AWS\Accesskey.csv",
$AccessKey,
$SecretKey,
$ProfileName = "TempProfil"
)
###### ######
####################################################################
####################################################################
# Access- und SecretKey einlesen
if(!($AccessKey -and $SecretKey)){
$AWSAccessKey = C:\_Skripte\AWS\Get-AWSAccessKey.ps1 -AccessKeyCSVPath $AccessKeyCSVPath
$AccessKey = $AWSAccessKey.AccessKey
$SecretKey = $AWSAccessKey.SecretKey
}
if($AccessKey -and $SecretKey){
# AWS-Modul importieren
Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
# Credential für AWS erstellen
Set-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey -StoreAs $ProfileName
# Auswahlmenü für die Region erstellen
$AWSRegions = Get-AWSRegion
$AllEC2Instances = @()
$i = 1
foreach($AWSRegion in $AWSRegions){
# Region initialisieren
Initialize-AWSDefaults -ProfileName $ProfileName -Region $AWSRegion.Region
$EC2Instances = Get-EC2Instance
foreach($EC2Instance in $EC2Instances){
$RegionalEC2Instances = "" | Select ID,Tags,PrivateIpAddress,PublicIpAddress,GroupNames,Groups,Instances,KeyName,OwenerId,RequstId,ReservationId,Region,RegionName
$RegionalEC2Instances.ID = $i
$EC2InstanceTags = ""
$Tags = $EC2Instance.Instances.Tags
for($j=0; $j -lt $Tags.Count; $j++){
$EC2InstanceTags += $Tags[$j].Key + " = " + $Tags[$j].Value
if($j -lt ($Tags.Count - 1)){
$EC2InstanceTags += "; "
}
}
$RegionalEC2Instances.Tags = $EC2InstanceTags
$RegionalEC2Instances.PrivateIpAddress = $EC2Instance.Instances.PrivateIpAddress
$RegionalEC2Instances.PublicIpAddress = $EC2Instance.Instances.PublicIpAddress
$RegionalEC2Instances.GroupNames = $EC2Instance.GroupNames
$RegionalEC2Instances.Groups = $EC2Instance.Groups
$RegionalEC2Instances.Instances = $EC2Instance.Instances
$RegionalEC2Instances.KeyName = $EC2Instance.Instances.KeyName
$RegionalEC2Instances.OwenerId = $EC2Instance.OwenerId
$RegionalEC2Instances.RequstId = $EC2Instance.RequstId
$RegionalEC2Instances.ReservationId = $EC2Instance.ReservationId
$RegionalEC2Instances.Region = $AWSRegion.Region
$RegionalEC2Instances.RegionName = $AWSRegion.Name
$AllEC2Instances += $RegionalEC2Instances
$i++
}
}
# Auswahlmenü für die Region anzeigen
$Selection = $null
while($AllEC2Instances.ID -notcontains $Selection){
cls
Write-Host "Welche Instanz soll genommen werden?" -ForegroundColor Yellow
$AllEC2Instances | ft ID,KeyName,Tags,PrivateIpAddress,PublicIpAddress,Region | Out-String
$Selection = Read-Host "Bitte eine Auswahl treffen"
}
$Instance = ($AllEC2Instances | ? {$_.ID -eq $Selection})
return $Instance
# Credentials und Region wieder leeren
Clear-DefaultAWSRegion
Remove-AWSCredentialProfile -ProfileName $ProfileName -Force
}else{
Write-Host "Ohne AccessKey und SecretKey geht es nicht." -ForegroundColor Red
}
Feel free das Menü nach eigenem Ermessen anzupassen.