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

by (306k points)
Best answer
0 votes

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%.

by (320 points)
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 
by (306k points)
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

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 (80 points)
0 votes
1 answer

For any given AD attribute, I'd like to be able to retrieve the defined friendly name for it. Example: l = City, st = State, etc. Is there a way to do that in PowerShell? Thanks!

asked Aug 8 by jaymallery (60 points)
0 votes
1 answer

Hi, I have a lot of custom PowerShell scripts running and I am wondering, if its possible somehow to get the name of the PS Script the line of the execution of a code ... occurs and send directly the action, maybe path in Adaxes and line number of the script.

asked Oct 7, 2024 by wintec01 (2.3k points)
0 votes
1 answer

Is there a way to get the name of the user who approved a request and supply that to a step inside of a custom command? For example, HR submits a status change for an employee. ... and pass it as a param in a custom command that is called in one of the steps?

asked May 12, 2021 by davfount90 (20 points)
0 votes
1 answer