Reputation: 859
I got a couple of scripts which perform mailboxes moves to the cloud on office 365. The scripts sometimes fails and others not, I don't have any clue on why it fails but I guess it is because the service is not available when the task is executed, anyways, the point is that these scripts are running as scheduled tasks and one must be executed before of other, how I could detect if the first tasks failed and if so then reschedule the second task.
The tasks are running powershell v2 cmdlets on windows 2008 r2 so I'm using Task Scheduler.
I wonder if I should save some entries into a text file, to use a different task scheduler or maybe run each job using powershell, in the following way:
foreach ($script in $scripts)
{
#check status in a text file
$job = start-job -Filepath c:\myscript.ps1 -AsJob
Wait-Job $job -Timeout 180
}
What would be the best approach?
Thanks,
Upvotes: 0
Views: 1375
Reputation: 26160
You can use schtasks.exe to query / enable or reschedule your tasks. Or you can use the COM object of the scheduler (beware schtasks output is language dependant). With one or other method check the lastTaskResult and reschedule or not your other task.
Examples:
Get all tasks of the root folder using COM object
icm -AsJob -JobName getTasks -ComputerName $computername -ScriptBlock{
$Schedule = new-object -com("Schedule.Service")
$Schedule.connect($env:computername)
$Tasks = $Schedule.getfolder("\").gettasks(0)
$Tasks | Select-Object Name,Path,State,Enabled,LastRunTime,LastTaskResult
}
Get a particular task with schtasks
schtasks /query /TN "\Microsoft\Windows\Autochk\Proxy" /v /fo csv |ConvertFrom-Csv
Upvotes: 0