close-pcl.ps1 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Close PCL once the method-1 block appears in its log, or at the deadline.
  2. # ASCII only. Windows PowerShell 5.1 compatible.
  3. param(
  4. [string]$PclDir,
  5. [int]$MaxSeconds = 35
  6. )
  7. $deadline = (Get-Date).AddSeconds($MaxSeconds)
  8. $proc = $null
  9. while ((Get-Date) -lt $deadline) {
  10. $proc = Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.ProcessName -like '*Plain*' -and $_.MainWindowHandle -ne 0 } | Select-Object -First 1
  11. if ($proc) { break }
  12. Start-Sleep -Milliseconds 300
  13. }
  14. if (-not $proc) { exit 1 }
  15. $logDir = Join-Path $PclDir 'PCL'
  16. while ((Get-Date) -lt $deadline) {
  17. $log = Get-ChildItem -Path $logDir -Filter 'Log*.txt' -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1
  18. if ($log) {
  19. $hit = Select-String -LiteralPath $log.FullName -Pattern 'LaunchAdvanceRun' -Encoding UTF8 -ErrorAction SilentlyContinue | Select-Object -First 1
  20. if ($hit) {
  21. Start-Sleep -Seconds 1
  22. break
  23. }
  24. }
  25. Start-Sleep -Milliseconds 300
  26. }
  27. try { $proc.CloseMainWindow() | Out-Null } catch { }
  28. if (-not $proc.WaitForExit(6000)) {
  29. try { Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue } catch { }
  30. }