为什么Powershell的Write-Error cmdlet不能为我工作?我的输出看起来不像文档中的示例:
PS C:\> Write-Error "This is an error"
Write-Error "This is an error" : This is an error
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException我一直期望得到类似于Write-Warning的输出
PS H:\> Write-Warning "This is a warning"
WARNING: This is a warning从Write-Error和about_preference_variables文档中,我认为我不应该看到任何异常?
PS H:\> Get-Help About_Preference_Variables
$ErrorActionPreference
----------------------
...
PS> $erroractionpreference
Continue # Display the value of the preference.
PS> write-error "Hello, World"
# Generate a non-terminating error.
write-error "Hello, World" : Hello, World
# The error message is displayed and
execution continues.
PS> write-error "Hello, World" -ErrorAction:SilentlyContinue
# Use the ErrorAction parameter with a
value of "SilentlyContinue".
PS>
# The error message is not displayed and
execution continues.发布于 2011-04-14 03:53:32
要获得类似于write-warning的输出,您可以这样做:
$Host.UI.WriteErrorLine("This is an error")(克里斯·西尔斯的道具)
发布于 2011-03-11 08:10:25
为什么你不认为它起作用了?请记住,当您执行throw 'Access denied.'时,PowerShell区分了像上面这样的非终止错误和像您得到的终止错误。非终止性错误被写入标准错误并记录在$error集合中,但它们不会停止脚本的处理。当您正在处理(例如删除或复制)一堆文件时,此功能非常方便。您希望知道哪些文件无法处理,但又不希望整个操作在出现错误的第一个文件上停止。
PowerShell还为您提供了将非终止错误“转换”为终止错误的选项,例如
Remove-Item c:\file-doesnt-exist -ErrorAction Stop; "Did I get here"请注意,在这种情况下,执行将停止,并且不会打印出末尾的字符串。在不使用-ErrorAction Stop的情况下尝试一下,你会看到错误,但你也会看到字符串"Did get here“。
如果你想控制目录信息,你可以使用-Category参数,如下所示:
PS> write-error "foo" -Category 'InvalidResult'
write-error "foo" -Category 'InvalidResult' : foo
+ CategoryInfo : InvalidResult: (:) [Write-Error], WriteErrorExce..
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException但是WriteErrorException是这个cmdlet引发错误的机制(我认为)。有一个Exception参数,但我使用它不是很幸运。
发布于 2011-03-11 05:42:44
我相信您看到的是该cmdlet的预期输出。你输入的是“拒绝访问”参数,并将其输出到主机,最有可能输出到设计的错误流。您可以确认它正在输出到$Error变量,并且应该用您刚刚插入的错误填充它。
即
PS C:\> $error.Clear()
PS C:\> Write-Error "access denied"
Write-Error "access denied" : access denied
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
PS C:\> $error
Write-Error "access denied" : access denied
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException我可以看出哪里会令人困惑,也许MSFT应该为了清楚起见将示例错误从“访问被拒绝”更改为"Foobar“。
编辑以解决进一步的问题: Write-Error的默认errorAction是"continue",因此要使其行为类似于Write-Warning,您必须添加-ErrorAction SilentlyContinue。考虑以下示例:
PS E:\> $error.clear()
PS E:\> Write-Error 'test'
Write-Error 'test' : test
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
PS E:\> Write-Error 'test2' -ErrorAction silentlycontinue
PS E:\> $error[1]
Write-Error 'test' : test
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
PS E:\> $error[0]
Write-Error 'test2' -ErrorAction silentlycontinue : test2
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptionhttps://stackoverflow.com/questions/5265890
复制相似问题