Public/New-ScheduledTaskSystem.ps1

function New-ScheduledTaskSystem
{
    <#
        .DESCRIPTION
            Create a Windows task scheduler job to run as local system

        .EXAMPLE
            $New-ScheduledTaskSystem -TaskName "Example Task" -TaskDescription = "Example Description" -ScriptPath "$ENV:AllUsersProfile\MyScript.ps1"

        .NOTES
            Created by: Jon Anderson
            Modified: 2023-07-10
    #>

    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$TaskName,
        [parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$TaskDescription,
        [parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$ScriptPath
    )

    Write-LogEntry -Value "Creating a new Windows Task Scheduler job with the following parameters`n$($TaskName)`n$($TaskDescription)`n$($ScriptPath)" -Severity 1
    $Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-ExecutionPolicy Bypass -File `"$ScriptPath`""
    $Trigger =  New-ScheduledTaskTrigger -AtLogon
    $Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 00:05:00
    Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -User "System" -Settings $Settings -Description $TaskDescription
}