You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 line
2.7KB

  1. $ErrorActionPreference = 'Stop'
  2. $root = Split-Path -Parent $PSScriptRoot
  3. $hta = Join-Path $root 'MassMailer.hta'
  4. $mshta = Join-Path $env:SystemRoot 'SysWOW64\mshta.exe'
  5. $existing = @(Get-CimInstance Win32_Process -Filter "Name='mshta.exe'" |
  6. Where-Object { $_.CommandLine -like '*MassMailer.hta*' })
  7. if ($existing.Count) { throw 'Close the existing MassMailer HTA before this startup smoke test' }
  8. $started = $null
  9. try {
  10. $started = Start-Process -FilePath $mshta -ArgumentList ('"' + $hta + '"') -PassThru -WindowStyle Hidden
  11. $active = @()
  12. $session = $null
  13. for ($wait = 0; $wait -lt 25; $wait++) {
  14. Start-Sleep -Seconds 1
  15. $active = @(Get-CimInstance Win32_Process -Filter "Name='mshta.exe'" |
  16. Where-Object { $_.CommandLine -like '*MassMailer.hta*' })
  17. if ($active.Count -eq 1) {
  18. $session = Join-Path $root ('temp\session-' + $active[0].ProcessId + '.txt')
  19. if (Test-Path -LiteralPath $session -PathType Leaf) { break }
  20. }
  21. }
  22. if ($active.Count -ne 1) { throw 'Expected one running MassMailer HTA process' }
  23. if (-not (Test-Path -LiteralPath $session -PathType Leaf)) {
  24. throw 'HTA did not create an active dispatcher session heartbeat'
  25. }
  26. if ((Get-Item -LiteralPath $session).LastWriteTimeUtc -lt [DateTime]::UtcNow.AddSeconds(-20)) {
  27. throw 'HTA dispatcher heartbeat is stale'
  28. }
  29. $powerShellX86 = Join-Path $env:SystemRoot 'SysWOW64\WindowsPowerShell\v1.0\powershell.exe'
  30. $token = [System.IO.File]::ReadAllText($session).Trim()
  31. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-session-guard.ps1') -SessionPid $active[0].ProcessId -SessionToken $token
  32. if ($LASTEXITCODE -ne 0) { throw 'Open-window process guard rejected the active HTA' }
  33. Write-Output 'Hidden HTA startup and open-window dispatcher session passed'
  34. }
  35. finally {
  36. $active = @(Get-CimInstance Win32_Process -Filter "Name='mshta.exe'" |
  37. Where-Object { $_.CommandLine -like '*MassMailer.hta*' })
  38. foreach ($process in $active) {
  39. if ($process.ProcessId -notin @($existing | ForEach-Object ProcessId)) {
  40. Stop-Process -Id $process.ProcessId -Force -ErrorAction SilentlyContinue
  41. $session = Join-Path $root ('temp\session-' + $process.ProcessId + '.txt')
  42. $full = [System.IO.Path]::GetFullPath($session)
  43. $tempRoot = [System.IO.Path]::GetFullPath((Join-Path $root 'temp'))
  44. if ($full.StartsWith($tempRoot + [System.IO.Path]::DirectorySeparatorChar,
  45. [StringComparison]::OrdinalIgnoreCase) -and (Test-Path -LiteralPath $full)) {
  46. Remove-Item -LiteralPath $full -Force
  47. }
  48. }
  49. }
  50. }

Powered by TurnKey Linux.