Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何防止PowerShell将异常数据添加到$error_Powershell_Powershell 5.0_Powershell 7.0_Powershell 6.0 - Fatal编程技术网

如何防止PowerShell将异常数据添加到$error

如何防止PowerShell将异常数据添加到$error,powershell,powershell-5.0,powershell-7.0,powershell-6.0,Powershell,Powershell 5.0,Powershell 7.0,Powershell 6.0,我在函数中调用了很多REST调用。 我知道其中一些会失败,但这是意料之中的 我的问题是: 如何防止powershell向全局$error变量添加条目 foreach: $oldErrorActionPreference = $ErrorActionPreference $ErrorActionPreference = "Ignore" try { $response = Invoke-RestMethod -Uri $Uri -ea Ignore } catch {

我在函数中调用了很多REST调用。 我知道其中一些会失败,但这是意料之中的

我的问题是:

如何防止powershell向全局
$error
变量添加条目

foreach:

$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Ignore"
try {
    $response = Invoke-RestMethod -Uri $Uri -ea Ignore
} catch {
    Write-Verbose "$_"
} finally {
    $ErrorActionPreference = $oldErrorActionPreference
}

调用后,
$error
变量:


调用RestMethod
cmdlet始终会失败并出现终止错误,不能忽略该错误-它将始终在
$error
列表中结束


您可以清除它:
$Error.clear()
Invoke RestMethod
cmdlet总是失败并出现终止错误,不能忽略该错误-它将始终在
$Error
列表中结束


您可以清除它:
$Error.clear()

是否存储Powershell会话中发生的所有错误?如果用户查看最新的错误,然后在需要时缓慢地向上移动阵列,则会更加安全。
$error[0]
。您为什么还要这样做?您试图解决的问题是什么?@AnsgarWiechers我正在创建一组核心模块,这些模块本身必须不会泄漏到$global:error。异常是“预期的”,因此不是错误。我不喜欢我的代码已经“接受”的异常状态仍然泄漏到全局:错误。这将创建一个重载的$error,该错误同样无法用于跟踪和查找真正的“错误”
$error
是否存储Powershell会话中发生的所有错误?如果用户查看最新的错误,然后在需要时缓慢地向上移动阵列,则会更加安全。
$error[0]
。您为什么还要这样做?您试图解决的问题是什么?@AnsgarWiechers我正在创建一组核心模块,这些模块本身必须不会泄漏到$global:error。异常是“预期的”,因此不是错误。我不喜欢我的代码已经“接受”的异常状态仍然泄漏到全局:错误。这将创建一个重载的$error,该错误同样无法用于跟踪和查找该错误的真正“错误”源?@CasperLeonNielsen关于调用RestMethod始终抛出终止错误可以从这里推断:@CasperLeonNielsen PowerShell错误处理和什么以及何时进入$error变量解释(长度)此处:来源?@CasperLeonNielsen关于调用RestMethod始终抛出终止错误可从此处推断:@CasperLeonNielsen PowerShell错误处理以及$error变量的内容和时间解释(长度)如下: