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 (289k 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 by aweight (60 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 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 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 by johanpr (120 points)
0 votes
1 answer

So I have custom forms for onboarding / offboarding users. We sometimes know 2-3 weeks in advance so I would like to add the ability to schedule these for the future. I ... to take all the data that was inputed and then process the request on the given date?

asked Aug 4, 2023 by thatcher (120 points)
3,547 questions
3,238 answers
8,232 comments
547,809 users