penfold Posted August 30, 2021 Posted August 30, 2021 I need to check the configuration of a few different places where IP details are used. I can grab the data, but because there is no standard, it means the IP details could have been entered in a number of ways so I want to be able to check the first 3 parts of the address. So if I have a source of 192.168.1.10, it doesn't matter if what I am comparing it to is 192.168.1.1-192.168.1.254, 192.168.1.0/24, 192.168.1.55 etc. I want to be able to identify where there is definitely no match so I can start to look here first. I was thinking of using split to be able to grab just the first part of the address and using -like to see if I can find a match but I'm a little lost now. I'd like to grab just the first part of the address so I drop everything after the 3rd "." and then compare 2 sources but I'm not sure how to do this? I thought there was a way to split left of the 3rd "." char but I can't find out how to do this. Or is there an easier/better way to do this?
Steve21 Posted August 30, 2021 Posted August 30, 2021 (edited) At a very basic level you could just split it and compare the first 3 (no need to do left of . etc) $IP1 = "192.168.1.0" $IP2 = "192.168.5.1" $IPArray1 = $IP1.split(".") $IPArray2 = $IP2.split(".") if (($IPArray1[0] -eq $IPArray2[0]) -and ($IPArray1[1] -eq $IPArray2[1]) -and ($IPArray1[2] -eq $IPArray2[2])) { Write-Host "True" } else { Write-Host "False" } Deffo a neater way to do it, but whether it's worth the time is another matter The only issue with splitting it will be things like if you have a subnet that would cover multiple IPs in the 2nd/3rd compare, which this wouldn't show up Steve Edited August 30, 2021 by Steve21 1
penfold Posted January 28, 2022 Author Posted January 28, 2022 Just coming back to this. I have a number of IP which I want to check which Networks they are in. I have been able to basically drop the last number using the split method thanks to @Steve21 but now I want to find a match in a csv. I am using the following I have a csv file which has details about the network range and location. I want to be able to get the location in another cell if there is a match in the searchterm using code here $searchTerm = "1.1.1."$temp= $csv |Where-Object {$_.Network -match $searchTerm} |Select-Object Network,city The problem is that the $searchterm like above matches multiple times if there is an entry 1.1.1. and 1.1.10, 1.1.1.11 etc For some reason it is ignoring the last "." in the searchterm and therefore getting multiple matches. I've tried -eq and -like, but I do not get any matches that way. Is there something I am missing or will I have to go about it another way? What I want to be able to do is just match specifically "1.1.1." which is what I thought I was doing
Steve21 Posted January 28, 2022 Posted January 28, 2022 Change your search term to ^1.1.1.$ does that work? Note that wasn’t a typo add both parts Steve
tdk1069 Posted January 28, 2022 Posted January 28, 2022 As I understand it. -match is regex so you would want $searchTerm = "1\.1\.1\." ❯ $searchTerm = "1\.1\.1\." ❯ "1.1.1." -match $searchTerm True ❯ "1.1.10." -match $searchTerm False 1
penfold Posted January 28, 2022 Author Posted January 28, 2022 updated to 1\.1\.1\. and it works. Thanks
tdk1069 Posted January 28, 2022 Posted January 28, 2022 in regex "." is 'Any single character' so it would have matched 121314 as well. \. just escapes the "." to be a literal dot.. regex101.com is my bible 2
PowerShellWolf Posted June 30, 2022 Posted June 30, 2022 (edited) I use Regex for this: $IP = "10.75.241.225";if (-not ($IP -imatch "^([0-9]+).([0-9]+).([0-9]+).([0-9]+)$")) { Write-Host "Invalid IP Address";} else { Write-Host $Matches[1] Write-Host $Matches[2] Write-Host $Matches[3] Write-Host $Matches[4]} Also, if you need a pattern that also checks the IP is valid (each octet is 0-255) then: ^(2[0-4][0-9]|25[0-5]|[0-1]?[0-9]?[0-9]).(2[0-4][0-9]|25[0-5]|[0-1]?[0-9]?[0-9]).(2[0-4][0-9]|25[0-5]|[0-1]?[0-9]?[0-9]).(2[0-4][0-9]|25[0-5]|[0-1]?[0-9]?[0-9])$ P.S why do my code blocks ignore newlines on here? Edited June 30, 2022 by Rusketh
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now