Powershell - how to check if a transcript works?

I get this message every time my script does not end properly and stop-transcript is not executed:

Start-Transcript : Transcription has already been started. Use the stop-transcr ipt command to stop transcription. At C:\ps\003desifrovanie.ps1:4 char:17 + start-transcript <<<< -path c:\_LOG\sfrbdesifrovanie.log -append + CategoryInfo : NotSpecified: (:) [Start-Transcript], InvalidOpe rationException + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Power Shell.Commands.StartTranscriptCommand 

Is it possible to check if the transcript is working and stop it with if-then at the beginning of the script? Or how to reliably stop him at the end? Thanks you

+7
source share
4 answers

How about an empty try-catch at the beginning of your powershell script to stop decryption?

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

Wouldn’t it be like this job?

 try { Start-Transcript # Do my stuff } finally { Stop-Transcript } 

From the documentation: The statements of the finally block are executed regardless of whether the Try block encounters a final error. Windows PowerShell runs the finally block before the script completes or before the current block goes out of scope. The finally block works even if you use CTRL + C to stop the script. The finally block is also executed if the Exit keyword stops the script from the Catch block.

+6
source

Try using the test transcription function: http://poshcode.org/1500

  if(Test-Transcribing) {Stop-Transcript} 
+2
source
 try { Start-Transcript -path $myOutLog } catch { stop-transcript Start-Transcript -path $myOutLog } 
+1
source

All Articles