-
-
Save DexterPOSH/08559f60ea8296ade0a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 8 & 9
++ ChangeLog ++