powershell - how to check if transcript is running?

荒凉一梦 提交于 2019-12-21 07:33:58

问题


I get this message everytime my script doesn't 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 transcript is running and stop it with if-then at start of the script? Or how to reliably stop it at the end? Thank you


回答1:


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]{}



回答2:


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.




回答3:


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

 if(Test-Transcribing) {Stop-Transcript}



回答4:


try { 
     Start-Transcript -path $myOutLog

} catch { 

       stop-transcript

       Start-Transcript -path $myOutLog 
} 


来源:https://stackoverflow.com/questions/10170237/powershell-how-to-check-if-transcript-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!