Skip to content

Instantly share code, notes, and snippets.

@DexterPOSH
Forked from pcgeek86/azrdp.ps1
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterPOSH/08559f60ea8296ade0a8 to your computer and use it in GitHub Desktop.
Save DexterPOSH/08559f60ea8296ade0a8 to your computer and use it in GitHub Desktop.
function azrdp {
<#
.Author
Trevor Sullivan <pcgeek86@gmail.com>
.Description
Invoke a RDP session to an Azure Virtual Machine, without having to type the
Cloud Service name or Virtual Machine name.
.Outputs
None
.Links
http://trevorsullivan.net
http://twitter.com/pcgeek86
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[switch] $PromptSubscription
)
$AzureReauthenticate = {
$AuthResult = Add-AzureAccount 3>&1;
if ($AuthResult -match 'User canceled authentication') {
Write-Error -Message 'User canceled authentication, exiting function.';
return;
}
# Re-invoke the azrdp function using the same arguments that the user passed in originally
$Parameters = (Get-PSCallStack)[1].InvocationInfo.BoundParameters;
& $PSCmdlet.MyInvocation.MyCommand.Name @Parameters;
}
try {
if ($PromptSubscription) {
Write-Verbose -Message 'Prompting for Azure subscription.';
$SubscriptionList = Get-AzureSubscription;
if (!$SubscriptionList) {
& $AzureReauthenticate;
}
$Subscription = $SubscriptionList | Select-Object -Property SubscriptionName,SubscriptionId | Out-GridView -PassThru -Title 'Select your Azure Subscription';
if (!$Subscription) {
Write-Warning -Message 'No Azure subscription was selected.';
return;
}
Select-AzureSubscription -SubscriptionName $Subscription.SubscriptionName;
}
#One can select multiple VMs to connect to.
$VMs = @(Get-AzureVM -ErrorAction Stop | Out-GridView -PassThru -Title 'Select your Azure Virtual Machine';)
if (!$VMs) {
Write-Warning -Message 'No virtual machine was selected.';
return;
}
foreach ($VM in $VMs) {
#get the Public RDP Port for the VM
$PublicRDPPort = Get-AzureEndpoint -Name RemoteDesktop -VM $VM | Select-Object -ExpandProperty Port;
#Check if the Remote Azure VM is accepting incoming requests for RDP
if (Test-NetConnection -ComputerName ([uri]($VM.DNSName)).host -Port $PublicRDPPort -InformationLevel Quiet) {
Write-Verbose -Message ('Invoking Remote Desktop session to {0}\{1}' -f $VM.ServiceName, $VM.Name);
Get-AzureRemoteDesktopFile -ServiceName $VM.ServiceName -Name $VM.Name -Launch;
}
else {
Write-Warning -Message "Remote Azure VM named $($Vm.Name) not responding to RDP requests on Port $PublicRDPPort"
}
}
}
catch [System.ArgumentException] {
if ($PSItem.Exception.Message -match 'Add-AzureAccount') {
& $AzureReauthenticate -;
}
}
catch [System.ApplicationException] {
if ($PSItem.Exception.Message -match 'No default subscription') {
& $AzureReauthenticate;
}
}
}
@DexterPOSH
Copy link
Author

Revision 8 & 9

++ ChangeLog ++

  • Wrapped selected VMs by the User in an Array, One can select to connect to multiple VMs.
  • Checking if the Public RDP port for the Remote VM is serving connections before downloading the RDP file.
  • Uses Test-NetConnection cmdlet which is only available on Windows 8 and above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment