param( [string]$TagName = "OtOpcUaParityTest_001.Counter", [int]$LookbackMinutes = 1440, [int]$MaxRows = 1, [int]$AttachDelaySeconds = 3, [string]$OutputPath = $null ) $ErrorActionPreference = "Stop" $repoRoot = Split-Path -Parent $PSScriptRoot $fridaScript = Join-Path $PSScriptRoot "frida\aahclientmanaged-open-query.js" $nativeReadScript = Join-Path $PSScriptRoot "Test-AahClientManagedReadIntegrated.ps1" if ([string]::IsNullOrWhiteSpace($OutputPath)) { $stamp = Get-Date -Format "yyyyMMdd-HHmmss" $OutputPath = Join-Path $repoRoot "docs\reverse-engineering\frida-aahclientmanaged-attach-read-$stamp.ndjson" } $outputDirectory = Split-Path -Parent $OutputPath New-Item -ItemType Directory -Force -Path $outputDirectory | Out-Null $powershell = Join-Path $env:WINDIR "System32\WindowsPowerShell\v1.0\powershell.exe" $childOut = Join-Path $env:TEMP ("histsdk-native-read-{0}.out.log" -f ([Guid]::NewGuid().ToString("N"))) $childErr = Join-Path $env:TEMP ("histsdk-native-read-{0}.err.log" -f ([Guid]::NewGuid().ToString("N"))) $childArgs = @( "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", $nativeReadScript, "-TagName", $TagName, "-LookbackMinutes", $LookbackMinutes.ToString(), "-MaxRows", $MaxRows.ToString(), "-ConnectionWaitSeconds", "15", "-PreReadSleepSeconds", $AttachDelaySeconds.ToString(), "-DumpLoadedModules" ) Write-Host "Starting native read process and pausing $AttachDelaySeconds second(s) after aahClientManaged.dll load." $process = Start-Process -FilePath $powershell -ArgumentList $childArgs -WorkingDirectory $repoRoot -RedirectStandardOutput $childOut -RedirectStandardError $childErr -PassThru -WindowStyle Hidden try { Start-Sleep -Seconds 1 Write-Host "Attaching Frida to PID $($process.Id). Capture: $OutputPath" & frida -q -p $process.Id -l $fridaScript 2>&1 | Tee-Object -FilePath $OutputPath if (-not $process.HasExited) { $process.WaitForExit(30000) | Out-Null } } finally { if (-not $process.HasExited) { Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue } "--- native stdout ---" | Tee-Object -FilePath $OutputPath -Append Get-Content -LiteralPath $childOut -ErrorAction SilentlyContinue | Tee-Object -FilePath $OutputPath -Append "--- native stderr ---" | Tee-Object -FilePath $OutputPath -Append Get-Content -LiteralPath $childErr -ErrorAction SilentlyContinue | Tee-Object -FilePath $OutputPath -Append Remove-Item -LiteralPath $childOut, $childErr -ErrorAction SilentlyContinue } Write-Host "Capture complete: $OutputPath"