powershell - how to check if transcript is running?

Try the Test-Transcribing function available here: http://poshcode.org/1500

 if(Test-Transcribing) {Stop-Transcript}

Wouldn't something like this work?

try
{
    Start-Transcript
    # Do my stuff
}

finally
{
    Stop-Transcript
}

From the documentation: The Finally block statements run regardless of whether the Try block encounters a terminating error. Windows PowerShell runs the Finally block before the script terminates or before the current block goes out of scope. A Finally block runs even if you use CTRL+C to stop the script. A Finally block also runs if an Exit keyword stops the script from within a Catch block.


What about an empty try-catch block at the beginning of your powershell script to stop transcribing?

try{
  stop-transcript|out-null
}
catch [System.InvalidOperationException]{}

try { 
     Start-Transcript -path $myOutLog

} catch { 

       stop-transcript

       Start-Transcript -path $myOutLog 
}