locked
Powershell Script to Suppress Email Alerts RRS feed

  • Question

  • Good Morning,

    I have been attempting to suppress a few email alerts that are set up via powershell scripting.  I am attempting to suppress the emails from 21:00 through 03:00.  During that time frame I do NOT want emails to be generated to anyone.  Here is the portion of my script that I have been using to try and achieve this:

    # Using "IF" logic lines 29-32 suppresses alerts between
    $SuppressStart = "21"
    $SuppressEnd = "3"
    $now = (get-date).hour
    if ($now -gt $SuppressStart -lt $SuppressEnd){

    I have tried a combination of -le and -ge.  At best I have only been able to suppress from 21:59 through 00:59.  It seems to work fine if Im within the same 24 hour day but I suspect the issue lies with the suppression occurring over 2 calendar days.  Please help!!

    Thursday, June 18, 2015 4:02 PM

Answers

  • You can find out yourself by testing using appropriate hour ranges in a separate sample script.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Friday, August 14, 2015 8:00 PM
    Wednesday, July 8, 2015 3:40 PM

All replies

  • First of all, there's no need to set your "suppress" variables using strings. PowerShell can use numbers. You can remove the quotes.

    Second, it looks like this:


    if ($now -gt $SuppressStart -lt $SuppressEnd) {
    

    Should be this:


    if ( ($now.Hour -ge 21) -or ($now.Hour -le 3) ) {
    


    -- Bill Stewart [Bill_Stewart]


    Thursday, June 18, 2015 4:12 PM
  • Sorry, but I don't have the resources to reproduce your scenario and debug your code for you.

    Start small and write a minimal script that contains only the absolute minimum amount of code to reproduce the problem. In this way you will be able to identify the problem on your own.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 8, 2015 2:20 PM
  • Are there any other variations of the following that you know of:

    $SuppressStart = 21
    $SuppressEnd = 3
    $now = (get-date).hour
    if ( ($now.Hour -ge 21) -or ($now.Hour -le 3) ) {

    This is the only part that does not work correctly.  Do you think -and will get me there instead of -or

    Thanks

    Wednesday, July 8, 2015 3:30 PM
  • You can find out yourself by testing using appropriate hour ranges in a separate sample script.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Friday, August 14, 2015 8:00 PM
    Wednesday, July 8, 2015 3:40 PM