fix: PowerShell 7 正确创建缺失的中继任务 (#112)

This commit is contained in:
WuYongJie 2026-09-07 15:11:00 +08:00
parent c96b314a9d
commit 77d6671de3
2 changed files with 10 additions and 12 deletions

View File

@ -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'

View File

@ -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);
}
}