1

This is my PowerShell script.

$geturl = Invoke-WebRequest "https://www.sample.com/"
$li=$geturl.ParsedHtml.body.getElementsByTagName('li') | 
    Where {$_.getAttributeNode('class').Value -eq 'col-md-6'}
$ip = $li | select OuterText

This is what the script returns:

outerText                                               
---------                                               
 179.60.147.106 (1986 reports from 281 distinct users)  
 195.226.194.242 (1000 reports from 274 distinct users) 
 195.226.194.142 (957 reports from 260 distinct users)  
 61.177.173.50 (857 reports from 201 distinct users)    
 61.177.172.114 (708 reports from 200 distinct users)   
 61.177.173.36 (869 reports from 199 distinct users)    
 61.177.173.53 (847 reports from 198 distinct users)    
 61.177.173.49 (818 reports from 198 distinct users)    
 39.109.86.40 (287 reports from 197 distinct users)     
 61.177.172.124 (786 reports from 194 distinct users)

I would like save only IP Addresses to txt file. Is it possible?

mklement0
  • 382,024
  • 64
  • 607
  • 775

1 Answers1

0

Using the regex from this answer you can use a ForEach-Object to enumerate each object, extract the IPs and store them in a file:

$re = [regex] '((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}'
$li | ForEach-Object { $re.Match($_.OuterText).Value } |
    Set-Content path\to\extractedIps.txt

The code here assumes that all values of .OuterText property have an IP Address to be extracted. If you're unsure about this then it would be better to use an if condition:

$li | ForEach-Object {
    if($_.OuterText -match '((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}') {
        $Matches[0]
    }
} | Set-Content path\to\extractedIps.txt
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37