Functions/Public/Invoke-AzureRmRemoteDesktop.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
function Invoke-AzureRmRemoteDesktop {
    <#
    .Author
    Trevor Sullivan <pcgeek86@gmail.com>

    .Description
    Invoke a RDP session to an Azure Virtual Machine, without having to type the
    Resource Group name or Virtual Machine name. Instead, simply select it from the
    WPF window that pops open. This command is intended to work with the Azure
    Resource Manager (ARM) PowerShell module, not Azure Service Management (ASM).

    .Outputs
    None

    .Links
    http://trevorsullivan.net
    http://twitter.com/pcgeek86
    #>

    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory = $false, ParameterSetName = 'SpecifiedName')]
        [Alias('rgname')]
        [string] $ResourceGroupName
      , [Parameter(Position = 1, Mandatory = $false, ParameterSetName = 'SpecifiedName')]
        [string] $Name
    )

    $VM = Get-AzureRmVM -ErrorAction Stop | Out-GridView -PassThru -Title 'Select your Azure Virtual Machine';

    if (!$VM) { 
        Write-Warning -Message 'No virtual machines exist in this subscription, or no virtual machine was selected. Please create a virtual machine or select a different Azure subscription';
        return; 
    }

    Write-Verbose -Message ('Invoking Remote Desktop session to {0}\{1}' -f $VM.ResourceGroupName, $VM.Name);
    Get-AzureRmRemoteDesktopFile -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Launch;
}