-3

A task job is running by task scheduler every 30 mins everyday.

I just want one more task to monitor whether the task is completed or not.

Would you please give me the powershell script as necessary below.

Sorted by Event id : 101 and 102, specified task job name, action time, the job result in the last 24 hours,

I want to have log txt file into C drive. I guess it should be used "Get-WinEvent"...

Otherwise if you know it's simple way to export the result log automatically(Task completed or not)

on daily base, please let me know. Thank you

sainale
  • 1
  • 1
  • 1
  • 1
    Have you done any research into what such a script should look like. We are all glad to help answer questions. But, this isn't a script writing service. – Todd Palmer Aug 09 '18 at 02:55
  • Adding to what Todd Palmer said. You have to define what this...'task is completed or not'., means. Finished successfully or failed. There are several examples of close to this sort of thing all over the web. See the resource answer to get you started, but like Todd Palmer states, you need to show us your work, and explain you issues with what you are trying to do. – postanote Aug 09 '18 at 07:08

1 Answers1

0

See these examples to inspire you to what you are after. Once you've tried some things, well, you know.

Powershell Script to Monitor Scheduled Tasks https://social.technet.microsoft.com/Forums/windows/en-US/dc607f76-02c1-489f-b519-340b0faf8fcd/powershell-script-to-monitor-scheduled-tasks

$servername = "tome-mac"
$schedule = new-object -com("Schedule.Service")
$schedule.connect($servername)
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks |select name, lasttaskresult, lastruntime

Make Windows Task Scheduler alert me on fail https://superuser.com/questions/249103/make-windows-task-scheduler-alert-me-on-fail

$ScheduledTaskName = "Hans\Backup"
$Result = (schtasks /query /FO LIST /V /TN $ScheduledTaskName  | findstr "Result")
$Result = $Result.substring(12)
$Code = $Result.trim()

If ($Code -gt 0) {
    $User = "mymail@gmail.com"
    $Pass = ConvertTo-SecureString -String "myPassword" -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential $User, $Pass
################################################################################

$From = "Alert Scheduled Task <mymail@gmail.com>"
$To = "Me Gmail <mymail@gmail.com>"
$Subject = "Scheduled task 'Backup' failed"
$Body = "Error code: $Code"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"

Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $Cred
}

Or just use these module(s) to determine if they can assist you in your use case.

https://www.powershellgallery.com/packages/TaskScheduler/1.0 https://gallery.technet.microsoft.com/scriptcenter/Schedule-Task-Monitor-a7c74403

postanote
  • 15,138
  • 2
  • 14
  • 25