How to select multi-line strings (including spaces), return true if found - PowerShell/SQL Batch

403 views Asked by At

I have used select-string to validate strings found on one line, however I need PowerShell to recognise multi-line strings, as well as meeting all three conditions (see below)

Output within .log file that needs to be selected:

Return Value
------------
           0

I would want something to validate the following string values (all three to be met to return true)

"Return Value", "------------", "           0"

I have tried this, however this is the equivalent of OR not AND in a filter

| Select-String -pattern "Return Value", "------------", "           0"

Sources: http://ss64.com/ps/select-string.html : http://www.sqlservercentral.com/Forums/Topic1253397-1351-1.aspx

1

There are 1 answers

2
Martin Brandl On BEST ANSWER

You can use regex with the multiline option:

$a = 
@'
Return Value
------------
0
'@

$b = 
@'
Return Value
------------
1
'@

[regex]::Match($a, 'Return Value\s*------------\s*0', [System.Text.RegularExpressions.RegexOptions]::Multiline).Success # true
[regex]::Match($b, 'Return Value\s*------------\s*0', [System.Text.RegularExpressions.RegexOptions]::Multiline).Success # false

If you post an exmpale of your log, I can adapt the script more to your needs.