145 lines
5.0 KiB
PowerShell
145 lines
5.0 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Interactive TUI for listing and creating Scheduled Tasks on local or remote servers under SYSTEM account.
|
|
|
|
.DESCRIPTION
|
|
- Menu-driven console interface
|
|
- Always runs as SYSTEM (highest privilege)
|
|
- Supports ONCE/DAILY/WEEKLY/MONTHLY triggers
|
|
- Custom actions, settings (start when available, wake to run, retry on failure)
|
|
- Remote targeting via CIM session
|
|
#>
|
|
[CmdletBinding()]
|
|
Param(
|
|
[switch]$RunTUI
|
|
)
|
|
|
|
function New-UniversalScheduledTask {
|
|
[CmdletBinding()]
|
|
Param(
|
|
[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."
|
|
}
|
|
|
|
function List-ScheduledTasks {
|
|
Param([string]$ServerName)
|
|
if ($ServerName) { $CimSession = New-CimSession -ComputerName $ServerName }
|
|
Get-ScheduledTask -CimSession $CimSession |
|
|
Select-Object TaskName, State, Actions |
|
|
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
|
|
}
|
|
|
|
Write-Host "No action specified. Use -RunTUI to launch the console interface." -ForegroundColor Yellow
|