0
votes

I need to fetch NIC name from Public IP.I am stuck at fetching NIC name from IP configuration.

I've IP configuration of PIP as:

/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/resourceGroups/NetworkWatcherRG/providers/Microsoft.Network/networkInterfaces/testnic/ipConfigurations/ipconfig1

$ip.IpConfiguration.Id.tostring().Substring($ip.IpConfiguration.Id.ToString().LastIndexOf('/')+1)

I expect output as 'testnic' but it's giving me output 'ipconfig1'

1
Do you mean you're getting the wrong value from your $...Substring()... line? If $ip.IpConfiguration.Id is the value you posted above then that's obviously going to return you the string after the final /, i.e. ipconfig1, because that's what your Substring and LastIndexOf do. Or did you mean you expected $ip.IpConfiguration.Id to give you 'testnic'?Rup
Yes after final / I will get ipconfig1. But I can't understand how to play with indexof() to get 'testnic'. @RupEkansh Singh
$ip.IpConfiguration.Id.tostring().Split('/')[-3]iRon
Thanks @iRon it actually worked.Could you share me a link which can provide me info on such functions for strings.Ekansh Singh

1 Answers

1
votes
$ip.IpConfiguration.Id.tostring().Split('/')[-3]

Split('/') is a .Net method of the String class which returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.

You might also use the PowerShell -Split operator for this which has some more features that do not apply to this question:

($ip.IpConfiguration.Id.tostring() -Split '/')[-3]

[-3] is not a part of the method but an index in the array returned by the split method. Negative numbers count from the end of the array. For example, "-1" refers to the last element of the array. See: Reading an array