From afad8e0c56a009566a71c2e83ab19f2c2abe6d4f Mon Sep 17 00:00:00 2001 From: cproudlock Date: Fri, 12 Dec 2025 08:41:34 -0500 Subject: [PATCH] v1.3.1: Add Install-ScheduledTask.ps1 for service deployment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Creates scheduled task that runs as SYSTEM at boot - Runs with highest privileges, no user login required - Auto-restarts on failure (3 attempts, 1 min interval) - Runs hidden (no console window) - Supports -Uninstall to remove task 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- Install-ScheduledTask.ps1 | 119 ++++++++++++++++++++++++++++++++++++++ README.md | 26 ++++++++- 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 Install-ScheduledTask.ps1 diff --git a/Install-ScheduledTask.ps1 b/Install-ScheduledTask.ps1 new file mode 100644 index 0000000..3fe54b6 --- /dev/null +++ b/Install-ScheduledTask.ps1 @@ -0,0 +1,119 @@ +#Requires -RunAsAdministrator +<# +.SYNOPSIS + Installs eDNC Special Character Fix as a scheduled task that runs at startup with admin rights. + +.DESCRIPTION + Creates a Windows scheduled task that: + - Runs at system startup + - Runs with highest privileges (Administrator) + - Runs whether user is logged on or not + - Automatically restarts if it stops + +.PARAMETER WatchFolder + The folder to monitor. Default: C:\Dnc_Files\Q + +.PARAMETER FileFilter + File pattern to watch. Default: *.pun + +.PARAMETER Uninstall + Remove the scheduled task instead of installing. + +.EXAMPLE + .\Install-ScheduledTask.ps1 + Installs with default settings + +.EXAMPLE + .\Install-ScheduledTask.ps1 -WatchFolder "D:\DNC\Programs" -FileFilter "*.nc" + Installs with custom folder and filter + +.EXAMPLE + .\Install-ScheduledTask.ps1 -Uninstall + Removes the scheduled task +#> + +param( + [string]$WatchFolder = "C:\Dnc_Files\Q", + [string]$FileFilter = "*.pun", + [switch]$Uninstall +) + +$TaskName = "eDNC Special Character Fix" +$ScriptPath = Join-Path $PSScriptRoot "eDNC-SpecialCharFix.ps1" + +if ($Uninstall) { + Write-Host "Removing scheduled task '$TaskName'..." -ForegroundColor Yellow + Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue + Write-Host "Task removed." -ForegroundColor Green + exit 0 +} + +# Verify script exists +if (-not (Test-Path $ScriptPath)) { + Write-Host "[ERROR] Script not found: $ScriptPath" -ForegroundColor Red + exit 1 +} + +Write-Host "" +Write-Host "Installing eDNC Special Character Fix as Scheduled Task" -ForegroundColor Cyan +Write-Host "========================================================" -ForegroundColor Cyan +Write-Host "" +Write-Host " Task Name: $TaskName" +Write-Host " Script: $ScriptPath" +Write-Host " Watch Folder: $WatchFolder" +Write-Host " File Filter: $FileFilter" +Write-Host "" + +# Remove existing task if present +$existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue +if ($existingTask) { + Write-Host "Removing existing task..." -ForegroundColor Yellow + Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false +} + +# Build the PowerShell command +$Arguments = "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$ScriptPath`" -WatchFolder `"$WatchFolder`" -FileFilter `"$FileFilter`"" + +# Create the scheduled task +$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $Arguments -WorkingDirectory $PSScriptRoot + +# Trigger: At startup +$Trigger = New-ScheduledTaskTrigger -AtStartup + +# Settings +$Settings = New-ScheduledTaskSettingsSet ` + -AllowStartIfOnBatteries ` + -DontStopIfGoingOnBatteries ` + -StartWhenAvailable ` + -RestartInterval (New-TimeSpan -Minutes 1) ` + -RestartCount 3 ` + -ExecutionTimeLimit (New-TimeSpan -Days 9999) + +# Principal: Run as SYSTEM with highest privileges +$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest + +# Register the task +$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings -Principal $Principal -Description "Monitors DNC folder and removes invalid special characters (0xFF) from program files." + +Register-ScheduledTask -TaskName $TaskName -InputObject $Task | Out-Null + +Write-Host "Scheduled task created successfully!" -ForegroundColor Green +Write-Host "" +Write-Host "The task will start automatically at system boot." -ForegroundColor White +Write-Host "" +Write-Host "To start it now, run:" -ForegroundColor Yellow +Write-Host " Start-ScheduledTask -TaskName '$TaskName'" +Write-Host "" +Write-Host "To check status:" -ForegroundColor Yellow +Write-Host " Get-ScheduledTask -TaskName '$TaskName' | Select-Object State" +Write-Host "" +Write-Host "To uninstall:" -ForegroundColor Yellow +Write-Host " .\Install-ScheduledTask.ps1 -Uninstall" +Write-Host "" + +# Offer to start now +$response = Read-Host "Start the task now? (Y/N)" +if ($response -eq 'Y' -or $response -eq 'y') { + Start-ScheduledTask -TaskName $TaskName + Write-Host "Task started." -ForegroundColor Green +} diff --git a/README.md b/README.md index 8e0f7d2..9f3e847 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,30 @@ This utility monitors a folder for DNC files and automatically strips specified **Note:** The batch file automatically requests Administrator privileges via UAC prompt. -### Run at Startup (Optional) +### Run as Scheduled Task (Recommended) -To run automatically at Windows startup: +For production use, install as a scheduled task that runs as SYSTEM with admin rights: + +```powershell +# Run as Administrator +.\Install-ScheduledTask.ps1 + +# With custom settings +.\Install-ScheduledTask.ps1 -WatchFolder "D:\DNC\Programs" -FileFilter "*.nc" + +# To remove +.\Install-ScheduledTask.ps1 -Uninstall +``` + +This creates a task that: +- Starts at system boot (before any user logs in) +- Runs as SYSTEM with highest privileges +- Auto-restarts if it crashes +- Runs hidden (no console window) + +### Run at User Login (Alternative) + +To run when a specific user logs in: 1. Press `Win + R`, type `shell:startup`, press Enter 2. Create a shortcut to `Run-eDNCFix.bat` in the Startup folder @@ -131,6 +152,7 @@ The script uses exponential backoff (500ms → 1s → 2s → 4s → up to 16s) f | Version | Date | Changes | |---------|------|---------| +| 1.3.1 | 2025-12-12 | Added Install-ScheduledTask.ps1 for running as SYSTEM service | | 1.3.0 | 2025-12-12 | Fixed header UI, live status/stats, auto-elevate to Administrator | | 1.2.1 | 2025-12-12 | Added GE Aerospace ASCII banner | | 1.2.0 | 2025-12-12 | Immediate processing: 50ms delay, aggressive retry (100ms+), 15 attempts |