Skip to content

Instantly share code, notes, and snippets.

@moverperfect
Last active May 2, 2023 13:31
Show Gist options
  • Save moverperfect/654d0b843f799e9850d22d44eb1cac91 to your computer and use it in GitHub Desktop.
Save moverperfect/654d0b843f799e9850d22d44eb1cac91 to your computer and use it in GitHub Desktop.
A PowerShell script to create and manage RDP and SSH connections to Windows and Linux VMs.
<#
.SYNOPSIS
Connection_Manager.ps1 - A PowerShell script to create and manage RDP and SSH connections to Windows and Linux VMs.
.DESCRIPTION
This script manages connections to Windows and Linux VMs using RDP and SSH, respectively. It takes a server name and connection type as input or prompts the user for them.
For RDP connections, the script generates a temporary RDP file and initiates the RDP session with the provided server name and properties.
For SSH connections, the script checks if the user is logged into Azure CLI and connects using "az ssh vm" command.
.PARAMETER ServerName
The name or IP address of the server you want to connect to.
.PARAMETER ConnectionType
The type of connection you want to establish: "RDP" for Remote Desktop Protocol or "SSH" for Secure Shell.
.PARAMETER Help
Shows help information for this script.
.EXAMPLE
.\Connection_Manager.ps1 -ServerName "YourServerName" -ConnectionType "RDP"
.EXAMPLE
.\Connection_Manager.ps1 -ServerName "YourServerName" -ConnectionType "SSH"
.NOTES
Version: 1.0.0
#>
param (
[string]$ServerName,
[string]$ConnectionType,
[switch]$Help
)
$script:ServerSuffix = ".domain.com"
if ($Help)
{
Get-Help "$PSCommandPath" -Full
exit
}
if (-not $ServerName)
{
$ServerName = Read-Host "Please enter the server name"
}
function Test-IsIpAddress ([string]$value)
{
$ipv4Regex = '^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$'
$ipv6Regex = '^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$'
return ($value -match $ipv4Regex) -or ($value -match $ipv6Regex)
}
if (-not (Test-IsIpAddress $ServerName) -and $ServerName -notlike "*$ServerSuffix")
{
$ServerName = $ServerName + $ServerSuffix
}
if (-not $ConnectionType)
{
$ConnectionType = Read-Host "Please enter the connection type (RDP or SSH)"
}
if ($ConnectionType -eq "RDP")
{
$RDPFile = @"
screen mode id:i:2
use multimon:i:0
desktopwidth:i:1920
desktopheight:i:1080
session bpp:i:32
winposstr:s:0,3,0,0,800,600
compression:i:1
keyboardhook:i:2
audiocapturemode:i:0
videoplaybackmode:i:1
connection type:i:7
networkautodetect:i:1
bandwidthautodetect:i:1
enablecredsspsupport:i:0
authentication level:i:2
drivestoredirect:s:*
"@
$RDPFilePath = Join-Path $env:TEMP "temp_RDP_$([guid]::NewGuid()).rdp"
$RDPFile | Set-Content -Path $RDPFilePath
Start-Process -FilePath "mstsc.exe" -ArgumentList "/v:$ServerName /edit:$RDPFilePath"
}
elseif ($ConnectionType -eq "SSH")
{
$azAccount = az account show 2>&1
if ($azAccount -match "Please run 'az login' to setup account")
{
Write-Host "Not logged into Azure CLI, attempting to log in..."
az login
}
Write-Host "Connecting to $ServerName via SSH..."
az ssh vm --ip $ServerName
}
else
{
Write-Host "Invalid connection type. Please enter either 'RDP', 'SSH'"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment