From d7972dfdd09dba27fb990450d7ec7bed5be3ae87 Mon Sep 17 00:00:00 2001 From: seunggabi Date: Sat, 21 Feb 2026 16:35:43 +0900 Subject: [PATCH] (#38) fix: hide PowerShell window for scheduled task execution Add -WindowStyle Hidden and -NonInteractive to pwsh arguments in WinOpsTask.xml and TaskScheduler.psm1 so the console window does not remain open after Task Scheduler runs the cleanup script. Also update install.ps1 to auto-detect an existing scheduled task on reinstall and update it automatically without requiring -InstallScheduledTask. Co-Authored-By: Claude Sonnet 4.6 --- install.ps1 | 13 ++++++++----- scheduler/TaskScheduler.psm1 | 2 +- scheduler/WinOpsTask.xml | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/install.ps1 b/install.ps1 index 06391c4..1dc9fb4 100644 --- a/install.ps1 +++ b/install.ps1 @@ -316,9 +316,12 @@ if ($AddToPath) { Write-InstallMessage "Step 5: Skipping PATH addition (use -AddToPath to enable)" -Type Info } -# Step 6: Install scheduled task (optional) -if ($InstallScheduledTask) { - Write-InstallMessage "Step 6: Installing scheduled task..." -Type Info +# Step 6: Install scheduled task (optional, or auto-update if already installed) +$taskAlreadyExists = Get-ScheduledTask -TaskName "Win-Ops System Cleanup" -ErrorAction SilentlyContinue + +if ($InstallScheduledTask -or $taskAlreadyExists) { + $reason = if ($taskAlreadyExists -and -not $InstallScheduledTask) { "updating existing" } else { "installing" } + Write-InstallMessage "Step 6: $(if ($taskAlreadyExists -and -not $InstallScheduledTask) { 'Updating existing scheduled task (auto-detected)...' } else { 'Installing scheduled task...' })" -Type Info if (-not (Test-IsElevated)) { Write-InstallMessage "Administrator privileges required for scheduled task installation" -Type Error @@ -329,9 +332,9 @@ if ($InstallScheduledTask) { Import-Module $schedulerModule -Force Install-WinOpsScheduledTask -Interval $ScheduleInterval -Force - Write-InstallMessage "Scheduled task installed successfully" -Type Success + Write-InstallMessage "Scheduled task $reason successfully" -Type Success } catch { - Write-InstallMessage "Failed to install scheduled task: $_" -Type Error + Write-InstallMessage "Failed to $reason scheduled task: $_" -Type Error Write-InstallMessage "You can install it manually later using: Install-WinOpsScheduledTask" -Type Info } } diff --git a/scheduler/TaskScheduler.psm1 b/scheduler/TaskScheduler.psm1 index 2e58877..9afb4a3 100644 --- a/scheduler/TaskScheduler.psm1 +++ b/scheduler/TaskScheduler.psm1 @@ -150,7 +150,7 @@ function Install-WinOpsScheduledTask { # Create task action $action = New-ScheduledTaskAction ` -Execute "pwsh.exe" ` - -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`"" ` + -Argument "-NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$scriptPath`"" ` -WorkingDirectory $workingDir # Create task trigger based on interval diff --git a/scheduler/WinOpsTask.xml b/scheduler/WinOpsTask.xml index 448dff7..afab3b5 100644 --- a/scheduler/WinOpsTask.xml +++ b/scheduler/WinOpsTask.xml @@ -48,7 +48,7 @@ pwsh.exe - -NoProfile -ExecutionPolicy Bypass -File "SCRIPT_PATH" + -NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File "SCRIPT_PATH" WORKING_DIR