Skip to content

Commit 3a9f03b

Browse files
committed
Merge pull request #89 from DeepakRajendranMsft/VMssRdp
Vmss rdp
2 parents 64792be + 2bf4c0f commit 3a9f03b

File tree

2 files changed

+120
-22
lines changed

2 files changed

+120
-22
lines changed

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

Lines changed: 112 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323

2424
namespace Microsoft.Azure.Commands.Compute
2525
{
26+
using Microsoft.Azure.Management.Network.Models;
27+
2628
[Cmdlet(VerbsCommon.Get, ProfileNouns.RemoteDesktopFile)]
2729
public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseCmdlet
2830
{
31+
const string PublicIPAddressResource = "publicIPAddresses";
32+
const string NetworkInterfaceResouce = "networkInterfaces";
33+
const string LoadBalancerResouce = "loadBalancers";
34+
2935
[Parameter(
3036
Mandatory = true,
3137
Position = 0,
@@ -39,36 +45,88 @@ public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseC
3945
Mandatory = true,
4046
Position = 1,
4147
ValueFromPipelineByPropertyName = true,
42-
HelpMessage = "The resource name.")]
48+
HelpMessage = "The resource name.",
49+
ParameterSetName = "StandAloneVMDownload")]
50+
[Parameter(
51+
Mandatory = true,
52+
Position = 1,
53+
ValueFromPipelineByPropertyName = true,
54+
HelpMessage = "The resource name.",
55+
ParameterSetName = "StandAloneVMLaunch")]
4356
[ValidateNotNullOrEmpty]
4457
public string Name { get; set; }
4558

59+
[Parameter(
60+
Mandatory = true,
61+
Position = 1,
62+
ValueFromPipelineByPropertyName = true,
63+
HelpMessage = "The instance of the VM in virtual machine scaleset.",
64+
ParameterSetName = "ScaleSetVMDownload")]
65+
[Parameter(
66+
Mandatory = true,
67+
Position = 1,
68+
ValueFromPipelineByPropertyName = true,
69+
HelpMessage = "The instance of the VM in virtual machine scaleset.",
70+
ParameterSetName = "ScaleSetVMLaunch")]
71+
[ValidateNotNullOrEmpty]
72+
public string InstanceId { get; set; }
73+
74+
[Parameter(
75+
Mandatory = true,
76+
Position = 2,
77+
ValueFromPipelineByPropertyName = true,
78+
HelpMessage = "Path and name of the output RDP file.",
79+
ParameterSetName = "StandAloneVMDownload")]
80+
[Parameter(
81+
Mandatory = false,
82+
Position = 2,
83+
ValueFromPipelineByPropertyName = true,
84+
HelpMessage = "Path and name of the output RDP file.",
85+
ParameterSetName = "StandAloneVMLaunch")]
4686
[Parameter(
4787
Mandatory = true,
4888
Position = 2,
4989
ValueFromPipelineByPropertyName = true,
5090
HelpMessage = "Path and name of the output RDP file.",
51-
ParameterSetName = "Download")]
91+
ParameterSetName = "ScaleSetVMDownload")]
5292
[Parameter(
5393
Mandatory = false,
5494
Position = 2,
5595
ValueFromPipelineByPropertyName = true,
5696
HelpMessage = "Path and name of the output RDP file.",
57-
ParameterSetName = "Launch")]
97+
ParameterSetName = "ScaleSetVMLaunch")]
5898
[ValidateNotNullOrEmpty]
5999
public string LocalPath { get; set; }
60100

61101
[Parameter(
62102
Mandatory = true,
63103
Position = 3,
64-
HelpMessage = "Start a remote desktop session to the specified role instance.",
65-
ParameterSetName = "Launch")]
104+
HelpMessage = "Start a remote desktop session to the specified role instance.",
105+
ParameterSetName = "StandAloneVMLaunch")]
106+
[Parameter(
107+
Mandatory = true,
108+
Position = 3,
109+
HelpMessage = "Start a remote desktop session to the specified role instance.",
110+
ParameterSetName = "ScaleSetVMLaunch")]
66111
public SwitchParameter Launch
67112
{
68113
get;
69114
set;
70115
}
71116

