Stop script if it ran within last hour

Welcome Forums General PowerShell Q&A Stop script if it ran within last hour

Viewing 3 reply threads
  • Author
    Posts
    • #159111
      Participant
      Topics: 9
      Replies: 11
      Points: 82
      Rank: Member

      Thanks to the powershell.org community I got a script working that pings a list of servers every 10 minutes via Task Scheduler. If a server(s) go down I get an email and text message.

      https://powershell.org/forums/topic/how-to-use-encrypted-password-in-task-scheduler/#post-159053

       

      I would like to get the initial email/text, but if we know it’s down I don’t want to get notified every 10 minutes. I’d like to get the initial notification then if it’s still down after an hour get another notification. How can I block the notifications in that 50 minute time frame every hour?

    • #159120
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero

      You could update the results of each trigger in a CSV with the timestamp and for the servers not reachable, check the timestamp before sending the mail and update the timestamp.

    • #167413
      Participant
      Topics: 9
      Replies: 11
      Points: 82
      Rank: Member

      Thanks for the suggestion. I’m having an issue writing the date/time to the CSV file. I thought I could use this format for date/time, but when I look in the CSV it just displays the number of characters, not the actual date and time. How can I compare the current time with the last entry in the CSV?

      get-date -Uformat “%Y%m%d%H%M%S” | Export-Csv -Path .\serverstatus.csv -NoTypeInformation -NoClobber -append -force
      • #167482
        Participant
        Topics: 1
        Replies: 64
        Points: 363
        Helping Hand
        Rank: Contributor

        export-csv is an ‘odd duck’.  If you really want to export to a csv file, you can just do out-file.

         

        get-date -Uformat "%Y%m%d%H%M%S" | out-file .\serverstatus3.csv -append -force
    • #167506
      Participant
      Topics: 9
      Replies: 11
      Points: 82
      Rank: Member

      Thanks. I ended up using

      Test-Path -Path $LogFile -NewerThan $NewDate.AddMinutes(-55) to test the age of the CSV file.
Viewing 3 reply threads
  • The topic ‘Stop script if it ran within last hour’ is closed to new replies.