question

Sara925 avatar image
0 Votes"
Sara925 asked RichMatheisen-8856 edited

powershell filter

,

     #This will only send the alerts.

                          If (($SendAlert) -and (-not (((get-date).TimeOfDay.totalhours -ge 1) -and ((get-date).timeofday.totalhours -lt 4))))

                          {

                                        $Message = @"

$server is not converting

Information: $ErrorResult


**Ok ,I just need a additional line to be added on the script to remove alert and put in a filter like

-and ($errorresult notmatch "E:\Program Files (x86)\data")** , The question is ,can this be added along these lines

If (($SendAlert) -and (-not (((get-date).TimeOfDay.totalhours -ge 1) -and ((get-date).timeofday.totalhours -lt 4)))) ? or where and how it can be added effectively?

windows-server-powershell
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

19605-powershell.txt




The entire script!

0 Votes 0 ·
powershell.txt (5.9 KiB)

I think I'd rewrite that "if" to make it a bit less convoluted by testing for the positive instead of tossing in a "-not"?. Try this instead:

If ($SendAlert -and (((get-date).TimeOfDay.Hours -lt 1) -and ((get-date).timeofday.Hours -gt 3)))

As for you question, the answer is that you can do that, but the $ErrorResult doesn't hold a simple string. It holds an error object. I'm going to guess that the string you're looking for is in the ErrorDetails property of that object. If so, add this before the last parenthesis in the code above:

-and $ErrorResult.ErrorDetails -notlike "E:\Program Files (x86)\data"


Note that I'm using the simpler "-notlike" which makes more sense in this context since you really aren't looking to memorize the "x86" match in your regular expression and the left- and right-parentheses are part of the sting you want to match, not a group boundary.





0 Votes 0 ·
StoyanChalakov avatar image
1 Vote"
StoyanChalakov answered RichMatheisen-8856 edited

Hi @Saravanaraj-6475 ,

I would put the last condition in brackets, like that:

  If ($SendAlert -and (((get-date).TimeOfDay.Hours -lt 1) -and ((get-date).timeofday.Hours -gt 3))-and ($ErrorResult.ErrorDetails -notlike "E:\Program Files (x86)\data"))

hope this helps.


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)
Best Regards!
Stoyan

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Looking again at what I suggested, the parentheses surrounding the two individual time checks and the intervening "-and" are unnecessary. They were there in the original post but they were necessary then because of the "-not". The parentheses surrounding the individual time checks are also unnecessary. This works fine:

If ($SendAlert -and (get-date).TimeOfDay.Hours -lt 1 -and (get-date).timeofday.Hours -gt 3 -and $ErrorResult.ErrorDetails -notlike "E:\Program Files (x86)\data")


You can add parentheses to lend some clarity to the thing, but they're not necessary for PowerShell to make sense of what your intentions are.

If you want to make your logic a bit more clear, you an also try this:

 If ($SendAlert -and 
     (get-date).TimeOfDay.Hours -lt 1 -and 
     (get-date).timeofday.Hours -gt 3 -and 
     $ErrorResult.ErrorDetails -notlike "*E:\Program Files (x86)\data*"
 )


1 Vote 1 ·
Sara925 avatar image
0 Votes"
Sara925 answered

Thanks Rich,

Does anybody else have any other suggestions? , If ($SendAlert -and (((get-date).TimeOfDay.Hours -lt 1) -and ((get-date).timeofday.Hours -gt 3))-and $ErrorResult.ErrorDetails -notlike "E:\Program Files (x86)\data")

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.