14

每次我的脚本没有正确结束并且没有执行 stop-transcript 时,我都会收到此消息:

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

是否可以检查脚本是否正在运行并在脚本开始时使用 if-then 停止它?或者如何在最后可靠地阻止它?谢谢

4

5 回答 5

15

try-catch在你的 powershell 脚本开头有一个空块来停止转录呢?

try{
  stop-transcript|out-null
}
catch [System.InvalidOperationException]{}
于 2012-04-16T08:02:05.140 回答
10

像这样的东西不会起作用吗?

try
{
    Start-Transcript
    # Do my stuff
}

finally
{
    Stop-Transcript
}

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

于 2012-04-16T08:10:10.537 回答
2

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

 if(Test-Transcribing) {Stop-Transcript}
于 2012-04-16T08:05:22.953 回答
1
try { 
     Start-Transcript -path $myOutLog

} catch { 

       stop-transcript

       Start-Transcript -path $myOutLog 
} 
于 2014-12-01T18:11:45.913 回答
-1
$erroractionpreference="Silentlycontinue"
start-transcript c:\file.txt
$transcriptrunning=$?
$erroractionpreference="Continue"
if($transcriptrunning) {stop-transcript}
于 2020-02-12T17:58:59.537 回答