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

时间:2017-12-11 21:03:49

标签: powershell error-handling exchange-server

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

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

如果我将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"
        }
    }

2 个答案:

答案 0 :(得分:1)

您有两种追索方式,您可以使用-ErrorVariable公共参数或Try/Catch块与特定错误进行互动。

ErrorVariable

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"
        }
    }
}

的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"
            }
        }
    }
}

答案 1 :(得分:-1)

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

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