Skip to content

Commit

Permalink
Bug fixes and updates, mainly around optional parameter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
richardcloudsoft committed Sep 27, 2021
1 parent 84bf1e8 commit f567555
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
35 changes: 23 additions & 12 deletions EC2Access.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ function Get-EC2Password {
[Parameter(Position=2)] [string]$PrivateKeyFile
)

$ErrorActionPreference = "Stop"

# Verify the private key files exists
if($null -eq $PrivateKeyFile) {
if(!($PrivateKeyFile)) {
$PrivateKeyFile = $HOME + '\.ssh\id_rsa'
Write-Verbose "No private key file given - defaulting to $($PrivateKeyFile)"
}
if(-not (Test-Path $PrivateKeyFile)) {
Write-Error "$($PrivateKeyFile) does not exist. Do you need to use -PrivateKeyFile argument?"
}

Write-Verbose "Requesting password data from AWS"
$cipherText = (Get-EC2PasswordData -Region $Region -InstanceId $InstanceId)
if ($Region) {
$cipherText = Get-EC2PasswordData -InstanceId $InstanceId -Region $Region
} else {
$cipherText = Get-EC2PasswordData -InstanceId $InstanceId
}

Write-Verbose "Decrypting password"
$password = Convert-RSAEncryptedCipherTextToClearText -PemFile $PrivateKeyFile -CipherText $cipherText
Expand Down Expand Up @@ -120,12 +123,16 @@ function Start-DirectEC2RemoteDesktop {
[Parameter(Position=2)] [string]$PrivateKeyFile
)

$password = Get-EC2Password -Instance $InstanceId -Region $Region -PrivateKeyFile $PrivateKeyFile
$password = Get-EC2Password -InstanceId $InstanceId -Region $Region -PrivateKeyFile $PrivateKeyFile
$Credential = New-Object PSCredential "Administrator",$password

$instance = (Get-EC2Instance -Region $Region -InstanceId $InstanceId).Instances[0]
$HostName = $instance.PublicIpAddress
Write-Verbose "Instance IP address is $HostName"
if($Region) {
$response = Get-EC2Instance -InstanceId $InstanceId -Region $Region
} else {
$response = Get-EC2Instance -InstanceId $InstanceId
}
$HostName = $response.Instances[0].PublicIpAddress
Write-Verbose "Instance address is $HostName"

if ($PSCmdlet.ShouldProcess($InstanceId,'Start remote desktop session')) {
Start-RemoteDesktop -HostName $HostName -Credential $Credential
Expand Down Expand Up @@ -160,12 +167,16 @@ function Start-EC2RemoteDesktopViaSessionManager {
[Parameter(Position=2)] [string]$PrivateKeyFile
)

$password = Get-EC2Password -Instance $InstanceId -Region $Region -PrivateKeyFile $PrivateKeyFile
$password = Get-EC2Password -InstanceId $InstanceId -Region $Region -PrivateKeyFile $PrivateKeyFile
$Credential = New-Object PSCredential "Administrator",$password

$LocalPort = 33389
$PortForwardParams = @{ portNumber=(,"3389"); localPortNumber=(,$LocalPort.ToString()) }
$session = Start-SSMSession -Target $InstanceId -Region $Region -DocumentName AWS-StartPortForwardingSession -Parameters $PortForwardParams
if($Region) {
$session = Start-SSMSession -Target $InstanceId -DocumentName AWS-StartPortForwardingSession -Parameters $PortForwardParams -Region $Region
} else {
$session = Start-SSMSession -Target $InstanceId -DocumentName AWS-StartPortForwardingSession -Parameters $PortForwardParams
}

# We now need to emulate awscli - it invokes session-manager-plugin with the new session information.
# AWS Tools for PowerShell don't do this. Also some of the objects seem to look a bit different, and the
Expand Down Expand Up @@ -235,8 +246,8 @@ function Start-EC2RemoteDesktopViaSessionManager {

function Start-RemoteDesktop {
[CmdletBinding(SupportsShouldProcess)] param(
[Parameter(Mandatory=$true, Position=0)] [String] [string]$HostName,
[Parameter(Mandatory=$true, Position=1)] [PSCredential] [string]$Credential,
[Parameter(Mandatory=$true, Position=0)] [String] $HostName,
[Parameter(Mandatory=$true, Position=1)] [PSCredential] $Credential,
[Parameter()] [Int32] [string]$Port
)

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ to get a private key.
same name with a `.pub` extension. Then, using the *Import* button on the EC2 *Key pairs* page to import
the public key.

By default, the functions in this module will assume that your private key is in the ".ssh\id_rsa" file in
By default, the functions in this module will assume that your private key is in the `.ssh\id_rsa` file in
your home directory, which will be the normal situation in method 2 above. If you have used method 1, or have
your key in any other location, simply pass a "-PrivateKeyFile" parameter to the functions with the path to
your key in any other location, simply pass a `-PrivateKeyFile` parameter to the functions with the path to
your private key file.


Expand Down Expand Up @@ -117,7 +117,7 @@ Start-EC2RemoteDesktopViaSessionManager -InstanceId i-12345678abcd `
```

If your EC2 instance is reachable on its public IP address, then instead of
"Start-EC2RemoteDesktopViaSessionManager", you can invoke "Start-DirectEC2RemoteDesktop". This function takes
`Start-EC2RemoteDesktopViaSessionManager`, you can invoke `Start-DirectEC2RemoteDesktop`. This function takes
exactly the same parameters but uses the public IP address instead of Session Manager port forwarding.

```powershell
Expand Down

0 comments on commit f567555

Please sign in to comment.