Private/Get-SpecScheduledTask.ps1

Function Get-SpecScheduledTask {
    <#
    .SYNOPSIS
    This function checks if a scheduled task with a given name exists and provides corresponding exit codes.
 
    .DESCRIPTION
    The Get-SpecScheduledTask function checks if a scheduled task with the specified name exists. If the task exists, it returns an exit code of 900, and if the task doesn't exist, it returns an exit code of 901.
 
    .PARAMETER TaskName
    Specifies the name of the scheduled task to check for existence.
 
    .EXAMPLE
    $exitCode = Get-SpecScheduledTask -TaskName "MyTask"
    Returns: 900
    Checks if the scheduled task named "MyTask" exists. Since it exists, the function returns an exit code of 900.
 
    .EXAMPLE
    $exitCode = Get-SpecScheduledTask -TaskName "NonExistentTask"
    Returns: 901
    Checks if the scheduled task named "NonExistentTask" exists. Since it doesn't exist, the function returns an exit code of 901.
 
    .NOTES
    Author: owen.heaume
    Date: August 10, 2023
    Version: 1.0
    #>


    [cmdletbinding()]
    Param (
        [parameter (mandatory = $true)]
        [string]$TaskName
    )

    try {
        $Task = Get-ScheduledTask -TaskName $TaskName -ea stop -ev x
        Write-Verbose "The task '$TaskName' already exists and is in the '$($task.state)' state"
        return 900
    } catch {
        write-verbose "The task $TaskName does not exist."
        return 901
    }
}