Check scheduled tasks on localhost or remote servers

In this article we will focus on how to get information about scheduled tasks from localhost or remote machines.

Function is based on command Get-ScheduledTask which is available starting from Windows Server 2012 in module ScheduledTasks:

Get-ScheduledTask -CimSession DC01 | Where-Object {$_.state -match "Running"}

Basically we can get information about tasks using above 1 liner but just for fun we can create this function.

It will allow us to specify state of tasks which we would like to search and display results in host. For $State parameter we can select “Ready”, “Disabled” or “Running”.

Usage:


Get-ScheduledTasks
Get-ScheduledTasks -State Disabled
Get-ScheduledTasks -Servers DC01
Get-ScheduledTasks -Servers DC01,DC02
Get-ScheduledTasks -Servers (Get-Content "c:/temp/input.txt")
Get-ScheduledTasks -Servers DC01,DC02 -State Running

First we have to check if “Scheduled Tasks” module is installed and then on the beginning of “ForEach” loop script will test if there is network connectivity to the server using Test-Connection command. If test passed it will continue gathering tasks information.

Final script:


function Get-ScheduledTasks {
    [CmdletBinding()]        
  
    # Parameters used in this function
    param
    (
        [Parameter(Position=0, Mandatory = $false, HelpMessage="Provide server names", ValueFromPipeline = $true)] 
        $Servers = $env:computername,
  
        [Parameter(Position=1, Mandatory = $false, HelpMessage="Select task state (Ready, Disabled, Running)", ValueFromPipeline = $true)][ValidateSet("Ready", "Disabled", "Running")][string]
        $State = $null
    ) 
 
    # Error action set to Stop
    $ErrorActionPreference = "Stop"
 
    # Checking module
    Try
    {
        Import-Module ScheduledTasks
    }
    Catch
    {
        $_.Exception.Message
        Write-Warning "Scheduled Tasks module not installed"
        Break
    }
         
        # Looping each server
        ForEach($Server in $Servers)
        {
            Write-Host "Processing $Server" -ForegroundColor Yellow 
     
            # Testing connection
            If(!(Test-Connection -Cn $Server -BufferSize 16 -Count 1 -ea 0 -Quiet))
            {
                Write-Warning   "Failed to connect to $Server"
            }
            Else
            {
                $TasksArray = @()
 
                Try
                {
                    $Tasks = Get-ScheduledTask -CimSession $Server | Where-Object {$_.state -match "$State"}
                }
                Catch
                {
                    $_.Exception.Message
                    Continue
                }

                If($Tasks)
                {
                    # Loop through the servers
                    $Tasks | ForEach-Object {
  
                        # Define current loop to variable
                        $Task = $_
 
                        # Creating a custom object 
                        $Object = New-Object PSObject -Property @{ 
   
                            State             = $Task.State           
                            "Task Name"       = $Task.TaskName                
                            Author            = $Task.Author             
             
                        }  
 
                        # Add custom object to our array
                        $TasksArray += $Object
                    }
 
                    # Display results in console
                    $TasksArray | ft -AutoSize -Wrap
 
                    # Display results in pop-up window
                    $TasksArray | Out-GridView -Title "Tasks on $Server"
                }
                Else
                {
                    Write-Warning "Tasks not found"
                }
            }
        }   
}

Here you can find list of cmdlets available in “ScheduledTasks” module – link.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.