每当我的脚本没有正确结束并且没有执行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在脚本开始时停止?或者如何在结束时可靠地阻止它?谢谢
答案 0 :(得分:9)
你的powershell脚本开头的空try-catch
块如何停止转录呢?
try{
stop-transcript|out-null
}
catch [System.InvalidOperationException]{}
答案 1 :(得分:6)
不会有这样的工作吗?
try
{
Start-Transcript
# Do my stuff
}
finally
{
Stop-Transcript
}
从文档中: 无论Try块是否遇到终止错误,Finally块语句都会运行。 Windows PowerShell在脚本终止之前或当前块超出范围之前运行Finally块。即使您使用 CTRL + C 来停止脚本,也会运行Finally块。如果Exit关键字在Catch块中停止脚本,则也会运行Finally块。
答案 2 :(得分:2)
尝试此处提供的测试转录功能:http://poshcode.org/1500
if(Test-Transcribing) {Stop-Transcript}
答案 3 :(得分:1)
try {
Start-Transcript -path $myOutLog
} catch {
stop-transcript
Start-Transcript -path $myOutLog
}
答案 4 :(得分:0)
$erroractionpreference="Silentlycontinue"
start-transcript c:\file.txt
$transcriptrunning=$?
$erroractionpreference="Continue"
if($transcriptrunning) {stop-transcript}