117+
[Parameter(
118+
Mandatory = true,
119+
ValueFromPipelineByPropertyName = true,
120+
HelpMessage = "The virtual machine scaleset name.",
121+
ParameterSetName = "ScaleSetVMDownload")]
122+
[Parameter(
123+
Mandatory = true,
124+
ValueFromPipelineByPropertyName = true,
125+
HelpMessage = "The virtual machine scaleset name.",
126+
ParameterSetName = "ScaleSetVMLaunch")]
127+
[ValidateNotNullOrEmpty]
128+
public string VirtualMachineScaleSetName { get; set; }
129+
72130
public override void ExecuteCmdlet()
73131
{
74132
base.ExecuteCmdlet();
@@ -82,24 +140,46 @@ public override void ExecuteCmdlet()
82140
string address = string.Empty;
83141
int port = defaultPort;
84142

85-
// Get Azure VM
86-
var vmResponse = this.VirtualMachineClient.Get(this.ResourceGroupName, this.Name);
143+
NetworkInterface nic;
144+
145+
if (this.ParameterSetName.Contains("StandAloneVM"))
146+
{
147+
// Get standalone Azure VM
148+
var vmResponse = this.VirtualMachineClient.Get(this.ResourceGroupName, this.Name);
149+
150+
var nicId = vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri;
151+
152+
// Get the NIC
153+
var nicResourceGroupName = this.GetResourceGroupName(nicId);
87154

88-
// Get the NIC
89-
var nicResourceGroupName =
90-
this.GetResourceGroupName(vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri);
155+
var nicName = this.GetResourceName(nicId, NetworkInterfaceResouce);
91156

92-
var nicName =
93-
this.GetResourceName(
94-
vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri, "networkInterfaces");
157+
nic = this.NetworkClient.NetworkManagementClient.NetworkInterfaces.Get(
158+
nicResourceGroupName,
159+
nicName);
160+
}
161+
else
162+
{
163+
var vmResponse = this.VirtualMachineScaleSetVMClient.Get(this.ResourceGroupName, this.VirtualMachineScaleSetName, this.InstanceId);
164+
165+
var nicId = vmResponse.VirtualMachineScaleSetVM.NetworkProfile.NetworkInterfaces.First().ReferenceUri;
95166

96-
var nic =
97-
this.NetworkClient.NetworkManagementClient.NetworkInterfaces.Get(nicResourceGroupName, nicName);
167+
// Get the NIC
168+
var nicResourceGroupName = this.GetResourceGroupName(nicId);
169+
170+
var nicName = this.GetResourceName(nicId, NetworkInterfaceResouce);
171+
172+
nic = this.NetworkClient.NetworkManagementClient.NetworkInterfaces.GetVirtualMachineScaleSetNetworkInterface(
173+
nicResourceGroupName,
174+
this.VirtualMachineScaleSetName,
175+
this.InstanceId,
176+
nicName);
177+
}
98178

99-
if (nic.IpConfigurations.First().PublicIPAddress != null && !string.IsNullOrEmpty(nic.IpConfigurations.First().Id))
179+
if (nic.IpConfigurations.First().PublicIPAddress != null && !string.IsNullOrEmpty(nic.IpConfigurations.First().PublicIPAddress.Id))
100180
{
101181
// Get PublicIPAddress resource if present
102-
address = this.GetAddressFromPublicIPResource(nic.IpConfigurations.First().Id);
182+
address = this.GetAddressFromPublicIPResource(nic.IpConfigurations.First().PublicIPAddress.Id);
103183
}
104184
else if (nic.IpConfigurations.First().LoadBalancerInboundNatRules.Any())
105185
{
@@ -108,7 +188,7 @@ public override void ExecuteCmdlet()
108188
// Get ipaddress and port from loadbalancer
109189
foreach (var nicRuleRef in nic.IpConfigurations.First().LoadBalancerInboundNatRules)
110190
{
111-
var lbName = this.GetResourceName(nicRuleRef.Id, "loadBalancers");
191+
var lbName = this.GetResourceName(nicRuleRef.Id, LoadBalancerResouce);
112192
var lbResourceGroupName = this.GetResourceGroupName(nicRuleRef.Id);
113193

114194
var loadbalancer =
@@ -172,7 +252,7 @@ public override void ExecuteCmdlet()
172252
WindowStyle = ProcessWindowStyle.Hidden
173253
};
174254

175-
if (this.LocalPath == null)
255+
if (string.IsNullOrEmpty(this.LocalPath))
176256
{
177257
string scriptGuid = Guid.NewGuid().ToString();
178258

@@ -200,10 +280,10 @@ public override void ExecuteCmdlet()
200280
private string GetAddressFromPublicIPResource(string resourceId)
201281
{
202282
string address = string.Empty;
203-
283+
204284
// Get IpAddress from public IPAddress resource
205285
var publicIPResourceGroupName = this.GetResourceGroupName(resourceId);
206-
var publicIPName = this.GetResourceName(resourceId, "publicIPAddresses");
286+
var publicIPName = this.GetResourceName(resourceId, PublicIPAddressResource);
207287

208288
var publicIp =
209289
this.NetworkClient.NetworkManagementClient.PublicIPAddresses.Get(
@@ -223,14 +303,24 @@ private string GetAddressFromPublicIPResource(string resourceId)
223303

224304
return address;
225305
}
306+
226307
private string GetResourceGroupName(string resourceId)
227308
{
228309
return resourceId.Split('/')[4];
229310
}
230311

231312
private string GetResourceName(string resourceId, string resource)
232313
{
233-
return resourceId.Split('/')[8];
314+
int resourceTypeLocation = resourceId.IndexOf(resource, StringComparison.OrdinalIgnoreCase);
315+
316+
var resourceName = resourceId.Substring(resourceTypeLocation + resource.Length + 1);
317+
if (resourceName.Contains("/"))
318+
{
319+
int resourceNameEnd = resourceName.IndexOf("/");
320+
resourceName = resourceName.Substring(0, resourceNameEnd);
321+
}
322+
323+
return resourceName;
234324
}
235325
}
236326
}

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/VirtualMachineBaseCmdlet.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ public IVirtualMachineOperations VirtualMachineClient
2525
return ComputeClient.ComputeManagementClient.VirtualMachines;
2626
}
2727
}
28+
29+
public IVirtualMachineScaleSetVMOperations VirtualMachineScaleSetVMClient
30+
{
31+
get
32+
{
33+
return ComputeClient.ComputeManagementClient.VirtualMachineScaleSetVMs;
34+
}
35+
}
2836
}
2937
}

0 commit comments

Comments
 (0)