1

I am trying to search a text file for a specific string and output the line and its context:

$StateCheck = Select-String -Path C:\ImageManagerTool\Results.txt -Pattern "State:*"
$SValueCheck = Select-String -Path C:\ImageManagerTool\Results.txt -Pattern "State Value:*"

If(Get-Content C:\ImageManagerTool\Results.txt | %{$StateCheck -notmatch "State: Active"})
{
    Get-Content C:\ImageManagerTool\Results.txt | Select-String -Pattern "State:*"-Context 2,7| Select-String -Pattern "State: Active" -NotMatch |Select-String -Pattern "State Value:*" -NotMatch
} 

If(Get-Content C:\ImageManagerTool\Results.txt | %{$SValueCheck -notmatch "State Value: 0"})
{
    Get-Content C:\ImageManagerTool\Results.txt | Select-String -Pattern "State Value:*"-Context 3,6| Select-String -Pattern "State Value: 0" -NotMatch
}

Results of Above Script:

Results of Above Script

So far, all that I've been able to accomplish is outputting the string instances I want to find, but not their context. With the current setup of 'if' statements to filter the data for strings that don't match, I haven't figured out if using the parameter -context will work in this situation, since -context isn't valid on receiving Select-String commands. When reading Microsoft's description of -Context on the PowerShell site, it states that context is stored as an array of strings in the context property of an object. Is there a way I can either rewrite my script pieces to get the desired effect(i.e. a switch?) or utilize the context parameter as a property to output its data?

1
  • Instead nof reading the file C:\ImageManagerTool\Results.txt several times, store it in a variable and act on it. Also show the content by editing your question. Commented Jun 18, 2018 at 17:47

1 Answer 1

1

Your if statements are completely unnecessary - Select-String will simply not return anything if nothing matches the pattern you supplied.

For the second regex match, you can inspect the matched line with Where-Object instead of chaining Select-String:

Select-String -Path C:\ImageManagerTool\Results.txt -Pattern "State:*"-Context 2,7|Where-Object {$_.Line -notmatch "State: Active" -and $_.Line -notmatch "State Value:*"}

The second example can be simplified even further, simply make sure whatever comes after State Value: is not 0 - you can use a negated character set like this [^0]:

Select-String -Path C:\ImageManagerTool\Results.txt -Pattern "State Value: [^0]" -Context 3,6
Sign up to request clarification or add additional context in comments.

1 Comment

The line for 'State Value' works great, but the line for 'State' is only showing pieces of data, and running both of these lines in the same script will run each of their queries separate from each other(i.e. output will show line 308 and then jump to line 183 and start from there). In the case of 'State', is the context within the search of Where-Object?

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.