Skip to content

Commit 2bf4c0f

Browse files
committed
Add RDP for VMss VMs
1 parent f8c3974 commit 2bf4c0f

File tree

2 files changed

+104
-17
lines changed

2 files changed

+104
-17
lines changed

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

Lines changed: 96 additions & 17 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,40 +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+
4674
[Parameter(
4775
Mandatory = true,
4876
Position = 2,
4977
ValueFromPipelineByPropertyName = true,
5078
HelpMessage = "Path and name of the output RDP file.",
51-
ParameterSetName = "Download")]
79+
ParameterSetName = "StandAloneVMDownload")]
5280
[Parameter(
5381
Mandatory = false,
5482
Position = 2,
5583
ValueFromPipelineByPropertyName = true,
5684
HelpMessage = "Path and name of the output RDP file.",
57-
ParameterSetName = "Launch")]
85+
ParameterSetName = "StandAloneVMLaunch")]
86+
[Parameter(
87+
Mandatory = true,
88+
Position = 2,
89+
ValueFromPipelineByPropertyName = true,
90+
HelpMessage = "Path and name of the output RDP file.",
91+
ParameterSetName = "ScaleSetVMDownload")]
92+
[Parameter(
93+
Mandatory = false,
94+
Position = 2,
95+
ValueFromPipelineByPropertyName = true,
96+
HelpMessage = "Path and name of the output RDP file.",
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

72-
const string PublicIPAddressResource = "publicIPAddresses";
73-
const string NetworkInterfaceResouce = "networkInterfaces";
74-
const string LoadBalancerResouce = "loadBalancers";
75-
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+
76130
public override void ExecuteCmdlet()
77131
{
78132
base.ExecuteCmdlet();
@@ -86,17 +140,41 @@ public override void ExecuteCmdlet()
86140
string address = string.Empty;
87141
int port = defaultPort;
88142

89-
// Get Azure VM
90-
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);
91149

92-
var nicId = vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri;
150+
var nicId = vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri;
93151

94-
// Get the NIC
95-
var nicResourceGroupName = this.GetResourceGroupName(nicId);
152+
// Get the NIC
153+
var nicResourceGroupName = this.GetResourceGroupName(nicId);
96154

97-
var nicName = this.GetResourceName(nicId, NetworkInterfaceResouce);
155+
var nicName = this.GetResourceName(nicId, NetworkInterfaceResouce);
98156

99-
var nic = this.NetworkClient.NetworkManagementClient.NetworkInterfaces.Get(nicResourceGroupName, nicName);
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;
166+
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+
}
100178

101179
if (nic.IpConfigurations.First().PublicIPAddress != null && !string.IsNullOrEmpty(nic.IpConfigurations.First().PublicIPAddress.Id))
102180
{
@@ -174,7 +252,7 @@ public override void ExecuteCmdlet()
174252
WindowStyle = ProcessWindowStyle.Hidden
175253
};
176254

177-
if (this.LocalPath == null)
255+
if (string.IsNullOrEmpty(this.LocalPath))
178256
{
179257
string scriptGuid = Guid.NewGuid().ToString();
180258

@@ -225,6 +303,7 @@ private string GetAddressFromPublicIPResource(string resourceId)
225303

226304
return address;
227305
}
306+
228307
private string GetResourceGroupName(string resourceId)
229308
{
230309
return resourceId.Split('/')[4];

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)