1

I have working Import-Csv statement that uses Where-Object

Import-Csv D:\Script\my.csv | Where-Object {$_.SourceIP -Like '10.251.22.11*' -Or $_.SourceIP -Like '10.251.22.*' -Or $_.DestinationIP -Like '10.251.22.11*' -Or $_.DestinationIP -Like '10.251.22.*'}

If I try to simplify the statement, it doesn't work

Import-Csv D:\Script\my.csv | Where-Object {($_.SourceIP -Like ('10.251.22.11*' -Or '10.251.22.*')) -Or ($_.DestinationIP -like ('10.251.22.11*' -Or '10.251.22.*'))}

Google is not helping :-(

1
  • 1
    -like accepts only a single string with wildcards. See help about_Operators for details. There is no -or for -like. Commented May 11, 2015 at 16:53

1 Answer 1

1

Instead of -like, use -match in this case.

Import-Csv D:\Script\my.csv | Where-Object {$_.SourceIP -match '^10\.251\.22\.*' -or $_.DestinationIP -match '^10\.251\.22\.*'}

Also, 10.251.22.* will match 10.251.22.11*, so you can combine them.

Sign up to request clarification or add additional context in comments.

6 Comments

Instead of -like, use -match: I does not think that OP want to match this 123.210.251.225 address by this 10.251.22.* pattern ('123.210.251.225' -match '10.251.22.*' returns true).
@PetSerAl Good catch, thanks. Added the '^' character to indicate start of string.
You also need to do something with dots, as them means any char, rather than literal dot, for example you matching to 10.251.225.123. At least you should use literal dot at the end of pattern ^10.251.22\..
Presuming those dots are a part of an IP address, she should just escape them. Thanks
You still matching to 10.251.225.123, as you apply * (zero or more) to last dot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.