Using -Not with If

by May 13, 2015

I have some code that is checking an HL7-type file for certain segments.  I read chunks of the file into a byte array.  The objective here is to validate that some specific segments exist in the file.

First I need to verify there are header and trailer records:

If ($bytes -match 'HDR|' -And $bytes -match 'TRL|')

If it passes that test, I then need to verify another pair of segments.  The issue here is that if they exist, I don't need to do anything, but if either or both of them do not exist, set a flag.  I can't figure out the correct format for using -Not.  I have tried every combination of parenthesis around these, but its not working.  Here's the current attempt:

If (-Not ($bytes -match 'CHD|' -And $bytes -match 'CTR|')) {$BadFile = $true}

Of course, I could write it this way, but as I said, if this evaluates as true, there's nothing to do:

If ($bytes -match 'CHD|' -And $bytes -match 'CTR|') {nothing to do}
Else {$BadFile = $true}

What am I missing with using -Not?