PowerShell - 为整个脚本设置$ ErrorActionPreference

rom*_*the 14 error-handling powershell powershell-ise powershell-3.0

我今天第一次给PowerShell(v3.0)拍摄了一个镜头,并且对它的一些错误处理概念实现的奇怪方式感到非常沮丧.

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

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 $_
}
Run Code Online (Sandbox Code Playgroud)

(这只是一个片段)

显然,PowerShell的默认行为是不捕获非终止的错误.所以我按照不同的人的建议在我的脚本顶部添加了以下行:

$ErrorActionPreference = "Stop"
Run Code Online (Sandbox Code Playgroud)

在PowerShell ISE中执行此操作确实捕获了所有错误.但是,从终端执行以下命令仍然无法捕获我的错误.

来自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.
Run Code Online (Sandbox Code Playgroud)

从命令行:

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
Run Code Online (Sandbox Code Playgroud)

我不知道为什么Preference Variables的行为会根据它们的调用位置而有所不同,特别是因为ISE似乎执行完全相同的命令?

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

New-RegKey -ComputerName $PCName -Key $Key -Name $Value
Run Code Online (Sandbox Code Playgroud)

至:

New-RegKey -ComputerName $PCName -Key $Key -Name $Value -ErrorAction Stop
Run Code Online (Sandbox Code Playgroud)

使用这种方法,我能够从命令行和ISE中捕获错误,但我不想在每次调用的Cmdlet上指定错误行为,特别是因为捕获错误对于正常运行至关重要.代码.(另外,这种方法起作用的事实只会让我更加困惑)

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

另外,这是我的$ 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
Run Code Online (Sandbox Code Playgroud)

mjo*_*nor 11

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

$PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}
Run Code Online (Sandbox Code Playgroud)

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

$PSDefaultParameterValues = @{}
$PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}
Run Code Online (Sandbox Code Playgroud)

如果要继承父作用域中已有的内容,然后为本地作用域添加它:

 $PSDefaultParameterValues = $PSDefaultParameterValues.clone()
 $PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}
Run Code Online (Sandbox Code Playgroud)

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