- 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 <noreply@anthropic.com>
120 lines
4.1 KiB
PowerShell
120 lines
4.1 KiB
PowerShell
#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
|
|
}
|