Private/New-pseScheduledTaskSettingsSet.ps1

Function New-pseScheduledTaskSettingsSet {

    [cmdletbinding()]

    param (
        [switch]$TurnOffExecutionTimeLimit
    )

    try {
        Write-Host "Assigning Task Settings Set" -ForegroundColor DarkGray

        $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-Host "Task Settings Set assigned successfully" -ForegroundColor DarkGreen

        return $taskSettings
    } catch {
        Write-Error "An error occured assigning Task Settings Set: $x" -stop
    }
}