Created
July 14, 2022 01:11
-
-
Save BlueDoge/de912d8786de945c28a09ef45f82748a to your computer and use it in GitHub Desktop.
Spawn multiple PuTTY instances to connect to multiple AWS EC2 Instances via Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Originally created by Liz Clements in 2022 | |
#License: MIT | |
$AWSRegion = 'us-west-2' | |
$AWSProfileLocation = ("$env:USERPROFILE\.aws\credentials") | |
$AWSProfileName = 'default' | |
$ListOfInstances = (Get-EC2Instance -Region $AWSRegion -ProfileLocation $AWSProfileLocation -ProfileName $AWSProfileName).Instances ` | |
| Select-Object ` | |
@{Name="ServerAlias"; Expression={$_.Tags | Where-Object Key -eq "Name" | Select-Object Value -ExpandProperty Value}} ` | |
, @{Name="State"; Expression={$_.State.Name}} ` | |
, InstanceId, PrivateIpAddress | |
[String[]]$PrivateHostAddresses = $ListOfInstances | Sort-Object -Property State, ServerAlias ` | |
| Out-GridView -PassThru ` | |
| Select-Object PrivateIpAddress -ExpandProperty PrivateIpAddress | |
try { | |
$Credentials = $host.ui.PromptForCredential( ` | |
"EC2 Instance SSH Credential Prompt" ` | |
, "Please enter your username and password" ` | |
, "" ` | |
, "NetBiosUserName") | |
} | |
catch { | |
Write-Error "Failed to get SSH Credentials!" | |
Exit 1 #error! | |
} | |
ForEach($IPAddress in $PrivateHostAddresses) { | |
[String[]]$CommandArguments = "-ssh", ($Credentials.GetNetworkCredential().UserName + "@" + $IPAddress), "-pw", ($Credentials.GetNetworkCredential().Password); | |
try { | |
Start-Process "putty" -ArgumentList $CommandArguments | |
} | |
catch { | |
Write-Error "Could not launch PuTTY!" | |
Exit 2 #error! | |
} | |
finally { | |
Clear-Variable CommandArguments | |
Remove-Variable CommandArguments | |
} | |
} | |
#Ensure we aren't holding onto anything. | |
Clear-Variable Credentials | |
Remove-Variable Credentials |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment