PowerShell的 - 如何检查成绩单运行?(powershell - how to check

2019-07-28 23:42发布

我得到这个消息,每次我的脚本不正确结束和停止成绩单不执行:

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

是否有可能以检查是否成绩单正在运行,如果有,则在脚本的开始停止吗? 或如何可靠地在年底停止吗? 谢谢

Answer 1:

怎么样一个空try-catch在你的PowerShell脚本的开头块以阻止转录?

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


Answer 2:

不会像这样的工作?

try
{
    Start-Transcript
    # Do my stuff
}

finally
{
    Stop-Transcript
}

从文档:finally块语句无论try块是否遇到终止错误运行。 的Windows PowerShell脚本终止或当前块超出范围之前之前运行finally块。 Finally块运行,即使您使用CTRL + C停止脚本。 Finally块也运行如果退出的关键字从catch块停止脚本。



Answer 3:

尝试可用的测试,录制功能在这里: http://poshcode.org/1500

 if(Test-Transcribing) {Stop-Transcript}


Answer 4:

try { 
     Start-Transcript -path $myOutLog

} catch { 

       stop-transcript

       Start-Transcript -path $myOutLog 
} 


文章来源: powershell - how to check if transcript is running?