Skip to content

Instantly share code, notes, and snippets.

@BlueDoge
Created July 15, 2022 08:46
Show Gist options
  • Save BlueDoge/9ca281d99a8e20e90577940355a502db to your computer and use it in GitHub Desktop.
Save BlueDoge/9ca281d99a8e20e90577940355a502db to your computer and use it in GitHub Desktop.
Jump into EC2 Instances with plink and scp a tar.bz2 to the home\tmp folder
#Created by Elizabeth Clements in 2022
#License MIT
$AWSRegion = 'us-west-2'
$AWSProfileLocation = ("$env:USERPROFILE\.aws\credentials")
$AWSProfileName = 'default'
#Should resolve to <working_directory>\plink-commands.txt
$PLink_CommandFilePath = ((Get-Location | Select-Object Path -ExpandProperty Path) + "\plink-commands.txt")
$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 `
| Out-GridView -PassThru
[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!
}
$PromptedCommandFilePath = Read-Host -Prompt "Command File Path - Default [$PLink_CommandFilePath]"
if($PromptedCommandFilePath)
{
$PLink_CommandFilePath = $PromptedCommandFilePath
}
ForEach($IPAddress in $PrivateHostAddresses)
{
Start-Process "plink" `
-ArgumentList `
@("-ssh", ($Credentials.GetNetworkCredential().UserName + "@" + $IPAddress), `
"-pw", ($Credentials.GetNetworkCredential().Password), `
"-m", $PLink_CommandFilePath)
}
#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