Azure Function Apps - PowerShell 的 ErrorActionPreference

Azure Function Apps - PowerShell's ErrorActionPreference

默认情况下,环境的 $ErrorActionPreference 设置为“Continue”。当 cmdlet 抛出错误时,脚本将继续。

我希望它“停止”并被我的 try-catch 块捕获。在我的脚本中,我可以设置:

$ErrorActionPreference = "Stop"

如果我将值打印到屏幕上,我可以看到它现在设置为“Stop”而不是“Continue” '.但是,当我的 cmdlet 抛出错误时,它仍然会继续。它忽略了我的错误操作偏好并根据默认值运行。

任何人都可以阐明这一点吗?

所以,我不确定你在做什么,但对我来说它工作正常:

Write-Output "PowerShell Timer trigger function executed at:$(get-date)";

try {
    Get-Process 'hh' -erroraction stop
}
catch{
    Write-Output "caught"
    exit
}

Write-Output "never reached"

或者像这样:

Write-Output "PowerShell Timer trigger function executed at:$(get-date)";
$global:erroractionpreference = 1
try {
    Get-Process 'hh'
}
catch{
    Write-Output "caught"
    exit
}

Write-Output "i do not work"

两者打印相同(除了时间戳,ofc):

2017-02-02T19:56:35.916 PowerShell Timer trigger function executed at:02/02/2017 19:56:35

2017-02-02T19:56:35.933 caught

2017-02-02T19:56:35.933 Function completed (Success, Id=6097f1bf-06e1-4a1d-ba27-392607d9b33c)