我有一个正在执行的代码块,它设置了一个简单的 If/Else 块来捕获错误。我通常会使用 Try/Catch,但我正在使用的 Exchange 2010 PS 环境不允许我使用大多数 Try/Catch 功能(并且我无法以任何方式更新或更改它,因为它是客户的系统,他们不愿意)。
问题是,虽然当使用 -ErrorAction“Stop”设置 Add-DistributionGroupMember cmdlet 时代码将按预期运行,但它每次都会将错误输出到主机,这让客户(和我)感到烦恼,因为它实际上只是红噪声,因为所有可能的错误都通过详细的输出文件进行处理。
如果我将 cmdlet 设置为 -ErrorAction "SilentlyContinue",错误文本将被抑制,但错误不会按照我的预期添加到 $Error[0] 位置。 -ErrorAction“忽略”也是如此。该代码要求每次出现错误时都将 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"
}
}
您有两种追索选项,您可以利用
-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"
}
}
}
}
Add-DistributionGroupMember 命令在指定时似乎未填充 ErrorVariable。 我发现没有办法避免不需要的输出,所以我用 Tristan 的技巧来处理错误,而不是通过异常处理(这绝对没问题,但不适合我自己的开发):
$Error.clear()
Add-DistributionGroupMember -Identity $mySite.MailingList -Member $emailAddress
if ($Error.count -eq 0) {
$script:logger["ldissue"] += "<li>Liste de diffusion $($mySite.MailingList) : La ressource <b>$($ADRecord.Name)</b> ($($ADRecord.SamAccountName) - $($BoondRecord.flags)) a été ajoutée à la liste</li>"
} else {
$script:logger["ldissue"] += "<li>Liste de diffusion $($mySite.MailingList) : La ressource <b>$($ADRecord.Name)</b> ($($ADRecord.SamAccountName) - $($BoondRecord.flags)) n'a pas pu être ajoutée à la liste (<i>$($Error[0])</i>)</li>"
}
以下技术将抑制 Add-Type 命令生成的红色错误消息文本,并且仍会填充 $Error 变量,但我不确定这是否适用于 Add-DistributionGroupMember 命令,我无法测试该命令眼下。这是在 PowerShell 5.1 中测试的。
$Error.Clear()
.{Add-Type -Path 'NonExisting.dll' -ErrorAction Stop} 2>$Null
$Error[0]
使用“-ErrorAction Continue”这不会抑制错误,但会确保脚本继续并将错误放入 $Error 变量中。
您还可以使用 $Error.clear() 命令删除会话中任何存储的错误。