This might be an easy one but I tried hard to find "how to" and didn't came across any answers. So, my question is, "How do I find the Scheduled Task Name which activated my Powershell script"
So, I have a Scheduled Task configured on my Windows 2012 R2 Server which run's a program XYZ and also executes a Powershell script to send an email notification with the name of Task hard-coded inside the script.
$SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "smtp.mydomain.com" $mailmessage.from = ("email@example.com") $mailmessage.To.add("firstname.lastname@example.org") $mailmessage.Subject = “Task XYZ has started” $mailmessage.Body = “This is to notify that task XYZ has been started” $smtpclient.Send($mailmessage)
Now, I have 100 different tasks which needs this email notification and I have to use only a single PowerShell Script to trigger the email. This email should consist of the name of the Scheduled Task inorder to understand which Task has been started running for which we are receiving the email notification.
So, is there any inbuilt Powershell variable/function to find the name of Scheduled Task which executed my Powershell script?