Skip to content

Commit 0614630

Browse files
committed
add Get-RDP cmd and fix addnic
1 parent 6e0b9ea commit 0614630

File tree

8 files changed

+283
-87
lines changed

8 files changed

+283
-87
lines changed

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
<SpecificVersion>False</SpecificVersion>
7777
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.4.0.0-preview\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
7878
</Reference>
79+
<Reference Include="Microsoft.Azure.Management.Network">
80+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.2.0.0-preview\lib\net40\Microsoft.Azure.Management.Network.dll</HintPath>
81+
</Reference>
7982
<Reference Include="Microsoft.Azure.Management.Storage, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
8083
<SpecificVersion>False</SpecificVersion>
8184
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Storage.2.3.0-preview\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>
@@ -150,6 +153,8 @@
150153
<Compile Include="Models\PSVirtualMachineExtension.cs" />
151154
<Compile Include="Models\PSAvailabilitySet.cs" />
152155
<Compile Include="Models\PSVirtualMachineInstanceView.cs" />
156+
<Compile Include="RemoteDesktop\VirtualMachineRemoteDesktopBaseCmdlet.cs" />
157+
<Compile Include="RemoteDesktop\GetAzureRemoteDesktopFileCommand.cs" />
153158
<Compile Include="StorageAccount\StorageManagementClient.cs" />
154159
<Compile Include="Models\SourceImageReferenceExtension.cs" />
155160
<Compile Include="StorageAccount\GetAzureStorageAccount.cs" />
@@ -204,6 +209,10 @@
204209
<Project>{c60342b1-47d3-4a0e-8081-9b97ce60b7af}</Project>
205210
<Name>Commands.Profile</Name>
206211
</ProjectReference>
212+
<ProjectReference Include="..\..\Network\Commands.Network\Commands.Network.csproj">
213+
<Project>{98cfd96b-a6bc-4f15-ae2c-603fc2b58981}</Project>
214+
<Name>Commands.Network</Name>
215+
</ProjectReference>
207216
<ProjectReference Include="..\..\Resources\Commands.Resources\Commands.Resources.csproj">
208217
<Project>{e1f5201d-6067-430e-b303-4e367652991b}</Project>
209218
<Name>Commands.Resources</Name>

src/ResourceManager/Compute/Commands.Compute/Properties/Resources.Designer.cs

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Compute/Commands.Compute/Properties/Resources.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@
123123
<data name="VirtualMachineExtensionRemovalConfirmation" xml:space="preserve">
124124
<value>This cmdlet will remove the specified virtual machine extension. Do you want to continue?</value>
125125
</data>
126+
<data name="VirtualMachineNotAssociatedWithPublicIPOrPublicLoadBalancer" xml:space="preserve">
127+
<value>The RDP file cannot be generated because the network interface of the virtual machine does not reference a PublicIP or an InboundNatRule of a public load balancer. </value>
128+
</data>
129+
<data name="VirtualMachineNotAssociatedWithPublicLoadBalancer" xml:space="preserve">
130+
<value>The RDP file cannot be generated because the network interface of the virtual machine does not reference an InboundNatRule of a public load balancer.</value>
131+
</data>
132+
<data name="VirtualMachineReferencesInternalNetworkInterface" xml:space="preserve">
133+
<value>The RDP file cannot be generated because the network interface of the virtual machine does not reference a PublicIP or an InboungNatRule of the load balancer.</value>
134+
</data>
126135
<data name="VirtualMachineRemovalCaption" xml:space="preserve">
127136
<value>Virtual machine removal operation</value>
128137
</data>

src/ResourceManager/Compute/Commands.Compute/RemoteDesktop/GetAzureRemoteDesktopFile.cs

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)