为什么在指定-ErrorAction SilentlyContinue时仍会显示错误?

时间:2012-06-25 22:02:25

标签: powershell

PS C:\Users\ad_ctjares> Stop-Transcript -ErrorAction silentlycontinue
Transcription has not been started. Use the start-transcript command to start transcription.
Stop-Transcript : An error occurred stopping transcription: The console host is not currently transcribing.
At line:1 char:16
+ Stop-Transcript <<<<  -ErrorAction silentlycontinue
    + CategoryInfo          : InvalidOperation: (:) [Stop-Transcript], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.StopTranscriptCommand

代码说明了一切。

2 个答案:

答案 0 :(得分:11)

ErrorAction普遍存在的参数可用于使用参数值SilentlyContinue来消除非终止错误,并且可以使用参数值{{1来将非终止错误转换为终止错误}}。但它无法帮助您忽略终止错误,在这种情况下,Stop-Transcript会引发终止错误。如果你想忽略,请使用try / catch例如:

Stop

答案 1 :(得分:1)

您可以使用Trap {Continue} Stop-Transcript来避免任何错误。

相关问题