TheGeekery

The Usual Tech Ramblings

PowerShell; Checking a Job Status

With Windows 2008, the task scheduler is setup differently than earlier. You can use the Com object Schedule.Service to access information about tasks.

First we need to get the Com object to talk to the server. This is easily done with a “new object”.

1
2
$st = new-object -com("Schedule.Service")
$st.connect()

The first line gets you the com object, the second connects to the server. You can put in a server address to query remote servers too.

As 2008 uses task folders, you have to get the folder to search inside. If you don’t organize, this can be “\” or you can use the folder name.

1
$folder = $st.getFolder("\")

Then get the tasks from that folder.

1
$tasks = $folder.GetTasks(0)

$tasks is now an array of tasks, which you can walk through, or query. Now we have a task listing, we can figure out the state of each task using a foreach.

1
$tasks | select Name,State

The state column is based on the values here. So we know that 4 is running for example.

I used this information to build a script that allowed my Nagios server to manage alerts based on our build server running the build process.

Comments