I have a script that pulls information from an open source IPAM tool. The script, using the native API, returns an xml string like so:
<HostList> <Host>172.20.1.49,FH-INET-TK-FW-P,FH,,Default gateway for segment,</Host> <Host>172.20.3.1,FW1FHNGXINT,FH,Network - Firewall,Cluster IP. Default Gateway for segment ,</Host> <Host>172.24.56.1,FW1OKNGXINT,OK,Network - Firewall,Default Gateway for the segment,Reserved (Network)</Host> <Host>172.25.80.1,Available,OK,,CAUTION!! This is NOT the default gateway. The real default gateway is 172.2 5.85.1,See Comment</Host> <Host>172.31.200.254,FW1OKNGXINT,OK,,Default Gateway for the segment,</Host> </HostList>
This is all returned in a single string $bn. I am attempting to use regex to pull the various items out, with the intent to eventually be able to find the default gateway for a given network segment. This is what I am using currently for the regex:
$hosts = [regex]::new("(?<=Host>)(.*)(?=</Host>)") $hosts.Matches($bn) | ForEach-Object { write-host "HostName: $(($_.Groups[1].Value).Split(",")[1])" }
The problem is I am only getting some of the return. I get three of the 5 records returned, but the others don’t show up for some reason. I tried changing the regex from greedy (.) to non (.?) but that is truly the limit of my regex skills (still makes my eyes bleed). Just curious if anyone sees anything I am doing wrong on the regex before I think of another way to get the data like casting to xml.