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 .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 .DESCRIPTION
- Menu-driven console interface - Menu-driven console interface
- Always runs as SYSTEM (highest privilege) - 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) - Custom actions, settings (start when available, wake to run, retry on failure)
- Remote targeting via CIM session - Remote targeting via CIM session
.PARAMETER RunTUI
Launch the text-based UI.
#> #>
[CmdletBinding()] [CmdletBinding()]
Param( Param(
@@ -32,14 +33,12 @@ function New-UniversalScheduledTask {
[int]$RestartIntervalMinutes = 0, [int]$RestartIntervalMinutes = 0,
[int]$RestartCount = 0 [int]$RestartCount = 0
) )
# Determine target # Determine target
$target = if ($ServerName) { $ServerName } else { 'local' } $target = if ($ServerName) { $ServerName } else { 'local' }
if ($ServerName) { if ($ServerName) {
Write-Host "Creating CIM session to $ServerName..." Write-Host "Creating CIM session to $ServerName..."
$CimSession = New-CimSession -ComputerName $ServerName $CimSession = New-CimSession -ComputerName $ServerName
} }
# Build trigger # Build trigger
switch ($Frequency) { switch ($Frequency) {
'Once' { $trigger = New-ScheduledTaskTrigger -Once -At $At } 'Once' { $trigger = New-ScheduledTaskTrigger -Once -At $At }
@@ -47,43 +46,33 @@ function New-UniversalScheduledTask {
'Weekly' { $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $DaysOfWeek -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 } 'Monthly' { $trigger = New-ScheduledTaskTrigger -Monthly -MonthsOfYear $Months -DaysOfMonth $DaysOfMonth -At $At.TimeOfDay }
} }
# Action and settings
$action = New-ScheduledTaskAction -Execute $Command -Argument $Arguments $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) { if ($RestartIntervalMinutes -gt 0) {
$interval = New-TimeSpan -Minutes $RestartIntervalMinutes $interval = New-TimeSpan -Minutes $RestartIntervalMinutes
$settings = $settings | Set-ScheduledTaskSettings -RestartOnFailureInterval $interval -RestartCount $RestartCount $settings = $settings | Set-ScheduledTaskSettings -RestartOnFailureInterval $interval -RestartCount $RestartCount
} }
# Register task
Write-Host "Registering task '$TaskName' on $target" Write-Host "Registering task '$TaskName' on $target"
Register-ScheduledTask \ Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -Settings $settings -Description $Description -User 'NT AUTHORITY\\SYSTEM' -RunLevel Highest${($CimSession) ? ' -CimSession $CimSession' : ''} -Force
-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." Write-Host "Task '$TaskName' created successfully on $target."
} }
function List-ScheduledTasks { function List-ScheduledTasks {
[CmdletBinding()]
Param([string]$ServerName) Param([string]$ServerName)
if ($ServerName) { $CimSession = New-CimSession -ComputerName $ServerName } # Determine target
Get-ScheduledTask -CimSession $CimSession | $target = if ($ServerName) { $ServerName } else { 'local' }
Select-Object TaskName, State, Actions | if ($ServerName) {
Format-Table -AutoSize 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 { function Show-Menu {
Clear-Host
Write-Host "=== Scheduled Task Manager ===" -ForegroundColor Cyan Write-Host "=== Scheduled Task Manager ===" -ForegroundColor Cyan
Write-Host "1) List Tasks" Write-Host "1) List Tasks"
Write-Host "2) Create Task" Write-Host "2) Create Task"
@@ -99,18 +88,18 @@ function Prompt-TaskParameters {
$p.Frequency = Read-Host 'Frequency (Once,Daily,Weekly,Monthly)' $p.Frequency = Read-Host 'Frequency (Once,Daily,Weekly,Monthly)'
$p.At = [datetime](Read-Host 'Date/time (e.g. 2025-06-27 22:00)') $p.At = [datetime](Read-Host 'Date/time (e.g. 2025-06-27 22:00)')
if ($p.Frequency -eq 'Weekly') { 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') { if ($p.Frequency -eq 'Monthly') {
$p.Months = (Read-Host 'Months (January,June...)').Split(',') $p.Months = (Read-Host 'Months (January,June)').Split(',')
$p.DaysOfMonth = (Read-Host 'DaysOfMonth (1,15,28...)').Split(',') | % {[int]$_} $p.DaysOfMonth = (Read-Host 'DaysOfMonth (1,15)').Split(',') | ForEach-Object {[int]$_}
} }
$cmd = Read-Host 'Command (default=powershell.exe)' $cmd = Read-Host 'Command (default=powershell.exe)'
if ($cmd) { $p.Command = $cmd } 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 'Start when available? (Y/N)') -eq 'Y') { $p.StartWhenAvailable = $true }
if ((Read-Host 'Wake to run? (Y/N)') -eq 'Y') { $p.WakeToRun = $true } if ((Read-Host 'Wake to run? (Y/N)') -eq 'Y') { $p.WakeToRun = $true }
$retry = [int](Read-Host 'Retry interval (minutes, 0=none)') $retry = [int](Read-Host 'Retry interval minutes (0=none)')
if ($retry -gt 0) { if ($retry -gt 0) {
$p.RestartIntervalMinutes = $retry $p.RestartIntervalMinutes = $retry
$p.RestartCount = [int](Read-Host 'Retry count') $p.RestartCount = [int](Read-Host 'Retry count')
@@ -118,7 +107,7 @@ function Prompt-TaskParameters {
return $p return $p
} }
# Main execution # Entry point
if ($RunTUI) { if ($RunTUI) {
do { do {
switch (Show-Menu) { switch (Show-Menu) {
@@ -135,10 +124,10 @@ if ($RunTUI) {
Read-Host Read-Host
} }
'3' { break } '3' { break }
default { Write-Host "Invalid choice." -ForegroundColor Red } default { Write-Host 'Invalid choice.' -ForegroundColor Red }
} }
} while ($true) } while ($true)
return 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