-
Notifications
You must be signed in to change notification settings - Fork 14
/
Connect-EC2.ps1
44 lines (38 loc) · 2.55 KB
/
Connect-EC2.ps1
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
function Connect-EC2
{
<#
.Synopsis
Connects to an EC2 instance
.Description
Connects to an EC2 instance with Remote Desktop
.Example
Get-EC2 -Name MyServer |
Connect-EC2
.Link
Get-EC2
#>
[CmdletBinding(SupportsShouldProcess='true', ConfirmImpact='High')]
[OutputType([string])]
param(
# The ID of the EC2 Instance
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[string]
$InstanceId
)
process {
$ec2 = Get-EC2 -InstanceId $instanceId
$ec2Cred = $ec2 | Get-EC2InstancePassword -AsCredential
$rdpFile =
New-RDPFile -ComputerName $ec2.PublicDnsName -Credential $ec2Cred
if (-not (Test-Path "$home\EC2RDP")) {
$null = New-Item -ItemType Directory -Path "$home\EC2RDP"
}
$rdpFileName = "$home\EC2RDP\$($ec2.PublicDnsName)_$(Get-Random).rdp"
[io.file]::WriteAllText($rdpfilename,$rdpFile.trim())
$ec2 |
Open-EC2Port -Range 3389 -ErrorAction SilentlyContinue
Invoke-Item $rdpFileName
Start-Sleep -Milliseconds 500
Remove-Item -Path $rdpFileName
}
}