23
23
24
24
namespace Microsoft . Azure . Commands . Compute
25
25
{
26
+ using Microsoft . Azure . Management . Network . Models ;
27
+
26
28
[ Cmdlet ( VerbsCommon . Get , ProfileNouns . RemoteDesktopFile ) ]
27
29
public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseCmdlet
28
30
{
31
+ const string PublicIPAddressResource = "publicIPAddresses" ;
32
+ const string NetworkInterfaceResouce = "networkInterfaces" ;
33
+ const string LoadBalancerResouce = "loadBalancers" ;
34
+
29
35
[ Parameter (
30
36
Mandatory = true ,
31
37
Position = 0 ,
@@ -39,36 +45,88 @@ public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseC
39
45
Mandatory = true ,
40
46
Position = 1 ,
41
47
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" ) ]
43
56
[ ValidateNotNullOrEmpty ]
44
57
public string Name { get ; set ; }
45
58
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" ) ]
46
86
[ Parameter (
47
87
Mandatory = true ,
48
88
Position = 2 ,
49
89
ValueFromPipelineByPropertyName = true ,
50
90
HelpMessage = "Path and name of the output RDP file." ,
51
- ParameterSetName = "Download " ) ]
91
+ ParameterSetName = "ScaleSetVMDownload " ) ]
52
92
[ Parameter (
53
93
Mandatory = false ,
54
94
Position = 2 ,
55
95
ValueFromPipelineByPropertyName = true ,
56
96
HelpMessage = "Path and name of the output RDP file." ,
57
- ParameterSetName = "Launch " ) ]
97
+ ParameterSetName = "ScaleSetVMLaunch " ) ]
58
98
[ ValidateNotNullOrEmpty ]
59
99
public string LocalPath { get ; set ; }
60
100
61
101
[ Parameter (
62
102
Mandatory = true ,
63
103
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" ) ]
66
111
public SwitchParameter Launch
67
112
{
68
113
get ;
69
114
set ;
70
115
}
71
116
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
+
72
130
public override void ExecuteCmdlet ( )
73
131
{
74
132
base . ExecuteCmdlet ( ) ;
@@ -82,24 +140,46 @@ public override void ExecuteCmdlet()
82
140
string address = string . Empty ;
83
141
int port = defaultPort ;
84
142
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 ) ;
87
154
88
- // Get the NIC
89
- var nicResourceGroupName =
90
- this . GetResourceGroupName ( vmResponse . VirtualMachine . NetworkProfile . NetworkInterfaces . First ( ) . ReferenceUri ) ;
155
+ var nicName = this . GetResourceName ( nicId , NetworkInterfaceResouce ) ;
91
156
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 ;
95
166
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
+ }
98
178
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 ) )
100
180
{
101
181
// Get PublicIPAddress resource if present
102
- address = this . GetAddressFromPublicIPResource ( nic . IpConfigurations . First ( ) . Id ) ;
182
+ address = this . GetAddressFromPublicIPResource ( nic . IpConfigurations . First ( ) . PublicIPAddress . Id ) ;
103
183
}
104
184
else if ( nic . IpConfigurations . First ( ) . LoadBalancerInboundNatRules . Any ( ) )
105
185
{
@@ -108,7 +188,7 @@ public override void ExecuteCmdlet()
108
188
// Get ipaddress and port from loadbalancer
109
189
foreach ( var nicRuleRef in nic . IpConfigurations . First ( ) . LoadBalancerInboundNatRules )
110
190
{
111
- var lbName = this . GetResourceName ( nicRuleRef . Id , "loadBalancers" ) ;
191
+ var lbName = this . GetResourceName ( nicRuleRef . Id , LoadBalancerResouce ) ;
112
192
var lbResourceGroupName = this . GetResourceGroupName ( nicRuleRef . Id ) ;
113
193
114
194
var loadbalancer =
@@ -172,7 +252,7 @@ public override void ExecuteCmdlet()
172
252
WindowStyle = ProcessWindowStyle . Hidden
173
253
} ;
174
254
175
- if ( this . LocalPath == null )
255
+ if ( string . IsNullOrEmpty ( this . LocalPath ) )
176
256
{
177
257
string scriptGuid = Guid . NewGuid ( ) . ToString ( ) ;
178
258
@@ -200,10 +280,10 @@ public override void ExecuteCmdlet()
200
280
private string GetAddressFromPublicIPResource ( string resourceId )
201
281
{
202
282
string address = string . Empty ;
203
-
283
+
204
284
// Get IpAddress from public IPAddress resource
205
285
var publicIPResourceGroupName = this . GetResourceGroupName ( resourceId ) ;
206
- var publicIPName = this . GetResourceName ( resourceId , "publicIPAddresses" ) ;
286
+ var publicIPName = this . GetResourceName ( resourceId , PublicIPAddressResource ) ;
207
287
208
288
var publicIp =
209
289
this . NetworkClient . NetworkManagementClient . PublicIPAddresses . Get (
@@ -223,14 +303,24 @@ private string GetAddressFromPublicIPResource(string resourceId)
223
303
224
304
return address ;
225
305
}
306
+
226
307
private string GetResourceGroupName ( string resourceId )
227
308
{
228
309
return resourceId . Split ( '/' ) [ 4 ] ;
229
310
}
230
311
231
312
private string GetResourceName ( string resourceId , string resource )
232
313
{
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 ;
234
324
}
235
325
}
236
326
}
0 commit comments