array IndexOf IPv4 subnets

Hi

I have an array of subnets like

PS C:\> $Subnet2update

Subnet         
------         
10.255.77.0/24
10.255.78.0/24
10.255.152.0/24
10.255.157.0/24
10.255.158.0/24
10.9.44.0/23   
10.9.46.0/23   
10.9.34.0/23   
10.9.50.0/24   
10.255.237.0/24
10.9.48.0/23   
10.9.32.0/23   
10.9.36.0/23   
10.9.51.0/24   
10.9.40.0/24   
10.9.39.0/24   
10.9.38.0/24   
10.9.41.0/24   
10.9.59.0/24 

I like to find the index of a subnet in the array

[array]::IndexOf($Subnet2update,10.9.48.0/23) but it give me always a False

What do I wrong? (PS version is 5.1 on Win10)

Is it because I'm working with IPv4 addresses?

Parents
  • How are you generating and passing in this array.
    If you are getting false, that means this is not an array but a string.

    You also need to quote that CIDR to be syntactically correct. Special characters have meaning in PowerShell, unless that are coerced as a string. Hence the need for the quotes.

        ($ips = 1..254 | % {"10.10.10.$_/24"})

        # Results

        10.10.10.1/24
        10.10.10.2/24
        10.10.10.3/24
        10.10.10.4/24
        10.10.10.5/24
        ...

        [array]::IndexOf($ips, '10.10.10.3/24')
        2

        [array]::IndexOf($ips, 10.10.10.3/24)
        -1

Reply Children
No Data