From 9779a35c986b787f82cfd5bd7eb1c3ce7c40e482 Mon Sep 17 00:00:00 2001 From: DistractADD Date: Fri, 27 Jun 2025 02:29:57 +0000 Subject: [PATCH] Create New File --- Powershell/Universal_Task_Script.ps1 | 144 +++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Powershell/Universal_Task_Script.ps1 diff --git a/Powershell/Universal_Task_Script.ps1 b/Powershell/Universal_Task_Script.ps1 new file mode 100644 index 0000000..8ead5c8 --- /dev/null +++ b/Powershell/Universal_Task_Script.ps1 @@ -0,0 +1,144 @@ +<# +.SYNOPSIS + Create or list Scheduled Tasks on local or remote servers under SYSTEM account. + +.DESCRIPTION + Streamlines task creation and management across multiple servers: + - Always runs as SYSTEM + - Supports Once/Daily/Weekly/Monthly triggers + - Custom action command + arguments + - Custom settings (start when available, wake to run, restart on failure) + - Optionally list existing tasks + - Can target a remote server via CIM session + +.PARAMETER ServerName + (Optional) Remote server to connect to. If omitted, actions run locally. + +.PARAMETER ListTasks + Switch. If specified, lists current scheduled tasks and exits. + +.PARAMETER TaskName + Name of the scheduled task (required unless -ListTasks). + +.PARAMETER Description + Task description (appears in Task Scheduler UI). + +.PARAMETER Frequency + Trigger frequency: Once, Daily, Weekly, Monthly. + +.PARAMETER At + DateTime for the trigger (for Once) or time of day (for recurring triggers). + +.PARAMETER DaysOfWeek + When Frequency = Weekly, specify days (e.g. Monday,Wednesday). + +.PARAMETER Months + When Frequency = Monthly, specify months (e.g. January,June). + +.PARAMETER DaysOfMonth + When Frequency = Monthly, specify days of month (e.g. 1,15,28). + +.PARAMETER Command + Executable to run (default: powershell.exe). + +.PARAMETER Arguments + Command arguments (e.g. '-File C:\Scripts\MyScript.ps1'). + +.PARAMETER StartWhenAvailable + If set, task will start if missed. + +.PARAMETER WakeToRun + If set, will wake the machine to run. + +.PARAMETER RestartIntervalMinutes + On failure, how often (minutes) to retry. Zero = no retry. + +.PARAMETER RestartCount + How many retry attempts on failure. +#> +[CmdletBinding()] +Param( + [string]$ServerName, + [switch]$ListTasks, + [Parameter(Mandatory = $false, Position = 0)][string]$TaskName, + [string]$Description, + [ValidateSet('Once','Daily','Weekly','Monthly')][string]$Frequency, + [datetime]$At, + [string[]]$DaysOfWeek, + [string[]]$Months, + [int[]]$DaysOfMonth, + [string]$Command = 'powershell.exe', + [string]$Arguments = '', + [switch]$StartWhenAvailable, + [switch]$WakeToRun, + [int]$RestartIntervalMinutes = 0, + [int]$RestartCount = 0 +) + +# Create CIM session if remote +if ($ServerName) { + Write-Host "Creating CIM session to $ServerName..." + $CimSession = New-CimSession -ComputerName $ServerName +} + +# List tasks and exit +if ($ListTasks) { + Write-Host "Listing Scheduled Tasks on" ($ServerName ? $ServerName : 'local') + Get-ScheduledTask -CimSession $CimSession | + Select-Object TaskName, State, Actions | + Format-Table -AutoSize + return +} + +# Validate required parameters for task creation +if (-not $TaskName -or -not $Description -or -not $Frequency -or -not $At) { + Throw 'Parameters -TaskName, -Description, -Frequency, and -At are required when creating a task.' +} + +# Build trigger +switch ($Frequency) { + 'Once' { + $trigger = New-ScheduledTaskTrigger -Once -At $At + } + 'Daily' { + $trigger = New-ScheduledTaskTrigger -Daily -At $At.TimeOfDay + } + 'Weekly' { + if (-not $DaysOfWeek) { Throw 'Specify -DaysOfWeek for Weekly triggers.' } + $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $DaysOfWeek -At $At.TimeOfDay + } + 'Monthly' { + if (-not $Months -or -not $DaysOfMonth) { Throw 'Specify -Months and -DaysOfMonth for Monthly triggers.' } + $trigger = New-ScheduledTaskTrigger -Monthly -MonthsOfYear $Months -DaysOfMonth $DaysOfMonth -At $At.TimeOfDay + } +} + +# Build action +$action = New-ScheduledTaskAction -Execute $Command -Argument $Arguments + +# Build settings +$settings = New-ScheduledTaskSettingsSet ` + -AllowStartIfOnBatteries:$false ` + -DontStopOnIdleEnd ` + -StartWhenAvailable:$StartWhenAvailable.IsPresent ` + -WakeToRun:$WakeToRun.IsPresent + +if ($RestartIntervalMinutes -gt 0) { + $interval = New-TimeSpan -Minutes $RestartIntervalMinutes + $settings = $settings | Set-ScheduledTaskSettings -RestartOnFailureInterval $interval -RestartCount $RestartCount +} + +# Register task as SYSTEM +Write-Host "Registering task '$TaskName' on" ($ServerName ? $ServerName : 'local') +Register-ScheduledTask ` + -TaskName $TaskName ` + -Action $action ` + -Trigger $trigger ` + -Settings $settings ` + -Description $Description ` + -User 'NT AUTHORITY\SYSTEM' ` + -RunLevel Highest ` + -CimSession $CimSession ` + -Force + +Write-Host "Task '$TaskName' created successfully."