Prof. Powershell

Managing Scheduled Tasks, Part 3

Let's take one more look at the fun involved in managing tasks on an enterprise level.

Let's wrap up our look at managing scheduled tasks in PowerShell and Windows 8 (or Windows Server 2012) by managing the task we created last time:

PS C:\> get-scheduledtask MSInfoReport

There's more to this than meets the eye. Figure 1 shows the complete object.

The first thing I want to do is modify the task a bit and add some new information. The easiest approach I think is to get the task, modify it and then reset it.

PS C:\> $task=get-scheduledtask MSInfoReport
PS C:\> $task.Description="My sample scheduled task"
PS C:\> $task.Author="$env:userdomain\$env:username"

Scheduled Task object

Figure 1. A Scheduled Task object. (Click image to view larger version.)

I also want to modify some of the current settings:

PS C:\> $task.settings

AllowDemandStart                : True
AllowHardTerminate              : True
Compatibility                   : Vista
DeleteExpiredTaskAfter          :
DisallowStartIfOnBatteries      : True
Enabled                         : True
ExecutionTimeLimit              : PT72H
Hidden                          : False
IdleSettings                    : MSFT_TaskIdleSettings
MultipleInstances               : IgnoreNew
NetworkSettings                 : MSFT_TaskNetworkSettings
Priority                        : 7
RestartCount                    : 0
RestartInterval                 :
RunOnlyIfIdle                   : False
RunOnlyIfNetworkAvailable       : False
StartWhenAvailable              : False
StopIfGoingOnBatteries          : True
WakeToRun                       : False
DisallowStartOnRemoteAppSession : False
UseUnifiedSchedulingEngine      : False
MaintenanceSettings             :
volatile                        : False
PSComputerName                  :

There are cmdlets I could use to modify this object, but it is just as easy to set new values:

PS C:\> $task.settings.Compatibility="Win8"
PS C:\> $task.settings.RunOnlyIfNetworkAvailable=$True

All that is left at this point is to apply the changes by setting the task.

PS C:\> $task | Set-ScheduledTask -User "win8cp\administrator" -Password "P@ssw0rd"

When setting the task, you have to include the username and password. After refreshing the Task Scheduler management console, I can see my changes (see Fig. 2).

Revised Task

Figure 2. The Revised Task. (Click image to view larger version.)

Now, suppose I don't want to wait and want to run it now? Easy as this:

PS C:\> Start-ScheduledTask MSInfoReport

I can then check the results using the Get-ScheduledTaskInfo cmdlet which returns an Info object:

PS C:\> Get-ScheduledTaskInfo MSInfoReport

LastRunTime        : 5/23/2012 10:53:53 AM
LastTaskResult     : 0
NextRunTime        : 5/23/2012 12:00:00 PM
NumberOfMissedRuns : 0
TaskName           : MSInfoReport
TaskPath           :
PSComputerName     :

With this kind of information I can easily create a report showing what my tasks have been doing:

Get-ScheduledTask | Where {$_.state -eq "Ready"} | Select TaskName,
@{Name="LastRun";Expression={(Get-ScheduledTaskInfo -Input $_).LastRunTime}},
@{Name="Result";Expression={(Get-ScheduledTaskInfo -Input $_).LastTaskResult}} |
Sort Result,LastRun

Figure 3 displays my results.

Scheduled Tasks Results Report

Figure 3. Scheduled Tasks Results Report. (Click image to view larger version.)

A result of 0 should indicate success. Anything else I would need to investigate. There are many reporting ideas of I have but let's get back to the task at hand, so to speak.

Let's say I want to disable the scheduled task. I can get the task's settings object as I did earlier, modify it and reset. Or I can do it all in a one-line expression:

PS C:\> Get-ScheduledTask MSInfoReport | foreach {$_.Settings.Enabled=$False; $_} |
>> Set-ScheduledTask -user "win8cp\administrator" -Password "P@ssw0rd"
>>

TaskPath             TaskName           State
--------             --------           -----
\                    MSInfoReport       Disabled

Finally, I want to delete the task. This is merely a matter of unregistering it:

PS C:\> Unregister-ScheduledTask MSInfoReport

Confirm
Are you sure you want to perform this action?
Performing operation '\MSInfoReport' on Target 'Delete'.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
The cmdlet supports -WhatIf.

As we move further into the Windows 8 and Windows Server 2012 world, I think you'll find managing scheduled tasks in the enterprise will be much easier and maybe even a little fun.

Reader Comments:

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above