【问题标题】:Suppress Powershell Errors while adding to $Error Variable在添加到 $Error 变量时抑制 Powershell 错误
【发布时间】:2018-05-25 11:22:54
【问题描述】:

我有一个代码块正在执行,它设置了一个简单的 If/Else 块来捕获错误。我通常会使用 Try/Catch,但我正在使用的 Exchange 2010 PS 环境不允许我使用大部分 Try/Catch 功能(而且我无法更新或以任何方式更改它,因为它是客户的系统,他们不愿意)。

问题是,当 Add-DistributionGroupMember cmdlet 设置为 -ErrorAction "Stop" 时,虽然代码将按预期运行,但它每次都会将错误输出到主机,这让客户(和我)很恼火,因为它是实际上只是红噪声,因为所有可能的错误都是通过详细的输出文件处理的。

如果我将 cmdlet 设置为 -ErrorAction "SilentlyContinue",则会抑制错误文本,但不会像我预期的那样将错误添加到 $Error[0] 位置。 -ErrorAction "Ignore" 也是如此。该代码要求每次出现错误时将Error添加到$Error变量中。

代码如下:

$ListMembershipsIn | % {

        $Alias = $_.Alias
        $Member = $_.Member

        Add-DistributionGroupMember -Identity $Alias -Member $Member -Confirm:$false -ErrorAction Stop


        if($Error[0] -match "The recipient"){
            Write-Host -ForegroundColor Yellow "Already a member"
            Add-Content -Path $OutputPath -Value "$($Alias),$($Member),Group already contains Member"
        }
        elseif($Error[0] -match "couldn't be found"){
            Write-Host -ForegroundColor Yellow "not found"
            Add-Content -Path $OutputPath -Value "Group does not exist or cannot be found,$($Alias),N/A"
        }
        elseif($Error[0] -match "couldn't find"){
            Write-Host -ForegroundColor Yellow "not found"
            Add-Content -Path $OutputPath -Value "Member does not exist or cannot be found,$($Alias),$($Member)"
        }
        elseif($Error[0] -match "There are Multiple"){
            Add-Content -Path $OuputPath -Value "Member name matches too many recipient - Add Member Manually,$($Alias),$($Member)"
        }
        else{
            Add-Content -Path $OutputPath -Value "Member Successfully Added to Group,$($Alias),$($Member)"
            Write-Host -ForegroundColor Green "Throw Flag here"
        }
    }

【问题讨论】:

  • 2010 年的产品可能正在使用 PowerShell 2.0。 PowerShell 3.0 出现在 Server 2012 中。想想客户留下的所有安全漏洞。 biztechmagazine.com/article/2017/01/…
  • 幸运的是,所有这些工作都是为了将​​它们转移到 Exchange Online 中,以便他们摆脱所有这些旧服务器。我当然不会失去风险。

标签: powershell error-handling exchange-server


【解决方案1】:

您有两个追索选项,您可以利用-ErrorVariable 通用参数或Try/Catch 块与特定错误进行交互。


错误变量

在与ErrorVariable 交互时,您可以在名称前加上+ 来附加额外的错误,就像$Error 自动变量一样,例如:-ErrorVariable '+MyError'

$ListMembershipsIn | ForEach-Object {
    $Alias = $_.Alias
    $Member = $_.Member
    Add-DistributionGroupMember -Identity $Alias -Member $Member -ErrorVariable 'MyError'

    ## No error = good, continue the next iteration of the loop
    If (-not $MyError)
    {
        Add-Content -Path $OutputPath -Value "Member Successfully Added to Group,$Alias,$Member"
        Write-Host -ForegroundColor Green "Throw Flag here"
        Continue
    }

    Switch -Regex ($MyError.Exception.Message)
    {
        'The recipient'
        {
            Write-Host -ForegroundColor Yellow "Already a member"
            Add-Content -Path $OutputPath -Value "$Alias,$Member,Group already contains Member"
        }
        "couldn't be found"
        {
            Write-Host -ForegroundColor Yellow "not found"
            Add-Content -Path $OutputPath -Value "Group does not exist or cannot be found,$Alias,N/A"
        }
        "couldn't find"
        {
            Write-Host -ForegroundColor Yellow "not found"
            Add-Content -Path $OutputPath -Value "Member does not exist or cannot be found,$Alias,$Member"
        }
        'There are Multiple'
        {
            Add-Content -Path $OuputPath -Value "Member name matches too many recipient - Add Member Manually,$Alias,$Member"
        }
    }
}

尝试/抓住

$ListMembershipsIn | ForEach-Object {
    $Alias = $_.Alias
    $Member = $_.Member

    Try
    {
        Add-DistributionGroupMember -Identity $Alias -Member $Member -ErrorAction 'Stop'

        ## No error thrown = successful processing
        Add-Content -Path $OutputPath -Value "Member Successfully Added to Group,$Alias,$Member"
        Write-Host -ForegroundColor Green "Throw Flag here"
    }
    Catch
    {
        Switch -Regex ($_.Exception.Message)
        {
            'The recipient'
            {
                Write-Host -ForegroundColor Yellow "Already a member"
                Add-Content -Path $OutputPath -Value "$Alias,$Member,Group already contains Member"
            }
            "couldn't be found"
            {
                Write-Host -ForegroundColor Yellow "not found"
                Add-Content -Path $OutputPath -Value "Group does not exist or cannot be found,$Alias,N/A"
            }
            "couldn't find"
            {
                Write-Host -ForegroundColor Yellow "not found"
                Add-Content -Path $OutputPath -Value "Member does not exist or cannot be found,$Alias,$Member"
            }
            'There are Multiple'
            {
                Add-Content -Path $OuputPath -Value "Member name matches too many recipient - Add Member Manually,$Alias,$Member"
            }
        }
    }
}

【讨论】:

  • 这是对编写类似代码块的其他方法的有用演示,但它不能解决我的问题。而且我怀疑这与我的powershell环境有关。当我尝试 +MyError 代码时,我得到一个错误:A variable that cannot be referenced in restricted language mode or a Data section is being referenced. 并且它在一个循环后停止执行,而不是进入 Switch 块。我开始怀疑这些 Exchange cmdlet 有点问题..
  • @CharlieKing 你试过 Try/Catch 方法了吗?
【解决方案2】:

使用“-ErrorAction Continue”这不会抑制错误,但会确保脚本继续运行并将错误放在 $Error 变量中。

您还可以使用 $Error.clear() 命令删除会话中存储的任何错误。

【讨论】:

  • If I set the cmdlet to -ErrorAction "SilentlyContinue" the error text is suppressed, but the Error is not added to the $Error[0] spot as I expected. The same is true of -ErrorAction "Ignore". - 从他的问题中复制过来..
  • 对不起,我读了标题并意识到 OP 希望抑制错误并捕获错误。我正在使用 -Continue 进行测试,它捕获了 $error 变量中的错误。环境是什么版本的Powershell?
  • 不用担心。如果我只是想忽略错误并稍后检查,特里斯坦你的答案会起作用。不幸的是,这里的要求意味着我需要隐藏所有那些难看的红色错误文本。
猜你喜欢
  • 2017-07-05
  • 2015-07-06
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 2016-06-18
  • 2015-12-20
  • 2018-11-07
相关资源
最近更新 更多