#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 Monitors: C:\Dnc_Files\Q for *.pun files .PARAMETER Uninstall Remove the scheduled task instead of installing. .EXAMPLE .\Install-ScheduledTask.ps1 Installs the scheduled task .EXAMPLE .\Install-ScheduledTask.ps1 -Uninstall Removes the scheduled task #> param( [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: C:\Dnc_Files\Q" Write-Host " File Filter: *.pun" 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`"" # 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 }