選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

56 行
3.1KB

  1. $ErrorActionPreference = 'Stop'
  2. $root = Split-Path -Parent $PSScriptRoot
  3. $tempRoot = [System.IO.Path]::GetFullPath((Join-Path $root 'temp'))
  4. $work = Join-Path $tempRoot ('hta-hidden-' + [guid]::NewGuid().ToString('N'))
  5. $hta = Join-Path $work 'MassMailer.hta'
  6. $started = $null
  7. try {
  8. $null = New-Item -ItemType Directory -Path $work -Force
  9. Copy-Item -LiteralPath (Join-Path $root 'tools') -Destination (Join-Path $work 'tools') -Recurse
  10. Copy-Item -LiteralPath (Join-Path $root 'src') -Destination (Join-Path $work 'src') -Recurse
  11. $null = New-Item -ItemType Directory -Path (Join-Path $work 'templates') -Force
  12. $app = Get-Content -LiteralPath (Join-Path $root 'MassMailer.hta') -Raw
  13. $app.Replace('APPLICATIONNAME="MassMailer"', 'APPLICATIONNAME="MassMailerSmoke"') |
  14. Set-Content -LiteralPath $hta -Encoding UTF8
  15. $dbPath = Join-Path $work 'data\test.mdb'
  16. $null = New-Item -ItemType Directory -Path (Split-Path -Parent $dbPath) -Force
  17. $settings = Get-Content -LiteralPath (Join-Path $root 'config.example.ini') -Raw
  18. $settings.Replace('DB_PATH=data/MassMailer.mdb', ('DB_PATH=' + $dbPath)) |
  19. Set-Content -LiteralPath (Join-Path $work 'config.ini') -Encoding UTF8
  20. $cscript = Join-Path $env:SystemRoot 'SysWOW64\cscript.exe'
  21. & $cscript //nologo (Join-Path $work 'tools\init-db.vbs') (Join-Path $work 'config.ini')
  22. if ($LASTEXITCODE -ne 0) { throw 'Isolated MDB initialization failed' }
  23. $mshta = Join-Path $env:SystemRoot 'SysWOW64\mshta.exe'
  24. $started = Start-Process -FilePath $mshta -ArgumentList ('"' + $hta + '"') -PassThru -WindowStyle Hidden
  25. $session = $null
  26. for ($wait = 0; $wait -lt 30; $wait++) {
  27. Start-Sleep -Seconds 1
  28. $matches = @(Get-CimInstance Win32_Process -Filter "Name='mshta.exe'" |
  29. Where-Object { $_.CommandLine -and $_.CommandLine.IndexOf($hta,
  30. [StringComparison]::OrdinalIgnoreCase) -ge 0 })
  31. if ($matches.Count -eq 1) {
  32. $session = Join-Path $work ('temp\session-' + $matches[0].ProcessId + '.txt')
  33. if (Test-Path -LiteralPath $session -PathType Leaf) { break }
  34. }
  35. }
  36. if ($matches.Count -ne 1 -or -not (Test-Path -LiteralPath $session -PathType Leaf)) {
  37. throw 'Isolated HTA did not initialize its hidden dispatcher session'
  38. }
  39. Start-Sleep -Seconds 3
  40. Write-Output 'Isolated HTA startup passed while the main app remained open'
  41. }
  42. finally {
  43. $matches = @(Get-CimInstance Win32_Process -Filter "Name='mshta.exe'" |
  44. Where-Object { $_.CommandLine -and $_.CommandLine.IndexOf($hta,
  45. [StringComparison]::OrdinalIgnoreCase) -ge 0 })
  46. foreach ($process in $matches) {
  47. Stop-Process -Id $process.ProcessId -Force -ErrorAction SilentlyContinue
  48. }
  49. $resolvedWork = [System.IO.Path]::GetFullPath($work)
  50. if (-not $resolvedWork.StartsWith($tempRoot + [System.IO.Path]::DirectorySeparatorChar,
  51. [StringComparison]::OrdinalIgnoreCase)) { throw 'Unsafe isolated test cleanup path' }
  52. if (Test-Path -LiteralPath $resolvedWork) {
  53. Remove-Item -LiteralPath $resolvedWork -Recurse -Force
  54. }
  55. }

Powered by TurnKey Linux.