#Requires -RunAsAdministrator <# .SYNOPSIS Installs eDNC Special Character Fix as a scheduled task that runs at user logon with visible UI. .DESCRIPTION Creates a Windows scheduled task that: - Runs at user logon (visible window for debugging) - Runs with highest privileges (Administrator) - Shows the monitoring UI to the logged-in user - 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" # IMPORTANT: Always use the local installation path, NOT $PSScriptRoot # This ensures the task runs from C:\eDNC-Fix even if installed from S:\ share $LocalInstallPath = "C:\eDNC-Fix" $ScriptPath = Join-Path $LocalInstallPath "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 local installation exists if (-not (Test-Path $LocalInstallPath)) { Write-Host "[ERROR] Local installation folder not found: $LocalInstallPath" -ForegroundColor Red Write-Host "" Write-Host "Please run Deploy.bat first to copy files to $LocalInstallPath" -ForegroundColor Yellow exit 1 } if (-not (Test-Path $ScriptPath)) { Write-Host "[ERROR] Script not found: $ScriptPath" -ForegroundColor Red Write-Host "" Write-Host "Please run Deploy.bat first to copy files to $LocalInstallPath" -ForegroundColor Yellow 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 (visible window for debugging) $Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$ScriptPath`"" # Create the scheduled task $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $Arguments -WorkingDirectory $LocalInstallPath # Trigger: At user logon (shows UI to logged-in user) $Trigger = New-ScheduledTaskTrigger -AtLogOn # Settings $Settings = New-ScheduledTaskSettingsSet ` -AllowStartIfOnBatteries ` -DontStopIfGoingOnBatteries ` -StartWhenAvailable ` -RestartInterval (New-TimeSpan -Minutes 1) ` -RestartCount 3 ` -ExecutionTimeLimit (New-TimeSpan -Days 9999) # Principal: Run as the logged-in user with highest privileges (for visible UI) $Principal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Users" -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 when any user logs in." -ForegroundColor White Write-Host "A visible PowerShell window will show the monitoring UI." -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 }