Private/New-SpecScheduledTaskSettingsSet.ps1

Function New-SpecScheduledTaskSettingsSet {
    <#
    .SYNOPSIS
    This function creates a new scheduled task settings set with predefined values.
 
    .DESCRIPTION
    The New-SpecScheduledTaskSettingsSet function creates a new scheduled task settings set with predefined values for various task settings. These settings include compatibility, wake to run, battery-related settings, network availability, visibility, priority, and execution time limit.
 
    .EXAMPLE
    $taskSettingsSet = New-SpecScheduledTaskSettingsSet
    Creates a new scheduled task settings set with the predefined values.
 
    .NOTES
    Author: owen.heaume
    Date: August 10, 2023
    Version: 1.0 - Initial release
                1.1 - Added TurnOffExecutionTimeLimit parameter to turn off the execution time limit.
    Status Codes:
    - Successful assignment: Returns the created task settings set object.
    - An error occurred assigning task settings set: Returns 913.
    #>


    [cmdletbinding()]

    param (
        [switch]$TurnOffExecutionTimeLimit
    )

    try {
        write-verbose "Assigning Task Settings Set"

        $taskSettings = New-ScheduledTaskSettingsSet -ea Stop -ev x

        $taskSettings.Compatibility = 'Win8'
        $taskSettings.WakeToRun = $true
        $taskSettings.DisallowStartIfOnBatteries = $false
        $taskSettings.StopIfGoingOnBatteries = $false
        $taskSettings.RunOnlyIfNetworkAvailable = $true
        $taskSettings.Hidden = $false
        $taskSettings.Priority = 7


        if($TurnOffExecutionTimeLimit.IsPresent) {
            $MaxTime = "PT0S"
            $taskSettings.executiontimelimit = $MaxTime
        } else {
            # Set execution time limit to 30 minutes
            $eTime = New-TimeSpan -Minutes 30
            $MaxTime = "PT" + $eTime.ToString('hh') + "H" + $eTime.ToString('mm') + "M" + $eTime.ToString('ss') + "S"
            $taskSettings.executiontimelimit = $MaxTime
        }

        write-verbose "Task Settings Set assigned successfully"

        return $taskSettings
    } catch {
        write-warning "An error occured assigning Task Settings Set: $x"
        return 913
    }
}