Get-AllScheduledScripts.ps1


Description

Purpose

Retrieves scheduled scripts from the local machine.

Detailed Description

The Get-AllScheduledScripts function retrieves all scheduled tasks on the local machine that execute PowerShell scripts.

Back to Top

Usage

Example 1

Get-AllScheduledScripts

Retrieves all scheduled scripts on the local machine.

Back to Top

Notes

Author: Luke Leigh Date: [Current Date] Version: 1.0

Back to Top


Script

<#
.SYNOPSIS
    Retrieves scheduled scripts from the local machine.

.DESCRIPTION
    The Get-AllScheduledScripts function retrieves all scheduled tasks on the local machine that execute PowerShell scripts.

.PARAMETER None
    This function does not accept any parameters.

.EXAMPLE
    Get-AllScheduledScripts

    Retrieves all scheduled scripts on the local machine.

.OUTPUTS
    System.Management.Automation.TaskScheduler.ScheduledTask[]

    This function returns an array of ScheduledTask objects representing the scheduled scripts.

.NOTES
    Author: Luke Leigh
    Date: [Current Date]
    Version: 1.0

.LINK
    [Link to any related documentation or resources]

#>
function Get-AllScheduledScripts {
    Get-ScheduledTask | Where-Object { $_.Actions.Execute -eq 'powershell.exe' } | ForEach-Object {
        $task = $_
        $taskInfo = Get-ScheduledTaskInfo -TaskName $task.TaskName -TaskPath $task.TaskPath

        $lastTaskResultDescription = switch ($taskInfo.LastTaskResult) {
            0 { "The operation completed successfully." }
            2147750687 { "The task is already running." }
            2147750686 { "The task will be triggered by user logon." }
            2147942402 { "The system cannot find the file specified." }
            2147942405 { "Access is denied." }
            default { "Unknown error." }
        }

        if ($taskInfo.LastTaskResult -eq 267011 -and $taskInfo.LastRunTime -eq "30/11/1999 00:00:00") {
            $lastTaskResultDescription = "New Task - Task Schedule not yet started."
        }
        elseif ($taskInfo.LastTaskResult -eq 267011) {
            $lastTaskResultDescription = "The task is currently running."
        }

        $output = New-Object PSObject
        $output | Add-Member -MemberType NoteProperty -Name "TaskName" -Value $task.TaskName
        $output | Add-Member -MemberType NoteProperty -Name "TaskPath" -Value $task.TaskPath
        $output | Add-Member -MemberType NoteProperty -Name "State" -Value $task.State
        $output | Add-Member -MemberType NoteProperty -Name "LastRunTime" -Value $taskInfo.LastRunTime
        $output | Add-Member -MemberType NoteProperty -Name "NextRunTime" -Value $taskInfo.NextRunTime
        $output | Add-Member -MemberType NoteProperty -Name "LastTaskResult" -Value $lastTaskResultDescription
        $output | Add-Member -MemberType NoteProperty -Name "NumberOfMissedRuns" -Value $taskInfo.NumberOfMissedRuns

        $output
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top