2017-10-11 179 views
0

在PowerShell腳本中,我想用Start-Transcript來編寫任何腳本的腳本。但是,有時腳本失敗了,並且腳本已經在運行。在這種情況下會引發錯誤,我找不到避免該錯誤的方法。 我能做些什麼來防故障啓動一個成績單?PowerShell在腳本已經運行時的腳本錯誤

如果轉錄本已經在運行,我想停止它並開始一個新的。

可以通過故意啓動兩個轉錄本來複制錯誤。請記住,在現實生活中,以前的腳本運行已經讓記錄打開了。

錯誤情況

PS C:\> Start-Transcript c:\test.txt # pretend that this is a transcript which was not stopped properly by a previous script 

Transcript started, output file is c:\test.txt 

PS C:\> Start-Transcript c:\test.txt 
Start-Transcript : Transcription cannot be started. 
At line:1 char:1 
+ Start-Transcript c:\test.txt 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException 
    + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand 

忽略與-ErrorAction SilentlyContinue不起作用!

PS C:\> Start-Transcript c:\test.txt # pretend that this is a transcript which was not stopped properly by a previous script 
Transcript started, output file is c:\test.txt 

PS C:\> Start-Transcript c:\test.txt -ErrorAction SilentlyContinue 
Start-Transcript : Transcription cannot be started. 
At line:1 char:1 
+ Start-Transcript c:\test.txt -ErrorAction SilentlyContinue 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException 
    + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand 

停止談話開始失敗之前,如果成績單不上PS 5.0(這是唯一一個我在有訪問運行

PS C:\> Start-Transcript c:\test.txt 
Transcript started, output file is c:\test.txt 

PS C:\> Stop-Transcript # pretend that this is a transcript which was not stopped properly by a previous script 
Transcript stopped, output file is C:\test.txt 

PS C:\> Stop-Transcript 
Stop-Transcript : An error occurred stopping transcription: The host is not currently transcribing. 
At line:1 char:1 
+ Stop-Transcript 
+ ~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [Stop-Transcript], PSInvalidOperationException 
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.StopTranscriptCommand 

回答

1

所以,我的所有測試時刻)顯示,再次使用相同的輸出文件名啓動轉錄本會拋出您提到的錯誤,但也會停止轉錄。手動試圖阻止它後會告訴我,The host is not currently transcribing

所以,我認爲你可以使用嘗試捕捉方法來取消錯誤:

Try{Start-transcript C:\transcript.txt -ErrorAction Stop}catch{Start-Transcript C:\transcript.txt}