简体   繁体   中英

How to use “OR” and pipe in windows powershell command for findstr

I am trying to parse below Powershell command for getting list of mac address

getmac | findstr "Device" | select-string '(..-){5}' | ForEach-Object{$_ -replace '^(..-..-..-..-..-..).+$','$1'}

it gives me output like

7C-05-07-0F-F8-69

I want to add one more check to check if it is start with specific range as below

getmac | findstr "Device" | findstr "7C:05:07" | findstr "08:00:27" | findstr "00:50:56" | select-string '(..-){5}' | ForEach-Object{$_ -replace '^(..-..-..-..-..-..).+$','$1'}

But this findstr "Device" | findstr "7C:05:07" | findstr "08:00:27" | findstr "00:50:56" findstr "Device" | findstr "7C:05:07" | findstr "08:00:27" | findstr "00:50:56"

Does not work as or operation i tried below also

 getmac | (findstr "Device" -or findstr "7C:05:07" -or findstr "08:00:27" -or findstr "00:50:56")

it gives error in powershell Please help

You are getting an error because of the poorly formed expression in your last example. I imagine the error you are getting is: Expressions are only allowed as the first element of a pipeline.

You can't use -or in the way you are doing. Each side of the operator will be evaluated as a boolean. And while we can do that with subexpressions that might not get results you expect.

In general I would suggest just using this to remove the need for all the findstr's

getmac | select-string "[-A-F0-9]{17}" -AllMatches | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value

Then we could filter from there using -or

 getmac | select-string "[-A-F0-9]{17}" -AllMatches | 
    Select-Object -ExpandProperty Matches | 
    Select-Object -ExpandProperty Value | 
    Where-Object{$_ -match "7C:05:07" -or $_ -match "08:00:27" -or $_ -match "00:50:56"}

I would do something a little more terse with regex.

getmac | select-string "[-A-F0-9]{17}" -AllMatches | 
    Select-Object -ExpandProperty Matches | 
    Select-Object -ExpandProperty Value | 
    Where-Object{$_ -match "7C:05:07|08:00:27|00:50:56"}

If those have to be at the beginning you could change the last regex to the following: "^(7C:05:07|08:00:27|00:50:56)"

I would also consider just querying WMI for this information instead of text parsing.

Get-WmiObject win32_networkadapter | Where-Object{$_.MacAddress} | Select -ExpandProperty macaddress

Thanks Matt for detailed answer.

I also found a solution for a same problem i would like to post my answer.

We can use /c option from findstr for or operation in command prompt and powershell blow how i did

getmac | findstr "Device" | findstr /c:"00-15-5D" /c:"08-00-27" /c:"00-50-56" /c:"52-54-00"| select-string '(..-){5}' | ForEach-Object{$_ -replace '^(..-..-..-..-..-..).+$','$1'}

Please refer this link for more information about findstr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM