|
- $ErrorActionPreference = 'Stop'
- $root = Split-Path -Parent $PSScriptRoot
- $tempFolder = Join-Path $root 'temp'
- $null = New-Item -ItemType Directory -Path $tempFolder -Force
- $powerShellX86 = Join-Path $env:SystemRoot 'SysWOW64\WindowsPowerShell\v1.0\powershell.exe'
- $bridge = Join-Path $PSScriptRoot 'run-hidden-tool.ps1'
- $shell = New-Object -ComObject WScript.Shell
- foreach ($wait in @($true, $false)) {
- $token = [guid]::NewGuid().ToString('N')
- $requestPath = Join-Path $tempFolder ($token + '.request.json')
- $resultPath = Join-Path $tempFolder ($token + '.result.json')
- try {
- @{ ScriptName = 'dispatch-control.ps1'; ConfigPath = (Join-Path $root 'config.ini');
- Parameters = @{ Action = 'Status' } } |
- ConvertTo-Json -Compress -Depth 4 |
- Set-Content -LiteralPath $requestPath -Encoding UTF8
- $command = '"' + $powerShellX86 + '" -NoProfile -ExecutionPolicy Bypass -File "' +
- $bridge + '" -RequestPath "' + $requestPath + '" -ResultPath "' + $resultPath + '"'
- $exitCode = $shell.Run($command, 0, $wait)
- if (-not $wait) {
- for ($attempt = 0; $attempt -lt 160 -and
- -not (Test-Path -LiteralPath $resultPath -PathType Leaf); $attempt++) {
- Start-Sleep -Milliseconds 250
- }
- }
- if (-not (Test-Path -LiteralPath $resultPath -PathType Leaf)) {
- throw 'Hidden runner produced no result'
- }
- $response = Get-Content -LiteralPath $resultPath -Raw -Encoding UTF8 | ConvertFrom-Json
- if (($wait -and $exitCode -ne 0) -or -not $response.Success) {
- throw ('Hidden runner failed: ' + $response.Error)
- }
- $status = $response.Output | ConvertFrom-Json
- if ($null -eq $status.Paused) { throw 'Hidden runner status response is incomplete' }
- }
- finally {
- if (Test-Path -LiteralPath $requestPath) { Remove-Item -LiteralPath $requestPath -Force }
- if (Test-Path -LiteralPath $resultPath) { Remove-Item -LiteralPath $resultPath -Force }
- }
- }
- Write-Output 'Synchronous and asynchronous hidden helpers returned dispatch status'
|