|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.IO; |
| 17 | +using System.Linq; |
| 18 | +using System.Management.Automation; |
| 19 | +using Microsoft.Azure.Commands.Compute.Common; |
| 20 | +using Microsoft.Azure.Management.Compute; |
| 21 | +using Microsoft.Azure.Management.Network; |
| 22 | + |
| 23 | +namespace Microsoft.Azure.Commands.Compute |
| 24 | +{ |
| 25 | + [Cmdlet(VerbsCommon.Get, ProfileNouns.RemoteDesktopFile)] |
| 26 | + public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseCmdlet |
| 27 | + { |
| 28 | + [Parameter( |
| 29 | + Mandatory = true, |
| 30 | + Position = 0, |
| 31 | + ValueFromPipelineByPropertyName = true, |
| 32 | + HelpMessage = "The resource group name.")] |
| 33 | + [ValidateNotNullOrEmpty] |
| 34 | + public override string ResourceGroupName { get; set; } |
| 35 | + |
| 36 | + [Alias("ResourceName", "VMName")] |
| 37 | + [Parameter( |
| 38 | + Mandatory = true, |
| 39 | + Position = 1, |
| 40 | + ValueFromPipelineByPropertyName = true, |
| 41 | + HelpMessage = "The resource name.")] |
| 42 | + [ValidateNotNullOrEmpty] |
| 43 | + public override string Name { get; set; } |
| 44 | + |
| 45 | + [Parameter( |
| 46 | + Mandatory = true, |
| 47 | + Position = 2, |
| 48 | + ValueFromPipelineByPropertyName = true, |
| 49 | + HelpMessage = "Path and name of the output RDP file.")] |
| 50 | + [ValidateNotNullOrEmpty] |
| 51 | + public string LocalPath { get; set;} |
| 52 | + |
| 53 | + public override void ExecuteCmdlet() |
| 54 | + { |
| 55 | + base.ExecuteCmdlet(); |
| 56 | + |
| 57 | + const string fullAddressPrefix = "full address:s:"; |
| 58 | + const string promptCredentials = "prompt for credentials:i:1"; |
| 59 | + const int defaultPort = 3389; |
| 60 | + |
| 61 | + string address = string.Empty; |
| 62 | + int port = defaultPort; |
| 63 | + |
| 64 | + // Get Azure VM |
| 65 | + var vmResponse = this.VirtualMachineClient.Get(this.ResourceGroupName, this.Name); |
| 66 | + |
| 67 | + // Get the NIC |
| 68 | + var nicResourceGroupName = |
| 69 | + this.GetResourceGroupName(vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri); |
| 70 | + |
| 71 | + var nicName = |
| 72 | + this.GetResourceName( |
| 73 | + vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri, "networkInterfaces"); |
| 74 | + |
| 75 | + var nicResponse = |
| 76 | + this.NetworkClient.NetworkResourceProviderClient.NetworkInterfaces.Get(nicResourceGroupName, nicName); |
| 77 | + |
| 78 | + if (nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress != null && !string.IsNullOrEmpty(nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress.Id)) |
| 79 | + { |
| 80 | + // Get PublicIPAddress resource if present |
| 81 | + address = this.GetAddressFromPublicIPResource(nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress.Id); |
| 82 | + } |
| 83 | + else if (nicResponse.NetworkInterface.IpConfigurations.First().LoadBalancerInboundNatRules.Any()) |
| 84 | + { |
| 85 | + address = string.Empty; |
| 86 | + |
| 87 | + // Get ipaddress and port from loadbalancer |
| 88 | + foreach (var nicRuleRef in nicResponse.NetworkInterface.IpConfigurations.First().LoadBalancerInboundNatRules) |
| 89 | + { |
| 90 | + var lbName = this.GetResourceName(nicRuleRef.Id, "loadBalancers"); |
| 91 | + var lbResourceGroupName = this.GetResourceGroupName(nicRuleRef.Id); |
| 92 | + |
| 93 | + var loadbalancer = |
| 94 | + this.NetworkClient.NetworkResourceProviderClient.LoadBalancers.Get(lbResourceGroupName, lbName).LoadBalancer; |
| 95 | + |
| 96 | + // Iterate over the InboundNatRules where Backendport = 3389 |
| 97 | + var inboundRule = |
| 98 | + loadbalancer.InboundNatRules.Where( |
| 99 | + rule => |
| 100 | + rule.BackendPort == defaultPort |
| 101 | + && string.Equals( |
| 102 | + rule.Id, |
| 103 | + nicRuleRef.Id, |
| 104 | + StringComparison.OrdinalIgnoreCase)); |
| 105 | + |
| 106 | + if (inboundRule.Any()) |
| 107 | + { |
| 108 | + port = inboundRule.First().FrontendPort; |
| 109 | + |
| 110 | + // Get the corresponding frontendIPConfig -> publicIPAddress |
| 111 | + var frontendIPConfig = |
| 112 | + loadbalancer.FrontendIpConfigurations.First( |
| 113 | + frontend => |
| 114 | + string.Equals( |
| 115 | + inboundRule.First().FrontendIPConfiguration.Id, |
| 116 | + frontend.Id, |
| 117 | + StringComparison.OrdinalIgnoreCase)); |
| 118 | + |
| 119 | + if (frontendIPConfig.PublicIpAddress != null) |
| 120 | + { |
| 121 | + address = this.GetAddressFromPublicIPResource(frontendIPConfig.PublicIpAddress.Id); |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (string.IsNullOrEmpty(address)) |
| 128 | + { |
| 129 | + throw new ArgumentException(Properties.Resources.VirtualMachineNotAssociatedWithPublicLoadBalancer); |
| 130 | + } |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + throw new ArgumentException(Properties.Resources.VirtualMachineNotAssociatedWithPublicIPOrPublicLoadBalancer); |
| 135 | + } |
| 136 | + |
| 137 | + // Write to file |
| 138 | + using (var file = new StreamWriter(this.LocalPath)) |
| 139 | + { |
| 140 | + file.WriteLine(fullAddressPrefix + address + ":" + port); |
| 141 | + file.WriteLine(promptCredentials); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private string GetAddressFromPublicIPResource(string resourceId) |
| 146 | + { |
| 147 | + string address = string.Empty; |
| 148 | + |
| 149 | + // Get IpAddress from public IPAddress resource |
| 150 | + var publicIPResourceGroupName = this.GetResourceGroupName(resourceId); |
| 151 | + var publicIPName = this.GetResourceName(resourceId, "publicIPAddresses"); |
| 152 | + |
| 153 | + var publicIpResponse = |
| 154 | + this.NetworkClient.NetworkResourceProviderClient.PublicIpAddresses.Get( |
| 155 | + publicIPResourceGroupName, |
| 156 | + publicIPName); |
| 157 | + |
| 158 | + |
| 159 | + // Use the FQDN if present |
| 160 | + if (publicIpResponse.PublicIpAddress.DnsSettings != null |
| 161 | + && !string.IsNullOrEmpty(publicIpResponse.PublicIpAddress.DnsSettings.Fqdn)) |
| 162 | + { |
| 163 | + address = publicIpResponse.PublicIpAddress.DnsSettings.Fqdn; |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + address = publicIpResponse.PublicIpAddress.IpAddress; |
| 168 | + } |
| 169 | + |
| 170 | + return address; |
| 171 | + } |
| 172 | + private string GetResourceGroupName(string resourceId) |
| 173 | + { |
| 174 | + return resourceId.Split('/')[4]; |
| 175 | + } |
| 176 | + |
| 177 | + private string GetResourceName(string resourceId, string resource) |
| 178 | + { |
| 179 | + return resourceId.Split('/')[8]; |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments