fixed the excess parentheses and ensured each function block closes properly.

This commit is contained in:
2025-06-27 02:49:17 +00:00
parent 2b1d775158
commit 5f4f0a9808

View File

@@ -1,13 +1,14 @@
<#
.SYNOPSIS
Interactive TUI for listing and creating Scheduled Tasks on local or remote servers under SYSTEM account.
Interactive TUI for managing (list/create) 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
- Supports Once/Daily/Weekly/Monthly triggers
- Custom actions, settings (start when available, wake to run, retry on failure)
- Remote targeting via CIM session
.PARAMETER RunTUI
Launch the text-based UI.
#>
[CmdletBinding()]
Param(
@@ -32,14 +33,12 @@ function New-UniversalScheduledTask {
[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 }
@@ -47,43 +46,33 @@ function New-UniversalScheduledTask {
'Weekly' { $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $DaysOfWeek -At $At.TimeOfDay }
'Monthly' { $trigger = New-ScheduledTaskTrigger -Monthly -MonthsOfYear $Months -DaysOfMonth $DaysOfMonth -At $At.TimeOfDay }
}
# Action and settings
$action = New-ScheduledTaskAction -Execute $Command -Argument $Arguments
$settings = New-ScheduledTaskSettingsSet \
-StartWhenAvailable:$StartWhenAvailable.IsPresent \
-WakeToRun:$WakeToRun.IsPresent
$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
}
# Register task
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
Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -Settings $settings -Description $Description -User 'NT AUTHORITY\\SYSTEM' -RunLevel Highest${($CimSession) ? ' -CimSession $CimSession' : ''} -Force
Write-Host "Task '$TaskName' created successfully on $target."
}
function List-ScheduledTasks {
[CmdletBinding()]
Param([string]$ServerName)
if ($ServerName) { $CimSession = New-CimSession -ComputerName $ServerName }
Get-ScheduledTask -CimSession $CimSession |
Select-Object TaskName, State, Actions |
Format-Table -AutoSize
# Determine target
$target = if ($ServerName) { $ServerName } else { 'local' }
if ($ServerName) {
Write-Host "Creating CIM session to $ServerName..."
$CimSession = New-CimSession -ComputerName $ServerName
}
Write-Host "Listing tasks on $target"
Get-ScheduledTask${($CimSession) ? ' -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"
@@ -93,24 +82,24 @@ function Show-Menu {
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)')
$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(',')
$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]$_}
$p.Months = (Read-Host 'Months (January,June)').Split(',')
$p.DaysOfMonth = (Read-Host 'DaysOfMonth (1,15)').Split(',') | ForEach-Object {[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")'
$p.Arguments = Read-Host 'Arguments'
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 ((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')
@@ -118,7 +107,7 @@ function Prompt-TaskParameters {
return $p
}
# Main execution
# Entry point
if ($RunTUI) {
do {
switch (Show-Menu) {
@@ -135,10 +124,10 @@ if ($RunTUI) {
Read-Host
}
'3' { break }
default { Write-Host "Invalid choice." -ForegroundColor Red }
default { Write-Host 'Invalid choice.' -ForegroundColor Red }
}
} while ($true)
return
}
Write-Host "No action specified. Use -RunTUI to launch the console interface." -ForegroundColor Yellow
Write-Host 'No action specified. Use -RunTUI to launch the console interface.' -ForegroundColor Yellow