添加到$Error变量时抑制Powershell错误

添加到$Error变量时抑制Powershell错误,powershell,error-handling,exchange-server,Powershell,Error Handling,Exchange Server,我有一个正在执行的代码块,它被设置为一个简单的If/Else块来捕获错误。我通常会使用Try/Catch,但我工作的Exchange 2010 PS环境不允许我使用大多数Try/Catch功能(而且我无法以任何方式更新或更改它,因为这是客户的系统,他们不愿意) 问题在于,当Add-DistributionGroupMember cmdlet设置为-ErrorAction“Stop”时,代码将按预期运行,但每次都会将错误输出到主机,这会让客户(和我)感到恼火,因为这实际上只是一个红色噪音,因为所有

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

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

如果我将cmdlet设置为-ErrorAction“SilentlyContinue”,则错误文本将被抑制,但错误不会像我预期的那样添加到$error[0]位置。错误操作“忽略”也是如此。代码要求每次出现错误时都将错误添加到$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"
        }
    }
使用“-ErrorAction Continue”这不会抑制错误,但会确保脚本继续,并将错误放入$error变量中


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

您有两个追索选项,可以使用
-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"
        }
    }
}

尝试/抓住
如果我将cmdlet设置为-ErrorAction“SilentlyContinue”,则错误文本将被抑制,但错误不会像我预期的那样添加到$error[0]位置。这同样适用于-ErrorAction“Ignore”。
-从他的问题中逐字复制。对不起,我读了标题,意识到OP希望抑制错误并捕获错误。我正在使用-Continue进行测试,它捕获了$error变量中的错误。环境是什么版本的Powershell?不用担心。特里斯坦:如果我只是想忽略错误,稍后再检查,那么你的答案就行了。不幸的是,这里的要求意味着我需要抑制所有难看的红色错误文本。2010年的产品可能会使用PowerShell 2.0。PowerShell 3.0于2012年推出。想想客户留下的所有安全漏洞。幸运的是,所有这些工作都是将它们转移到Exchange Online中,以便它们摆脱所有这些旧服务器。我当然没有忘记这个风险。这是一个有用的演示,展示了编写类似代码块的其他方法,但它并不能解决我的问题。我怀疑原因与我的powershell环境有关。当我尝试+MyError代码时,我得到一个错误:
一个在受限语言模式下无法引用的变量,或者正在引用一个数据节。
并且它在一个循环后停止执行,而不会进入开关块。我开始怀疑这些Exchange cmdlet有点问题。@CharlieKing您尝试过try/Catch方法吗?
$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"
            }
        }
    }
}