Task Scheduler Runner with email(s)
Description soon
# This script run and logs others, used as a launcher
function log_entry ($logwhat,$logstr){
[string]$thisstr = "Now:" + ( Get-Date ) + " : Running script job " + $logwhat + " : " + $logstr
$thisstr | out-file $mylogfile -append -noclobber
} #End log entry
function do_mail ([string]$mytext) {
$mymail = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient("mailrelay.domain.com") #Dns name or IP
$attach = new-object Net.Mail.Attachment($mylogfile)
$mymail.From = "Scriptlogger@domain.com"
$mymail.To.Add("Administration@domain.com,Scriptlogger@domain.com")
$mymail.Subject = ("SCRIPTSVR001: " + $runthis + " " + $mytext)
$mymail.Body = $mytext
$mymail.Attachments.Add($attach)
$smtp.Send($mymail)
} #do_mail
$runthis = $args[0]
$scriptspath = "c:\Scripts"
$scriptslog = "c:\logs\scripts"
[string]$mylogfile = $scriptslog + "\" + $runthis + ".log"
try {
log_entry ($runthis," Begining script execution.")
powershell ($scriptspath + "\" + $runthis) 2>&1 >> ($mylogfile)
if ($LASTEXITCODE -eq 0) {
log_entry ($runthis," Script execution completed successfully.")
do_mail ("Script execution completed successfully.")
} #if
else {
log_entry ($runthis," Script execution has failed.")
do_mail ("Script execution has failed.")
exit 3
} #else
}#try
catch {
log_entry ($runthis,(" Script execution has failed to begin error:" + $_ ))
do_mail ((" Script execution has failed to begin error:" + $_ ))
exit 4
}#catch
#exit success
exit 0