From 77d6671de3de3b629a24ffca84a85e539cbc42fb Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Mon, 7 Sep 2026 15:11:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20PowerShell=207=20=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=BC=BA=E5=A4=B1=E7=9A=84=E4=B8=AD=E7=BB=A7?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=20(#112)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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); } }