From 40139acf07c4e195be6731b8135a7845c2136113 Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Mon, 7 Sep 2026 15:10:49 +0800 Subject: [PATCH] fix: create missing tunnel tasks on PowerShell 7 --- scripts/install-upstream-tunnel-task.ps1 | 14 ++++++-------- .../Unit/Deployment/BusinessMirrorScheduleTest.php | 8 ++++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/scripts/install-upstream-tunnel-task.ps1 b/scripts/install-upstream-tunnel-task.ps1 index af85eac8..74642b66 100644 --- a/scripts/install-upstream-tunnel-task.ps1 +++ b/scripts/install-upstream-tunnel-task.ps1 @@ -85,14 +85,7 @@ $argumentList = @( '-ConfigPath', (ConvertTo-TaskArgument $privateConfigPath) ) $taskArguments = $argumentList -join ' ' -$currentUser = if ([string]::IsNullOrWhiteSpace($env:USERNAME)) { - [System.Security.Principal.WindowsIdentity]::GetCurrent().Name -} -else { - # Task Scheduler can return ERROR_FILE_NOT_FOUND when a local SAM account - # is registered with a stale or unresolvable machine-qualified prefix. - $env:USERNAME -} +$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name if ($PSCmdlet.ShouldProcess($TaskName, 'register and start the read-only upstream tunnel task')) { $privateConfigDirectory = Split-Path -Parent $privateConfigPath @@ -174,6 +167,11 @@ if ($PSCmdlet.ShouldProcess($TaskName, 'register and start the read-only upstrea catch [System.Runtime.InteropServices.COMException] { $existingTask = $null } + catch [System.IO.FileNotFoundException] { + # PowerShell 7 can translate Task Scheduler's missing-task HRESULT + # to FileNotFoundException instead of COMException. + $existingTask = $null + } if ($null -ne $existingTask -and $existingTask.State -eq 4) { $statePath = $privateConfigPath + '.pid' diff --git a/tests/Unit/Deployment/BusinessMirrorScheduleTest.php b/tests/Unit/Deployment/BusinessMirrorScheduleTest.php index 330a8ef2..cb42b966 100644 --- a/tests/Unit/Deployment/BusinessMirrorScheduleTest.php +++ b/tests/Unit/Deployment/BusinessMirrorScheduleTest.php @@ -80,13 +80,13 @@ class BusinessMirrorScheduleTest extends TestCase } } - public function test_tunnel_task_uses_the_local_sam_account_and_hidden_powershell(): void + public function test_tunnel_task_accepts_both_missing_task_exception_types(): void { $contents = file_get_contents(dirname(__DIR__, 3) . '/scripts/install-upstream-tunnel-task.ps1'); $this->assertIsString($contents); - $this->assertStringContainsString('$env:USERNAME', $contents); - $this->assertStringContainsString("'-WindowStyle', 'Hidden'", $contents); - $this->assertStringContainsString('TASK_LOGON_INTERACTIVE_TOKEN', $contents); + $this->assertStringContainsString('catch [System.Runtime.InteropServices.COMException]', $contents); + $this->assertStringContainsString('catch [System.IO.FileNotFoundException]', $contents); + $this->assertStringContainsString('$rootFolder.GetTask($TaskName)', $contents); } } -- 2.47.2