Last active
August 19, 2024 19:37
-
-
Save bentman/bb6c128d4f6a0fa94000ba19398052f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Wait-TranscriptEnd { | |
<## | |
.SYNOPSIS | |
Waits for the creation and completion of a PowerShell transcript file (helpful in race conditions). | |
.DESCRIPTION | |
This function polls for the existence of a transcript file and then monitors its content until a specific end pattern is detected. | |
.PARAMETER transcriptPath | |
The path to the transcript file. Required. | |
.PARAMETER pollSeconds | |
The polling interval in seconds. Default is 10 seconds. | |
.PARAMETER maxWaitCount | |
The maximum number of polling intervals to wait before issuing a warning. Default is 45. | |
.PARAMETER endPattern | |
The regex pattern to detect the end of the transcript. Default is "PowerShell transcript end\s*End time: \d{14}\s*\*{22}". | |
.EXAMPLE | |
Wait-TranscriptEnd -transcriptPath "C:\path\to\your\transcript.log" -pollSeconds 10 -maxWaitCount 45 | |
#> | |
param ( | |
[Parameter(Mandatory = $true)] [string]$transcriptPath, | |
[Parameter()] [int]$pollSeconds = 10, # Polling interval in seconds | |
[Parameter()] [int]$maxWaitCount = 45, # Max number of polls to wait before warning | |
[Parameter()] [string]$endPattern = "PowerShell transcript end\s*End time: \d{14}\s*\*{22}" # This could be any pattern to poll | |
) | |
# Initialize wait counters | |
$waitCountPath = 0; $waitCountEnd = 0 | |
$transcriptEnded = $false | |
# Signal polling initiated | |
Write-Output "`n****** Polling transcript & end at $transcriptPath..." | |
# Wait until the transcript file exists | |
do { Start-Sleep -Seconds $pollSeconds; $waitCountPath++ } | |
while (-not (Test-Path $transcriptPath) -and ($waitCountPath -lt $maxWaitCount)) | |
# Check if the transcript file was created within the expected time | |
if (-not (Test-Path $transcriptPath)) { Write-Error "*** Transcript at $transcriptPath not created within expected time of $([math]::Round($pollSeconds * $maxWaitCount / 60, 2)) minutes."; return } | |
Write-Output "*** Transcript at $transcriptPath detected!" | |
# Function to check if the transcript end pattern is found | |
function Test-TranscriptEnd { param ( [string]$filePath, [string]$pattern ) $content = Get-Content -Path $filePath -Raw; return $content -match $pattern } | |
# Monitor the content until the end of the transcript is detected | |
do { | |
Start-Sleep -Seconds $pollSeconds; $waitCountEnd++ | |
if (Test-TranscriptEnd -filePath $transcriptPath -pattern $endPattern) { $transcriptEnded = $true; Write-Output "*** Transcript End in $transcriptPath detected!" } | |
} | |
while (-not $transcriptEnded -and ($waitCountEnd -lt $maxWaitCount)) | |
# Check if the end of the transcript was detected within the expected time | |
if (-not $transcriptEnded) { Write-Error "*** Transcript End in $transcriptPath not detected within expected time of $([math]::Round($pollSeconds * $maxWaitCount / 60, 2)) minutes." } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment