PowerShell - 为整个脚本设置$ ErrorActionPreference [英] PowerShell - Setting $ErrorActionPreference for the entire script

查看:1759
本文介绍了PowerShell - 为整个脚本设置$ ErrorActionPreference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天一直在给PowerShell(v3.0)一个镜头,并以非常沮丧的方式实现了一些错误处理概念。

I've been giving PowerShell (v3.0) a shot for the first time today, and became enormously frustrated with the strange way some of its error-handling concepts are implemented.

我写了以下代码(使用远程注册表PowerShell模块)

I wrote the following piece of code (using the Remote Registry PowerShell Module)

try
{
    New-RegKey -ComputerName $PCName -Key $Key -Name $Value
    Write-Host -fore Green ($Key + ": created")
}
catch
{
    Write-Host -fore Red "Unable to create RegKey: " $Key
    Write-Host -fore Red $_
}

(这只是一个代码段)

显然,PowerShell的默认行为是不捕获不终止的错误。所以我按照不同的人的推荐,在顶部的op脚本中添加了以下行:

Apparently the default behavior of PowerShell is to NOT catch errors which are non-terminating. So I added the following line at the top op my script, as recommended by various people:

$ErrorActionPreference = "Stop"

在PowerShell ISE中执行此操作确实捕获了所有错误。然而,从终端执行命令仍然没有抓住我的错误。

Executing this in the PowerShell ISE did indeed catch all errors. However, execution following command from the terminal still does not catch my errors.

从ISE:

PS C:\windows\system32> C:\Data\Scripts\PowerShell\Error.ps1
Errorhandling:  Stop
SOFTWARE\MySoftware does not exist. Attempting to create
Unable to create RegKey:  SOFTWARE\MySoftware
Key 'SOFTWARE\MySoftware' doesn't exist.

从命令行:

PS C:\Data\Scripts\PowerShell> .\Error.ps1
Errorhandling:  Stop
SOFTWARE\MySoftware does not exist. Attempting to create
New-RegKey : Key 'SOFTWARE\MySoftware' doesn't exist.
At C:\Data\Scripts\PowerShell\Error.ps1:17 char:13
+             New-RegKey -ComputerName $PCName -Key $Key -Name $Value
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,New-RegKey

SOFTWARE\MySoftware: created

我不知道为什么首选项变量的行为有所不同,取决于特别是因为ISE似乎执行完全相同的命令?

I have no idea why the behavior of the Preference Variables behaves differently depending on where they are called from, especially since the ISE seems to execute the exact same command?

根据其他反馈,我更改了以下行:

Based on other feedback, I changed the following line:

New-RegKey -ComputerName $PCName -Key $Key -Name $Value

To:

New-RegKey -ComputerName $PCName -Key $Key -Name $Value -ErrorAction Stop

使用此方法,我能够从命令行和ISE中捕获错误,但是我不想在每个Cmdlet I调用时指定错误行为,特别是因为捕获错误对于代码的正常运行。 (此外,这种方法只能使我更加困惑)。

Using this method, I was able to trap errors from both the command-line and the ISE, but I don't want to specify the error behaviour on each Cmdlet I call, especially because the catching of errors is essential to the proper functioning of the code. (Plus, the fact that this method DOES work only serves to confuse me even more)

在整个范围内定义错误处理行为的正确方法是什么脚本和/或模块?

What is the proper way of defining error-handling behavior for the scope of an entire script and/or module?

另外,这里是我的$ PSVersionTable:

Also, here's my $PSVersionTable:

PS C:\Data\Scripts\PowerShell> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      3.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.18408
BuildVersion                   6.2.9200.16481
PSCompatibleVersions           {1.0, 2.0, 3.0}
PSRemotingProtocolVersion      2.2


推荐答案

由于您运行V3,您还可以选择使用$ PSDefaultParameterValues:

Since you're running V3, you also have the option of using $PSDefaultParameterValues:

$PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}

通常这将在全局范围内更改它。如果要将其隔离到本地或脚本范围,可以首先在本地范围内初始化一个新的:

Normally that will change it in the global scope. If you want to isolate it to just the local or script scope, you can initialize a new one in the local scope first:

$PSDefaultParameterValues = @{}
$PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}

如果您要继承父范围中已有的内容,然后将其添加到本地范围内:

If you want to inherit what's already in the parent scope and then add to it for the local scope:

 $PSDefaultParameterValues = $PSDefaultParameterValues.clone()
 $PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}

要为所有cmdlet设置默认的ErrorAction,而不仅仅是New-RegKey,请指定'*:ErrorAction'而不是<$上面的代码中的c $ c>'New-RegKey:ErrorAction'。

To set the default ErrorAction for all cmdlets, not just New-RegKey, specify '*:ErrorAction' instead of 'New-RegKey:ErrorAction' in the code above.

这篇关于PowerShell - 为整个脚本设置$ ErrorActionPreference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