Replaced the unsupported ternary logic with a proper $target variable
This commit is contained in:
@@ -1,144 +1,144 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Create or list Scheduled Tasks on local or remote servers under SYSTEM account.
|
Interactive TUI for listing and creating Scheduled Tasks on local or remote servers under SYSTEM account.
|
||||||
|
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Streamlines task creation and management across multiple servers:
|
- Menu-driven console interface
|
||||||
- Always runs as SYSTEM
|
- Always runs as SYSTEM (highest privilege)
|
||||||
- Supports Once/Daily/Weekly/Monthly triggers
|
- Supports ONCE/DAILY/WEEKLY/MONTHLY triggers
|
||||||
- Custom action command + arguments
|
- Custom actions, settings (start when available, wake to run, retry on failure)
|
||||||
- Custom settings (start when available, wake to run, restart on failure)
|
- Remote targeting via CIM session
|
||||||
- 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()]
|
[CmdletBinding()]
|
||||||
Param(
|
Param(
|
||||||
[string]$ServerName,
|
[switch]$RunTUI
|
||||||
[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
|
function New-UniversalScheduledTask {
|
||||||
if ($ServerName) {
|
[CmdletBinding()]
|
||||||
Write-Host "Creating CIM session to $ServerName..."
|
Param(
|
||||||
$CimSession = New-CimSession -ComputerName $ServerName
|
[string]$ServerName,
|
||||||
|
[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
|
||||||
|
)
|
||||||
|
|
||||||
|
# Determine target
|
||||||
|
$target = if ($ServerName) { $ServerName } else { 'local' }
|
||||||
|
if ($ServerName) {
|
||||||
|
Write-Host "Creating CIM session to $ServerName..."
|
||||||
|
$CimSession = New-CimSession -ComputerName $ServerName
|
||||||
|
}
|
||||||
|
|
||||||
|
# Build trigger
|
||||||
|
switch ($Frequency) {
|
||||||
|
'Once' { $trigger = New-ScheduledTaskTrigger -Once -At $At }
|
||||||
|
'Daily' { $trigger = New-ScheduledTaskTrigger -Daily -At $At.TimeOfDay }
|
||||||
|
'Weekly' { $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $DaysOfWeek -At $At.TimeOfDay }
|
||||||
|
'Monthly' { $trigger = New-ScheduledTaskTrigger -Monthly -MonthsOfYear $Months -DaysOfMonth $DaysOfMonth -At $At.TimeOfDay }
|
||||||
|
}
|
||||||
|
|
||||||
|
$action = New-ScheduledTaskAction -Execute $Command -Argument $Arguments
|
||||||
|
|
||||||
|
$settings = New-ScheduledTaskSettingsSet \
|
||||||
|
-StartWhenAvailable:$StartWhenAvailable.IsPresent \
|
||||||
|
-WakeToRun:$WakeToRun.IsPresent
|
||||||
|
|
||||||
|
if ($RestartIntervalMinutes -gt 0) {
|
||||||
|
$interval = New-TimeSpan -Minutes $RestartIntervalMinutes
|
||||||
|
$settings = $settings | Set-ScheduledTaskSettings -RestartOnFailureInterval $interval -RestartCount $RestartCount
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Registering task '$TaskName' on $target"
|
||||||
|
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 on $target."
|
||||||
}
|
}
|
||||||
|
|
||||||
# List tasks and exit
|
function List-ScheduledTasks {
|
||||||
if ($ListTasks) {
|
Param([string]$ServerName)
|
||||||
Write-Host "Listing Scheduled Tasks on" ($ServerName ? $ServerName : 'local')
|
if ($ServerName) { $CimSession = New-CimSession -ComputerName $ServerName }
|
||||||
Get-ScheduledTask -CimSession $CimSession |
|
Get-ScheduledTask -CimSession $CimSession |
|
||||||
Select-Object TaskName, State, Actions |
|
Select-Object TaskName, State, Actions |
|
||||||
Format-Table -AutoSize
|
Format-Table -AutoSize
|
||||||
|
}
|
||||||
|
|
||||||
|
function Show-Menu {
|
||||||
|
Clear-Host
|
||||||
|
Write-Host "=== Scheduled Task Manager ===" -ForegroundColor Cyan
|
||||||
|
Write-Host "1) List Tasks"
|
||||||
|
Write-Host "2) Create Task"
|
||||||
|
Write-Host "3) Exit"
|
||||||
|
return Read-Host 'Choose [1-3]'
|
||||||
|
}
|
||||||
|
|
||||||
|
function Prompt-TaskParameters {
|
||||||
|
$p = @{}
|
||||||
|
$p.ServerName = Read-Host 'Server (blank=local)'
|
||||||
|
$p.TaskName = Read-Host 'Task Name'
|
||||||
|
$p.Description= Read-Host 'Description'
|
||||||
|
$p.Frequency = Read-Host 'Frequency (Once,Daily,Weekly,Monthly)'
|
||||||
|
$p.At = [datetime](Read-Host 'Date/time (e.g. 2025-06-27 22:00)')
|
||||||
|
if ($p.Frequency -eq 'Weekly') {
|
||||||
|
$p.DaysOfWeek = (Read-Host 'DaysOfWeek (Monday,Tuesday...)').Split(',')
|
||||||
|
}
|
||||||
|
if ($p.Frequency -eq 'Monthly') {
|
||||||
|
$p.Months = (Read-Host 'Months (January,June...)').Split(',')
|
||||||
|
$p.DaysOfMonth = (Read-Host 'DaysOfMonth (1,15,28...)').Split(',') | % {[int]$_}
|
||||||
|
}
|
||||||
|
$cmd = Read-Host 'Command (default=powershell.exe)'
|
||||||
|
if ($cmd) { $p.Command = $cmd }
|
||||||
|
$p.Arguments = Read-Host 'Arguments (e.g. "-File C:\Scripts\MyScript.ps1")'
|
||||||
|
if ((Read-Host 'Start when available? (Y/N)') -eq 'Y') { $p.StartWhenAvailable = $true }
|
||||||
|
if ((Read-Host 'Wake to run? (Y/N)') -eq 'Y') { $p.WakeToRun = $true }
|
||||||
|
$retry = [int](Read-Host 'Retry interval (minutes, 0=none)')
|
||||||
|
if ($retry -gt 0) {
|
||||||
|
$p.RestartIntervalMinutes = $retry
|
||||||
|
$p.RestartCount = [int](Read-Host 'Retry count')
|
||||||
|
}
|
||||||
|
return $p
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main execution
|
||||||
|
if ($RunTUI) {
|
||||||
|
do {
|
||||||
|
switch (Show-Menu) {
|
||||||
|
'1' {
|
||||||
|
$srv = Read-Host 'Server to list (blank=local)'
|
||||||
|
List-ScheduledTasks -ServerName $srv
|
||||||
|
Write-Host 'Press Enter to continue...'
|
||||||
|
Read-Host
|
||||||
|
}
|
||||||
|
'2' {
|
||||||
|
$params = Prompt-TaskParameters
|
||||||
|
New-UniversalScheduledTask @params
|
||||||
|
Write-Host 'Press Enter to continue...'
|
||||||
|
Read-Host
|
||||||
|
}
|
||||||
|
'3' { break }
|
||||||
|
default { Write-Host "Invalid choice." -ForegroundColor Red }
|
||||||
|
}
|
||||||
|
} while ($true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
# Validate required parameters for task creation
|
Write-Host "No action specified. Use -RunTUI to launch the console interface." -ForegroundColor Yellow
|
||||||
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."
|
|
||||||
|
|||||||
Reference in New Issue
Block a user