Fix the issue that PS only stops transcription when all runspaces are closed (#3896)

Only stop transcription when all runspaces are closed
This commit is contained in:
Chunqing Chen
2017-06-13 15:44:06 -07:00
committed by Travis Plunk
parent 6bb12c9df5
commit 65f96e0298
2 changed files with 44 additions and 5 deletions
@@ -106,4 +106,34 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" {
$expectedError = "CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand"
ValidateTranscription -scriptToExecute $script -outputFilePath $null -expectedError $expectedError
}
It "Transcription should remain active if other runspace in the host get closed" {
try{
$ps = [powershell]::Create()
$ps.addscript("Start-Transcript -path $transcriptFilePath").Invoke()
$ps.addscript('$rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace()').Invoke()
$ps.addscript('$rs.open()').Invoke()
$ps.addscript('$rs.Dispose()').Invoke()
$ps.addscript('Write-Host "After Dispose"').Invoke()
$ps.addscript("Stop-Transcript").Invoke()
} finally {
if ($ps -ne $null) {
$ps.Dispose()
}
}
Test-Path $transcriptFilePath | Should be $true
$transcriptFilePath | Should contain "After Dispose"
}
It "Transcription should be closed if the only runspace gets closed" {
$powerShellPath = [System.Diagnostics.Process]::GetCurrentProcess().Path
$powerShellCommand = $powerShellPath + ' "start-transcript $transcriptFilePath; Write-Host ''Before Dispose'';"'
Invoke-Expression $powerShellCommand
Test-Path $transcriptFilePath | Should be $true
$transcriptFilePath | Should contain "Before Dispose"
$transcriptFilePath | Should contain "Windows PowerShell transcript end"
}
}