@@ -69,6 +69,10 @@ public SwitchParameter Launch
69
69
set ;
70
70
}
71
71
72
+ const string PublicIPAddressResource = "publicIPAddresses" ;
73
+ const string NetworkInterfaceResouce = "networkInterfaces" ;
74
+ const string LoadBalancerResouce = "loadBalancers" ;
75
+
72
76
public override void ExecuteCmdlet ( )
73
77
{
74
78
base . ExecuteCmdlet ( ) ;
@@ -85,21 +89,19 @@ public override void ExecuteCmdlet()
85
89
// Get Azure VM
86
90
var vmResponse = this . VirtualMachineClient . Get ( this . ResourceGroupName , this . Name ) ;
87
91
92
+ var nicId = vmResponse . VirtualMachine . NetworkProfile . NetworkInterfaces . First ( ) . ReferenceUri ;
93
+
88
94
// Get the NIC
89
- var nicResourceGroupName =
90
- this . GetResourceGroupName ( vmResponse . VirtualMachine . NetworkProfile . NetworkInterfaces . First ( ) . ReferenceUri ) ;
95
+ var nicResourceGroupName = this . GetResourceGroupName ( nicId ) ;
91
96
92
- var nicName =
93
- this . GetResourceName (
94
- vmResponse . VirtualMachine . NetworkProfile . NetworkInterfaces . First ( ) . ReferenceUri , "networkInterfaces" ) ;
97
+ var nicName = this . GetResourceName ( nicId , NetworkInterfaceResouce ) ;
95
98
96
- var nic =
97
- this . NetworkClient . NetworkManagementClient . NetworkInterfaces . Get ( nicResourceGroupName , nicName ) ;
99
+ var nic = this . NetworkClient . NetworkManagementClient . NetworkInterfaces . Get ( nicResourceGroupName , nicName ) ;
98
100
99
- if ( nic . IpConfigurations . First ( ) . PublicIPAddress != null && ! string . IsNullOrEmpty ( nic . IpConfigurations . First ( ) . Id ) )
101
+ if ( nic . IpConfigurations . First ( ) . PublicIPAddress != null && ! string . IsNullOrEmpty ( nic . IpConfigurations . First ( ) . PublicIPAddress . Id ) )
100
102
{
101
103
// Get PublicIPAddress resource if present
102
- address = this . GetAddressFromPublicIPResource ( nic . IpConfigurations . First ( ) . Id ) ;
104
+ address = this . GetAddressFromPublicIPResource ( nic . IpConfigurations . First ( ) . PublicIPAddress . Id ) ;
103
105
}
104
106
else if ( nic . IpConfigurations . First ( ) . LoadBalancerInboundNatRules . Any ( ) )
105
107
{
@@ -108,7 +110,7 @@ public override void ExecuteCmdlet()
108
110
// Get ipaddress and port from loadbalancer
109
111
foreach ( var nicRuleRef in nic . IpConfigurations . First ( ) . LoadBalancerInboundNatRules )
110
112
{
111
- var lbName = this . GetResourceName ( nicRuleRef . Id , "loadBalancers" ) ;
113
+ var lbName = this . GetResourceName ( nicRuleRef . Id , LoadBalancerResouce ) ;
112
114
var lbResourceGroupName = this . GetResourceGroupName ( nicRuleRef . Id ) ;
113
115
114
116
var loadbalancer =
@@ -200,10 +202,10 @@ public override void ExecuteCmdlet()
200
202
private string GetAddressFromPublicIPResource ( string resourceId )
201
203
{
202
204
string address = string . Empty ;
203
-
205
+
204
206
// Get IpAddress from public IPAddress resource
205
207
var publicIPResourceGroupName = this . GetResourceGroupName ( resourceId ) ;
206
- var publicIPName = this . GetResourceName ( resourceId , "publicIPAddresses" ) ;
208
+ var publicIPName = this . GetResourceName ( resourceId , PublicIPAddressResource ) ;
207
209
208
210
var publicIp =
209
211
this . NetworkClient . NetworkManagementClient . PublicIPAddresses . Get (
@@ -230,7 +232,16 @@ private string GetResourceGroupName(string resourceId)
230
232
231
233
private string GetResourceName ( string resourceId , string resource )
232
234
{
233
- return resourceId . Split ( '/' ) [ 8 ] ;
235
+ int resourceTypeLocation = resourceId . IndexOf ( resource , StringComparison . OrdinalIgnoreCase ) ;
236
+
237
+ var resourceName = resourceId . Substring ( resourceTypeLocation + resource . Length + 1 ) ;
238
+ if ( resourceName . Contains ( "/" ) )
239
+ {
240
+ int resourceNameEnd = resourceName . IndexOf ( "/" ) ;
241
+ resourceName = resourceName . Substring ( 0 , resourceNameEnd ) ;
242
+ }
243
+
244
+ return resourceName ;
234
245
}
235
246
}
236
247
}
0 commit comments