-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect-EC2Instance.psm1
163 lines (124 loc) · 6.33 KB
/
Connect-EC2Instance.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
Function Connect-EC2Instance{
<#
.SYNOPSIS
Connects to EC2 instances in AWS without needing to decrypt the password manually using a pem file.
.DESCRIPTION
Automatically figues out password for instance if the pem file is found, and connects via RDP.
Assumes development environment with AWS default profile name "dev".
Looks to default path in your documents for pem files under $environment directory.
You can specify path and file name of pem if needed.
.PARAMETER InstanceId
The AWS Id of the instance.
.PARAMETER Environment
The AWS profile name, usually mapped to an environment name.
.PARAMETER KeyName
If not gotten from the properties of the EC2 instance, the pem key file name.
.PARAMETER KeyPath
The path to the pem key file name.
.PARAMETER Username
The Username to use to log on to the EC2 instance if not the default 'Administrator'.
.PARAMETER Region
The AWS region to use - defaults to ap-southeast-2.
.PARAMETER Protocol
The protocol to connect with - can be RDP or SSH.
.EXAMPLE
Connect-Ec2instance i-12345678
No arguments given. Assumes "dev" AWS profile. Figures out pem key name from EC2 instance properties.
.EXAMPLE
Connect-Ec2instance i-12345678 -Environment production
Connects to EC2 instance in the "production" AWS profile. Figures out pem key name from EC2 instance properties.
.EXAMPLE
Connect-Ec2instance i-12345678 -Environment production -KeyName myproductionpem.pem -KeyPath c:\super\secret\path
Specify pem key filename and path, if not defaults.
.EXAMPLE
Connect-EC2Instance i-12345678 -Username DifferentAdministrator -KeyName myproductionpem.pem -KeyPath c:\super\secret\path
Specify non default username to connect with. Specify pem key filename and path, if not defaults.
.EXAMPLE
Connect-EC2Instance -Protocol ssh -port 22 -InstanceIds i-12345678 -Environment production -KeyName myproductionpem.pem -KeyPath c:\super\secret\path -Username ec2-user
Connect via SSH on port 22. Uses the "production" AWS environment.Specifies pem key filename and path. Specifies username to connect with.
.NOTES
Written to work with Jaap Brasser's Connect-Mstsc function - https://gallery.technet.microsoft.com/scriptcenter/Connect-Mstsc-Open-RDP-2064b10b
Author: Kirk Brady
Site: https://github.com/kirkbrady
Version History
1.0.2 - Enhancements to InstanceIds array support.
Added support for SSH protocol connections.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[Alias("Instances","Computers","ComputerNames","MachineNames","PrivateIpAddress")]
[string[]]$InstanceIds,
[Parameter(Mandatory=$false,Position=1)]
[Alias("Env","Profile","ProfileName")]
[string]$Environment="dev",
[Parameter(Mandatory=$false,Position=2)]
[Alias("Key","Pem")]
[string]$KeyName,
[Parameter(Mandatory=$false,Position=3)]
[Alias("Path","PemPath")]
[string]$KeyPath="$env:USERPROFILE\Documents\pem\$Environment",
[Parameter(Mandatory=$false,Position=4)]
[Alias("User")]
[string]$Username="Administrator",
[Parameter(Mandatory=$false,Position=5)]
[string]$Region="ap-southeast-2",
[Parameter(Mandatory=$false,Position=6)]
[ValidateSet(“SSH”,”RDP”)]
[Alias("ConnectWith","ConnectionType","Connection")]
[string]$Protocol="RDP",
[Parameter(Mandatory=$false,Position=7)]
[string]$Port="3389"
)
Begin {
If(Initialize-AWSDefaults -ProfileName $Environment -Region $Region){
Write-Output "Initialized AWS defaults for environment `"$Environment`"."
}
}
Process {
Try {
Foreach($InstanceId in $InstanceIds){
If(!$KeyName){
[string]$KeyName=(Get-EC2Instance -InstanceId $InstanceId).Instances.Keyname
}
$PemFile = (gci $Keypath\* -include *.pem| where {$_.name -match $keyname}).FullName
if($PemFile){
If(Test-Path $Pemfile){
$PrivateIP = (Get-EC2Instance $InstanceId).RunningInstance.PrivateIpAddress;
If(!$PrivateIP){
Throw "Could not obtain private ip value for $InstanceId."
}
if(!((Get-EC2Instance -InstanceId $InstanceId).Instances.Platform.Value -eq "windows")){
$Protocol = "SSH";
$Port = 22;
$Ami = (Get-EC2Instance -InstanceId $InstanceId).Instances.ImageId
$Platform = (Get-EC2Image -ImageId $Ami).Name
Switch -wildcard ($Platform){
"ubuntu*" { $Username = "ubuntu" }
default { $Username = "ec2-user" }
}
}
Write-Output "Connecting to instance $InstanceId in environment $Environment on IP $PrivateIP using $Protocol on platform $Platform."
Write-Output "Target PEM file is $PemFile.`n"
Switch($Protocol){
"RDP" {
$Pass = Get-EC2PasswordData -InstanceId $InstanceId -PemFile $PemFile -Decrypt;
Connect-Mstsc -computername $PrivateIP -password $Pass -user $Username -fullscreen;
}
"SSH" {
start-process ssh -ArgumentList @("-i", "$PemFile" ,"$Username@$PrivateIP", "-p $Port")
}
}
}
} else {
Throw "PEM file value is invalid - please check."
}
}
}
Catch {
$_.Exception
}
}
End {
}
}