I have a large txt file with hundreds of entries, which looks like this:
AUT-NE;server1.test.root.local;10.164.202.0;30-254
AUT-DE;server2.test.root.local;10.164.202.0;30-254
AUT-LI;server3.test.root.local;10.164.22.0;1-254
....
So the structure of the txt files is: location; servername; IP Network Address; IP Range
And I want to compare an IP-address (=>a string) with the txt file and check which server would be the right one. For example: A client has the IP address 10.164.22.101, then the correct server would be server3.test.root.local because 10.164.22.101 is in the range of 10.164.22.1 -10.164.22.254.
So my output in this case should be somehting like this: "Match found: server3"
How can I do this with Powershell?
I've got
$ip = "10.164.22.101"
$sep = $ip.LastIndexOf(".")
$network = $ip.substring(0,$sep)
$node = $ip.substring($sep+1)
So $network is 10.164.22 and $node is 101.
But how can I do the right node comparison and find the matching server?
THANKS!
I was just trying, but bit more complex and not quickest against a huge list
$servers = Get-content C:\PSProject\somefile.txt
$ip = "10.164.22.1"
foreach ($server in $servers) {
$range = $server.split(";")[2] #to get ip address from server variable
$numbertoCheck = $server.Split(";")[3] # to get the range 0-254
[int]$firstNum = $numbertoCheck.Split("-")[0] # 0
[int]$endNum = $numbertoCheck.Split("-")[1] # 254
$fromRange = $range.Substring(0, $range.LastIndexOf(".")) # to get 10.164.22 from server variable
$fromIP = $ip.Substring(0, $ip.LastIndexOf(".")) # to get 10.164.22 from ip variable
[int]$iplastOctet = $ip.split(".")[3] # to get last octet from ip variable
if ( ($fromIP -eq $fromRange) -and ([int]$firstNum -le [int]$iplastOctet) -and ([int]$endNum -ge [int]$iplastOctet) ) { # if True
Write-Host "Match found in $($server.Split(";")[1])"
}
}