0 votes

Hello, I've created a custom command to run a script which will send an email alert if the script encounters an error. I have the command set to run as a scheduled task.

How would I dynamically display the name of the custom command and scheduled task in the email alert?

This is an example of what I'd want to show in the email alert from the Powershell script that sends it:

Custom Command Name: Task Name: Time: Error Message:

Thank you.

by (320 points)

1 Answer

0 votes
by (295k points)
selected by
Best answer

Hello,

Unfortunately, there is no possibility to obtain the custom command name in the script. It can only be set as plain text. As for the scheduled task, you can use value reference %initiator%.

0

Support, thank you for the response. Using the %initiator% value reference will work for me. For anyone else wanting to do something similar this is how I approached it using a try/catch block:

$Now = "%datetime:format[MM/dd/yyyy hh:mm tt]%"
$TaskName = "%initiator%"
$Context.LogMessage($_.Exception.Message, "Warning")
$SMTPServer = "MODIFY ME"
$From = "MODIFY ME"
$To = "MODIFY ME"
$Subject = "Adaxes - Error Occurred When Running Scheduled Task"
$Body = "
Date/time: $Now <br>
Task: $TaskName <br>
Error: ($_.Exception.Message)"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer 
0

Hello,

Thank you for the confirmation. We do not see any try/catch blocks in your script, but it should work just fine. Also, you can use a built-in method $Context.SendMail for email delivery. In this case, the mail settings of Adaxes service will be used and there will be no need to specify the SMTP server.

Related questions

0 votes
1 answer

Using the powershell module, I know how to create a scheduled task, and also how to bind to a scheduled task that is already known. I also have used code to try creating ... same time as another. These are all one-time tasks and will be removed once executed.

asked Jan 19, 2024 by aweight (60 points)
0 votes
1 answer

How do we set up an e-mail notification (only once per user) as an scheduled task (ex. every hour) based on the property "accountExpires"? Some background: Our HR-software ... to achive this result, do you have any suggestions on how to solve it another way?

asked 5 days ago by Handernye (120 points)
0 votes
1 answer

If I have a scheduled task powershell script that's targeting an OU of users and in that script I were to call $context.cancel in the case of an error happening for a single ... it cancel the entire scheduled tasks and it won't run for other users in that OU?

asked Oct 18, 2024 by wrichardson (20 points)
0 votes
1 answer

Similar to Powershell's "whatif"? I'd like to enable this scheduled task - But would like to confirm who will actually be affected before enabling this. Is there at least ... objects in the console log? I could run that before adding the 'modify' actions back.

asked Jun 25, 2024 by msinger (210 points)
0 votes
1 answer

I want to create a scheduled task to disable a user if he is inactive for 30days, the task must check inacivity o AD and Azure.

asked May 16, 2024 by johanpr (120 points)
3,605 questions
3,292 answers
8,342 comments
548,448 users