I am still learning Powershell (after using vbscript for many years) and still trying to wrap my head around different ways of doing things. Right now, I am working on a script to parse the output from gprsult (redirected to a txt file) and return
if a policy is blocked or not. I have included my code and a snippet of the gpresult below.
One issue is my dothing check. I first tried to use true or false but that didn't work So I tried swapping to something else and it still doesn't work as expected.
What I was expecting with the code is
Start ForEach
if $line matches then Dothing = true
Else (else so that we go to the top of the foreach and not continue processing into the Dothing)
If doThing = true then
Dothing = false (set this so that only 1 check is performed)
if $line = not applied then $blocked = True (exit for)
Else $blocked = false (exit For)
End foreach
Blocked
but, if I don't include a break in the if/else check it keeps dipping into the dothing. even if dothing = false
I hope that this makes sense. What am I doing wrong?
$file1 = "C:\Users\bouchard\result.txt" $file2 = "C:\Users\bouchard\result_admin.txt" $lines = Get-Content $file1 $doThing = "b"#$false foreach ($line in $lines) { Write-Host "First line" If ($line -match "Policy 5") { $doThing = "c" } else #added as would immediately go into the dothing { if ($doThing = "c") { $doThing = "b" If ($line -match "Not Applied") { #Write-Host $doThing + $line $blocked = "Policy is blocked" #break } else { #Write-Host $line $blocked = "Policy is applied" #break } } } } Write-Host $blocked
Snippet of the txt from the gpresult
Applied Group Policy Objects
-----------------------------
Policy 01
Policy 02
Policy 03
Policy 04
Policy 08
The following GPOs were not applied because they were filtered out
-------------------------------------------------------------------
Policy 05 (move this line to just below policy 04 for file1 testing)
Filtering: Not Applied (Unknown Reason)
Policy 06
Filtering: Not Applied (Empty)
Policy 07
Filtering: Not Applied (Empty)
The computer is a part of the following security groups