Keith
Keith

Reputation: 2019

Get-ScheduledTask in PowerShell on Windows Server 2003

I have the below PowerShell code to validate if a scheduled task exists and to check its state.

$SchTsk = Get-ScheduledTask | Select Name, State | ? {$_.Name -eq $SchTask}
If ($SchTsk -ne $Null)
{
  Write-Host "SchTask $SchTask exists"
  If ($SchTsk.State -eq 3)
  {
    Write-Host "SchTask State: READY!"
  }
}

The code works fine on Windows Server 2008 but does not work on Windows Server 2003. In 2003 I get an error:

New-Object : Cannot load COM type Schedule.Service.

From what I've read it seems that the Schedule.Service COM object does not exist on Server 2003.

So...is there a work-around for this issue to validate a scheduled task and its state on Server 2003?

Upvotes: 3

Views: 25527

Answers (5)

woful
woful

Reputation: 11

If all you want to do is gather the basic properties of a task so you know it's name state and next run time you can use schtasks with the following methods:

function New-TaskInfo()
{
    param ($TaskPath, $TaskName, $NextRunTime, $Status);

    $task = new-object PSObject;

    $task | add-member -type NoteProperty -Name Path-Value $TaskPath;
    $task | add-member -type NoteProperty -Name Name -Value $TaskName;
    $task | add-member -type NoteProperty -Name NextRunTime -Value $NextRunTime;
    $task | add-member -type NoteProperty -Name Status -Value $Status;

    return $task;
}


function Get-ScheduledTaskInfo
{
    $tasks = @();
    $queryOutput = schtasks /QUERY /FO CSV
    foreach($line in $queryOutput) 
    {
        $columns = $line.Split(','); 
        $taskPath = $columns[0].Replace('"', '');
        if($taskPath -eq "TaskName")
        {
            #Ignore headder lines
            continue;
        }
        $taskName = [System.IO.Path]::GetFileName($taskPath);
        $nextRunTime = $columns[1].Replace('"', '');
        $status = $columns[2].Replace('"', '');
        $task = New-TaskInfo -TaskPath $taskPath -TaskName $taskName -NextRunTime $nextRunTime -Status $status;
        Write-Host -ForegroundColor Green "Add Task $task";
        $tasks += $task;
    }
    return $tasks;
}

If you then want to perform an action for a specific task you can use schtasks directly specifying data stored in the objects collected earlier.

$task = Get-ScheduledTaskInfo | Where-Object {$_.Name -eq 'TaskName'}
if($task.Status -eq 'Ready')
{
    Write-Host -ForegroundColor Green "Task" $task.Name "Is" $task.Status;

    #End the target task
    schtasks /END /TN $task.Path;
}

Upvotes: 1

Sitaram Pamarthi
Sitaram Pamarthi

Reputation: 459

@ameer deen.. The code you have given returns the tasks at the root level only. Windows 2008 onwards several modifications are made to task manager and one of them is folder structure. To list all the tasks in side sub folders as well, we need to query the subfolders and iterate through them.

You can find the code sample for querying all(including the ones in subfolders) scheduled tasks at http://techibee.com/powershell/powershell-get-scheduled-tasks-list-from-windows-72008-computers/1647

Upvotes: 0

slidmac07
slidmac07

Reputation: 387

I have PowerShell scripts running on Win2008 and Win2003, and found the command "schtasks" to be good enough for looking up information about scheduled tasks. This isn't a powershell command, but it's a standard function in Windows, and is compatible with Windows 2003 and 2008.

$scheduledTasks = schtasks /query /v /fo csv | ConvertFrom-Csv
#this will return an array of scheduled tasks with all info available for the task

To check if a scheduled task is ready on 2003, you'll need to make sure "Scheduled Task State" is "Enabled", and Status is blank.

On 2008 and above, Status will return enabled, disabled, running, etc.

Upvotes: 2

Ameer Deen
Ameer Deen

Reputation: 734

The following is a sample PowerShell script that reads from the COM object mentioned above and outputs some Task Schedule Information:

#Connecting to COM Object 
$schedService = New-Object -ComObject Schedule.Service 
$schedService.Connect($Computer) 

# Get Scheduled Tasks on Root Folder (Task Scheduler Library)
$folder = $SchedService.GetFolder("") 
$tasks = $folder.GetTasks("") 

# Output Task Details
$tasks | % { 
  "-" * 40
  "Task " + $_.Name + ":"
  "-" * 40
  $_.Definition.Actions
}

Upvotes: 2

JPBlanc
JPBlanc

Reputation: 72620

You'll find all the informations you need about the missing COM object in Working with scheduled tasks from Windows PowerShell.

Using Windows Server 2012 you can use Scheduled Tasks Cmdlets in Windows PowerShell.

Upvotes: 0

Related Questions