public/Invoke-OctaScheduler.ps1
|
function Invoke-OctaScheduler { <# .SYNOPSIS Creates/lists/removes a Windows Task Scheduler entry that runs an Octa category or Quick Clean non-interactively on a schedule. 007 US5. Every scheduled run invokes the exported cmdlets directly (never Invoke-Octa/the interactive menu), consistent with the non-interactive guard already in Show-OctaMenu. Runs elevated (RunLevel Highest) always, disclosed here before creating the task, since most categories require it anyway and a scheduled run has no one present to grant elevation on demand. #> [CmdletBinding()] param( [switch]$Create, [string]$Category, [ValidateSet('Daily', 'Weekly', 'Monthly')] [string]$Frequency, [string]$Remove ) $taskPath = '\Octa\' if ($Remove) { $existing = Get-ScheduledTask -TaskPath $taskPath -TaskName $Remove -ErrorAction SilentlyContinue if (-not $existing) { return [pscustomobject]@{ Status = 'NotFound'; Message = "No scheduled Octa run named: $Remove" } } Unregister-ScheduledTask -TaskPath $taskPath -TaskName $Remove -Confirm:$false return [pscustomobject]@{ Status = 'Success'; Message = "Removed scheduled run: $Remove" } } if ($Create) { if (-not $Category -or -not $Frequency) { return [pscustomobject]@{ Status = 'Error'; Message = 'Both -Category and -Frequency are required to create a scheduled run.' } } $cmdletCall = if ($Category -eq 'quick-clean') { 'Invoke-OctaQuickClean -Apply -Yes -Quiet' } else { "Invoke-OctaCategory -CategoryId '$Category' -Apply -Yes -Quiet" } $command = "Import-Module Octa -Force; $cmdletCall" Write-Host 'This scheduled task will run elevated (Administrator), since most categories require it.' $taskName = "Octa-$Category-$Frequency-$([guid]::NewGuid().ToString('N').Substring(0, 8))" $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NoProfile -WindowStyle Hidden -Command `"$command`"" $trigger = switch ($Frequency) { 'Daily' { New-ScheduledTaskTrigger -Daily -At 3am } 'Weekly' { New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am } 'Monthly' { # New-ScheduledTaskTrigger has no -Monthly parameter set (verified: only Once/ # Daily/Weekly/Startup/Logon exist in PS 5.1's ScheduledTasks module) - a genuine # calendar-monthly trigger needs the underlying MSFT_TaskMonthlyTrigger CIM class # directly, not an approximation like "every 4 weeks" (which drifts across # different days of the month over time). $monthlyTriggerClass = Get-CimClass -Namespace Root/Microsoft/Windows/TaskScheduler -ClassName MSFT_TaskMonthlyTrigger $t = New-CimInstance -CimClass $monthlyTriggerClass -ClientOnly $t.DaysOfMonth = 1 # bit 0 = day 1 of the month $t.MonthOfYear = 4095 # all 12 months (bits 0-11 set) $t.StartBoundary = (Get-Date -Hour 3 -Minute 0 -Second 0).ToString('yyyy-MM-ddTHH:mm:ss') $t.Enabled = $true $t } } $principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType S4U -RunLevel Highest Register-ScheduledTask -TaskName $taskName -TaskPath $taskPath -Action $action -Trigger $trigger -Principal $principal | Out-Null return [pscustomobject]@{ Status = 'Success'; TaskName = $taskName } } $tasks = @(Get-ScheduledTask -TaskPath $taskPath -ErrorAction SilentlyContinue) foreach ($t in $tasks) { Write-Host ("{0,-10} {1}" -f $t.State, $t.TaskName) } return $tasks } |