我在网络上读到的所有内容都告诉我,写错误写入错误流,但是没有终止脚本,但是当运行下面的脚本时,它显然会终止脚本。
Write-Error "This should not terminate the script..."
Write-Information "... and it hasn't"产出如下:
D:\MyBitsAndBobs\writeerrortest.ps1 : This should not terminate the script...
At D:\MyBitsAndBobs\writeerrortest.ps1:3 char:1
+ Write-Error "This should not terminate the script..."
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,writeerrortest.ps1信息".它没有“显然从未被执行过。有人能对这种明显的意外行为有所了解吗?
发布于 2020-01-14 11:26:54
这与书面资料默认的工作方式有关。
根据文件:
$InformationPreference首选项变量值确定您提供的写入信息消息是否显示在脚本操作的预期点。由于此变量的默认值为SilentlyContinue,因此默认情况下不显示信息性消息。如果不想更改$InformationPreference的值,可以通过将InformationAction公共参数添加到命令中来重写其值。
因此,在您的情况下,要么将Write-Information更改为Write-Host,要么使用:
Write-Error "This should not terminate the script..."
Write-Information "... and it hasn't" -InformationAction Continue输出:
写错误“这不应该终止脚本.”写信息“.而且它还没有”-InformationAction继续:这不应该终止脚本.+ CategoryInfo : NotSpecified:(:)写错误,WriteErrorException + FullyQualifiedErrorId :NotSpecified.而且它还没有
发布于 2020-01-14 11:31:48
看看您的$ErrorActionPreference变量。默认情况下,它被设置为继续,实际上不会终止脚本。在你的情况下,它可能会停止。将其更改为$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue并再次运行脚本。有更多的设置比这两个,探索他们。
https://stackoverflow.com/questions/59732577
复制相似问题