Kolob Canyon Kolob Canyon - 1 year ago 95
Bash Question

Powershell - Search for all IP addresses except in all files and subdirectories

I want to search for all IP addresses except in all files and subdirs of a given path.

I have tried:

function searchstr($string)
ls -Recurse | Select-String $string -List | Select Path

And this allows me to search for specific ip's

searchstr ""

but not all ips...

I usually do this with regular expressions in eclipse like this: 1)
ctrl + h
enter image description here

But this project is so big and has so many references to ips that eclipse dies when I try to run that regular expression on it.

I want my output to look like:


and to exclude entries with

Answer Source

Give this a try. It may be possible to modify the regex to perform the exclusion in the Select-String command but I am not sure. I only know enough about regex to get by.

Get-ChildItem -Recurse `
| Select-String -Pattern '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' `
| ? { $_.Line -ne '' } `
| Select-Object -ExpandProperty Path -Unique


Give this a try.

Get-ChildItem -Recurse `
| Select-String -Pattern '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' -AllMatches `
| ? { 
    $matches = ($_.Matches | Select-Object -Unique)
    return $matches.Count -gt 1 -or $matches[0].Value -ne '' 
} `
| Select-Object -ExpandProperty Path -Unique

This should exclude files containing lines like the one the OP brings up in the comments:

ip address =

But it will not exclude files with lines such as the following (the suggested fixes would exclude files with this line):

ip address =; ip address =

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download