|
| 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 | +// TODO: NRP |
| 16 | +// |
| 17 | +//using System; |
| 18 | +//using System.Diagnostics; |
| 19 | +//using System.IO; |
| 20 | +//using System.Linq; |
| 21 | +//using System.Management.Automation; |
| 22 | +//using Microsoft.Azure.Commands.Compute.Common; |
| 23 | +//using Microsoft.Azure.Management.Compute; |
| 24 | +//using Microsoft.Azure.Management.Network; |
| 25 | + |
| 26 | +//namespace Microsoft.Azure.Commands.Compute |
| 27 | +//{ |
| 28 | +// [Cmdlet(VerbsCommon.Get, ProfileNouns.RemoteDesktopFile)] |
| 29 | +// public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseCmdlet |
| 30 | +// { |
| 31 | +// [Parameter( |
| 32 | +// Mandatory = true, |
| 33 | +// Position = 0, |
| 34 | +// ValueFromPipelineByPropertyName = true, |
| 35 | +// HelpMessage = "The resource group name.")] |
| 36 | +// [ValidateNotNullOrEmpty] |
| 37 | +// public string ResourceGroupName { get; set; } |
| 38 | + |
| 39 | +// [Alias("ResourceName", "VMName")] |
| 40 | +// [Parameter( |
| 41 | +// Mandatory = true, |
| 42 | +// Position = 1, |
| 43 | +// ValueFromPipelineByPropertyName = true, |
| 44 | +// HelpMessage = "The resource name.")] |
| 45 | +// [ValidateNotNullOrEmpty] |
| 46 | +// public string Name { get; set; } |
| 47 | + |
| 48 | +// [Parameter( |
| 49 | +// Mandatory = true, |
| 50 | +// Position = 2, |
| 51 | +// ValueFromPipelineByPropertyName = true, |
| 52 | +// HelpMessage = "Path and name of the output RDP file.", |
| 53 | +// ParameterSetName = "Download")] |
| 54 | +// [Parameter( |
| 55 | +// Mandatory = false, |
| 56 | +// Position = 2, |
| 57 | +// ValueFromPipelineByPropertyName = true, |
| 58 | +// HelpMessage = "Path and name of the output RDP file.", |
| 59 | +// ParameterSetName = "Launch")] |
| 60 | +// [ValidateNotNullOrEmpty] |
| 61 | +// public string LocalPath { get; set; } |
| 62 | + |
| 63 | +// [Parameter( |
| 64 | +// Mandatory = true, |
| 65 | +// Position = 3, |
| 66 | +// HelpMessage = "Start a remote desktop session to the specified role instance.", |
| 67 | +// ParameterSetName = "Launch")] |
| 68 | +// public SwitchParameter Launch |
| 69 | +// { |
| 70 | +// get; |
| 71 | +// set; |
| 72 | +// } |
| 73 | + |
| 74 | +// protected override void ProcessRecord() |
| 75 | +// { |
| 76 | +// base.ProcessRecord(); |
| 77 | + |
| 78 | +// ExecuteClientAction(() => |
| 79 | +// { |
| 80 | +// const string fullAddressPrefix = "full address:s:"; |
| 81 | +// const string promptCredentials = "prompt for credentials:i:1"; |
| 82 | +// const int defaultPort = 3389; |
| 83 | + |
| 84 | +// string address = string.Empty; |
| 85 | +// int port = defaultPort; |
| 86 | + |
| 87 | +// // Get Azure VM |
| 88 | +// var vmResponse = this.VirtualMachineClient.Get(this.ResourceGroupName, this.Name); |
| 89 | + |
| 90 | +// // Get the NIC |
| 91 | +// var nicResourceGroupName = |
| 92 | +// this.GetResourceGroupName(vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri); |
| 93 | + |
| 94 | +// var nicName = |
| 95 | +// this.GetResourceName( |
| 96 | +// vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri, "networkInterfaces"); |
| 97 | + |
| 98 | +// var nicResponse = |
| 99 | +// this.NetworkClient.NetworkResourceProviderClient.NetworkInterfaces.Get(nicResourceGroupName, nicName); |
| 100 | + |
| 101 | +// if (nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress != null && !string.IsNullOrEmpty(nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress.Id)) |
| 102 | +// { |
| 103 | +// // Get PublicIPAddress resource if present |
| 104 | +// address = this.GetAddressFromPublicIPResource(nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress.Id); |
| 105 | +// } |
| 106 | +// else if (nicResponse.NetworkInterface.IpConfigurations.First().LoadBalancerInboundNatRules.Any()) |
| 107 | +// { |
| 108 | +// address = string.Empty; |
| 109 | + |
| 110 | +// // Get ipaddress and port from loadbalancer |
| 111 | +// foreach (var nicRuleRef in nicResponse.NetworkInterface.IpConfigurations.First().LoadBalancerInboundNatRules) |
| 112 | +// { |
| 113 | +// var lbName = this.GetResourceName(nicRuleRef.Id, "loadBalancers"); |
| 114 | +// var lbResourceGroupName = this.GetResourceGroupName(nicRuleRef.Id); |
| 115 | + |
| 116 | +// var loadbalancer = |
| 117 | +// this.NetworkClient.NetworkResourceProviderClient.LoadBalancers.Get(lbResourceGroupName, lbName).LoadBalancer; |
| 118 | + |
| 119 | +// // Iterate over the InboundNatRules where Backendport = 3389 |
| 120 | +// var inboundRule = |
| 121 | +// loadbalancer.InboundNatRules.Where( |
| 122 | +// rule => |
| 123 | +// rule.BackendPort == defaultPort |
| 124 | +// && string.Equals( |
| 125 | +// rule.Id, |
| 126 | +// nicRuleRef.Id, |
| 127 | +// StringComparison.OrdinalIgnoreCase)); |
| 128 | + |
| 129 | +// if (inboundRule.Any()) |
| 130 | +// { |
| 131 | +// port = inboundRule.First().FrontendPort; |
| 132 | + |
| 133 | +// // Get the corresponding frontendIPConfig -> publicIPAddress |
| 134 | +// var frontendIPConfig = |
| 135 | +// loadbalancer.FrontendIpConfigurations.First( |
| 136 | +// frontend => |
| 137 | +// string.Equals( |
| 138 | +// inboundRule.First().FrontendIPConfiguration.Id, |
| 139 | +// frontend.Id, |
| 140 | +// StringComparison.OrdinalIgnoreCase)); |
| 141 | + |
| 142 | +// if (frontendIPConfig.PublicIpAddress != null) |
| 143 | +// { |
| 144 | +// address = this.GetAddressFromPublicIPResource(frontendIPConfig.PublicIpAddress.Id); |
| 145 | +// break; |
| 146 | +// } |
| 147 | +// } |
| 148 | +// } |
| 149 | + |
| 150 | +// if (string.IsNullOrEmpty(address)) |
| 151 | +// { |
| 152 | +// throw new ArgumentException(Microsoft.Azure.Commands.Compute.Properties.Resources.VirtualMachineNotAssociatedWithPublicLoadBalancer); |
| 153 | +// } |
| 154 | +// } |
| 155 | +// else |
| 156 | +// { |
| 157 | +// throw new ArgumentException(Microsoft.Azure.Commands.Compute.Properties.Resources.VirtualMachineNotAssociatedWithPublicIPOrPublicLoadBalancer); |
| 158 | +// } |
| 159 | + |
| 160 | +// // Write to file |
| 161 | +// string rdpFilePath = this.LocalPath ?? Path.GetTempFileName(); |
| 162 | + |
| 163 | +// using (var file = new StreamWriter(rdpFilePath)) |
| 164 | +// { |
| 165 | +// file.WriteLine(fullAddressPrefix + address + ":" + port); |
| 166 | +// file.WriteLine(promptCredentials); |
| 167 | +// } |
| 168 | + |
| 169 | +// if (Launch.IsPresent) |
| 170 | +// { |
| 171 | +// var startInfo = new ProcessStartInfo |
| 172 | +// { |
| 173 | +// CreateNoWindow = true, |
| 174 | +// WindowStyle = ProcessWindowStyle.Hidden |
| 175 | +// }; |
| 176 | + |
| 177 | +// if (this.LocalPath == null) |
| 178 | +// { |
| 179 | +// string scriptGuid = Guid.NewGuid().ToString(); |
| 180 | + |
| 181 | +// string launchRDPScript = Path.GetTempPath() + scriptGuid + ".bat"; |
| 182 | +// using (var scriptStream = File.OpenWrite(launchRDPScript)) |
| 183 | +// { |
| 184 | +// var writer = new StreamWriter(scriptStream); |
| 185 | +// writer.WriteLine("start /wait mstsc.exe " + rdpFilePath); |
| 186 | +// writer.Flush(); |
| 187 | +// } |
| 188 | + |
| 189 | +// startInfo.FileName = launchRDPScript; |
| 190 | +// } |
| 191 | +// else |
| 192 | +// { |
| 193 | +// startInfo.FileName = "mstsc.exe"; |
| 194 | +// startInfo.Arguments = rdpFilePath; |
| 195 | +// } |
| 196 | + |
| 197 | +// Process.Start(startInfo); |
| 198 | +// } |
| 199 | +// }); |
| 200 | +// } |
| 201 | + |
| 202 | +// private string GetAddressFromPublicIPResource(string resourceId) |
| 203 | +// { |
| 204 | +// string address = string.Empty; |
| 205 | + |
| 206 | +// // Get IpAddress from public IPAddress resource |
| 207 | +// var publicIPResourceGroupName = this.GetResourceGroupName(resourceId); |
| 208 | +// var publicIPName = this.GetResourceName(resourceId, "publicIPAddresses"); |
| 209 | + |
| 210 | +// var publicIpResponse = |
| 211 | +// this.NetworkClient.NetworkResourceProviderClient.PublicIpAddresses.Get( |
| 212 | +// publicIPResourceGroupName, |
| 213 | +// publicIPName); |
| 214 | + |
| 215 | + |
| 216 | +// // Use the FQDN if present |
| 217 | +// if (publicIpResponse.PublicIpAddress.DnsSettings != null |
| 218 | +// && !string.IsNullOrEmpty(publicIpResponse.PublicIpAddress.DnsSettings.Fqdn)) |
| 219 | +// { |
| 220 | +// address = publicIpResponse.PublicIpAddress.DnsSettings.Fqdn; |
| 221 | +// } |
| 222 | +// else |
| 223 | +// { |
| 224 | +// address = publicIpResponse.PublicIpAddress.IpAddress; |
| 225 | +// } |
| 226 | + |
| 227 | +// return address; |
| 228 | +// } |
| 229 | +// private string GetResourceGroupName(string resourceId) |
| 230 | +// { |
| 231 | +// return resourceId.Split('/')[4]; |
| 232 | +// } |
| 233 | + |
| 234 | +// private string GetResourceName(string resourceId, string resource) |
| 235 | +// { |
| 236 | +// return resourceId.Split('/')[8]; |
| 237 | +// } |
| 238 | +// } |
| 239 | +//} |
0 commit comments