Windows Server 2012 / Windows 8 and above include some nifty Powershell cmdlets for working with scheduled tasks. However, when the need arises to programmatically work with them on older OS versions, mainly Windows Server 2008 , try out the code below.
1 2 3 4 5 6 7 8 |
# Create a Com Object for our Task Scheduler on local host ($TaskScheduler = New-Object -ComObject Schedule.Service).Connect("localhost") # Retrieve the folder our task is in (in this case, root) $TaskFolder = $TaskScheduler.GetFolder('\') # Now get our task $Task = $TaskFolder.GetTask('Delete Old Files') |
Now that we have our $Task object, piping it into Get-Member we can see we can do quite a bit with it. Including enable / disable the task, get it’s status, last run result, and more.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TypeName: System.__ComObject#{9c86f320-dee3-4dd1-b972-a303f26b061e} Name MemberType Definition ---- ---------- ---------- GetInstances Method IRunningTaskCollection GetInstances (int) GetSecurityDescriptor Method string GetSecurityDescriptor (int) Run Method IRunningTask Run (Variant) RunEx Method IRunningTask RunEx (Variant, int, int, string) SetSecurityDescriptor Method void SetSecurityDescriptor (string, int) Stop Method void Stop (int) Definition Property ITaskDefinition Definition () {get} Enabled Property bool Enabled () {get} {set} LastRunTime Property Date LastRunTime () {get} LastTaskResult Property int LastTaskResult () {get} Name Property string Name () {get} NextRunTime Property Date NextRunTime () {get} NumberOfMissedRuns Property int NumberOfMissedRuns () {get} Path Property string Path () {get} State Property _TASK_STATE State () {get} Xml Property string Xml () {get} |
I admit it’s not as great as the cmdlets in the newer versions of Windows, but it has saved me quite a bit of trouble in more than few scripts. Hope this helps, and happy scripting!
Thank you!
Do you perhaps know how to invoke the Run and Stop methods? What arguments do they need?
Absolutely!
With the above code, you simply add a line to call a method of the object held in the $Task variable.
$Task.Run($null)
We pass in $null because we don’t need to pass any parameters into this method to simply run it.
To stop the task add the following:
$Task.Stop(0)
The parameter passed into the Stop method must always be zero.
More info on the methods used found here:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa382094(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa380771(v=vs.85).aspx
Happy Scripting!
Thanks Ryan, that worked a treat. Much appreciated. I had a requirement for team members to run some server tasks ad-hoc after hours and wanted to avoid the task manager GUI. They are a good bunch but it’s just too risky, even with the best will in the world. Thanks to you I now have a couple of simple CMD scripts.
This is amazing! Thank you very much for posting this! I would love to run a scheduled task on several servers without having to change the server name each time in the script (task name is the same across all servers). Does something like a variable, foreach and Get-content from a csv/txt file work with this script?
Yep should work using either get-content, import-csv, or input from another cmdlet (Like Get-ADComputer) and put that into a foreach loop.
Glad it helped and good luck!