Schtask script to email status of a job

Welcome Forums General PowerShell Q&A Schtask script to email status of a job

Viewing 8 reply threads
  • Author
    Posts
    • #206220
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      Need some help

      I’m running a command in PS to query a task

      schtasks /query /fo LIST /tn ‘\Receive Orders’

      Get a result

      Folder: \

      HostName: My-Server-VM

      TaskName: \Receive Orders

      Next Run Time: 2/25/2020 2:40:00 PM

      Status: Ready

      Logon Mode: Interactive/Background

      What I would like to get from the query is this

      Look at the status if the status is “Ready” do nothing if the Status is “Running” send me an email.

      I don’t know how to achieve this.

      I also tried Get-ScheduledTask but I just don’t know how to get this to email me only when the task is runnig.

    • #206229
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      You may start with learning the very basics of Powershell first. The cmdlet to send a mail message is Send-MailMessage. Please read the complete help including the examples to learn how to use it.

      BTW: Did you follow the tip TheMadTechnician gave you in the comments on StackOverflow?

      https://stackoverflow.com/questions/60402821/schtask-script-to-email-status-of-a-job

    • #206247
      Participant
      Topics: 12
      Replies: 1607
      Points: 2,495
      Helping Hand
      Rank: Community Hero

      A better approach would be to edit what is being executed in \Receive Orders to send the email when it starts so it’s the same process. The next would be to make an email task that’s a dependency for \Receive Orders, something like this:

      http://woshub.com/schedule-task-to-start-when-another-task-finishes/

      It’s a scheduled task, so you could also just run the email at the same time on the same schedule. Checking the status of the running service is typically looping continually to check the state which would be running all of the time.

    • #206262
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      The comment that he left for me has nothing to do with the problem I am having. I don’t care about the time when the job is running or if its off by 30 seconds, I just want to get a notification when its running.

      ‘Receive Orders’ job runs for about 2 minutes. So for example job starts at 10:00 am should finish at 10:02. At 10:04 my “Check Receive Orders” kicks off and checks if the “Receive Orders” is still running. If it is send me an email if it doesn’t doesn’t send me an email.

      I guess what Im asking for is how do phrase that

      I just don’t know what to put in front of all of this to get the result.

       

      $emailFrom = “test@test.com”
      $emailTo = “test@test.com”
      $Subject = “Receive Orders Running”
      $Body = “Job Running.”
      $SmtpServer = “smtp.server.com”
      $smtp = new-object Net.Mail.SmtpClient($smtpServer)

      if = status is Running – This is incorrect I know

      {
      $smtp.Send($emailFrom, $emailTo, $subject, $body)
      }
      else
      {
      #Do Nothing
      }

       

    • #206274
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      As suggested you should put the code to send an email into the task. To send a mail message you could use the cmdlet Send-MailMessage. … maybe like this:

      $SendMailMessageProps = @{
          From       = 'test@test.com'
          To         = 'test@test.com'
          SMTPServer = 'smtp.server.com'
          Subject    = 'Receive Orders Running'
          Body       = 'Job Running.'
      }
      
      Send-MailMessage @SendMailMessageProps
      • This reply was modified 2 months, 4 weeks ago by Olaf.
    • #206283
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      This send an email but it sends it when the job is in ready or running state.

      To clarify I am checking on an existing Task Scheduler job with a new Task Scheduler job. Two different Tasks.

      PS C:\Windows\system32> schtasks /query /fo LIST /tn ‘\Receive Orders’
      $SendMailMessageProps = @{
      From = ‘test@test.com’
      To = ‘test@test.com’
      SMTPServer = ‘smtp.test.com’
      Subject = ‘Receive Orders Running’
      Body = ‘Job Running.’
      }

      Send-MailMessage @SendMailMessageProps

      Folder: \
      HostName: test-VM
      TaskName: \Receive Orders
      Next Run Time: 2/26/2020 9:40:00 AM
      Status: Ready – It should look at this field and only email when Running
      Logon Mode: Interactive/Background

    • #206289
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      OK. So you’re not able to change the action of the task “Receive Orders”, right?

      Does this help?

      (Get-ScheduledTask -TaskPath \ -TaskName 'Receive Orders').State
    • #206292
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      Correct “Receive Orders” needs to stay as it is cannot change the action of the task.

      With the command you get

      Get-ScheduledTask : A parameter cannot be found that matches parameter name ‘TaskPath’.
      At line:1 char:20
      + (Get-ScheduledTask -TaskPath \ -TaskName ‘Receive Orders’).State
      + ~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [Get-ScheduledTask], ParameterBindingException
      + FullyQualifiedErrorId : NamedParameterNotFound,Get-ScheduledTask

      I also tried this

      Get-ScheduledJob | ? State -eg running
      Get-ScheduledJob -TaskPath \Receive Orders

      and got same an error.

      What Im missing is when I run

      Get-ScheduledTask -name ‘Receive Orders’

      I get the output I need, once that output is displayed there should be something to say look at the status and if it =Running send an email

      I don’t have that argument anywhere in the script.

       

    • #206328
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Please – format your code as code here in the forum using the code tag button named “PRE“. Thanks. And please post the complete output you get with the command

      Get-ScheduledTask -name 'Receive Orders'

      If you get the output for the desired scheduled task you can “extract” the status with this piece of code:

      (Get-ScheduledTask -name 'Receive Orders').state

      You can use this to create a condition to check for the state and send an email if the state is what you expect.
      If you don’t know how to create a condition I repeat my suggestion from my first reply. 😉 Additionally you should read the help for about_if.

Viewing 8 reply threads
  • You must be logged in to reply to this topic.