2017-12-11 4 views
0

간단한 If/Else 블록을 사용하여 오류를 catch하도록 설정된 코드 블록을 실행하고 있습니다. Try/Catch를 사용하지만 일반적으로 Exchange 2010 PS 환경에서는 Try/Catch 기능을 대부분 사용할 수 없습니다. (고객의 문제이기 때문에 업데이트하거나 변경할 수 없습니다. 시스템이고 그들은 내키지 않는다).

Add-DistributionGroupMember cmdlet을 -ErrorAction "Stop"으로 설정하면 코드가 예상대로 작동하지만 매번 고객에게 오류가 출력되므로 문제가 발생합니다. 가능한 모든 오류가 상세한 출력 파일을 통해 처리되므로 이는 사실상 단순한 노이즈입니다.

-ErrorAction "SilentlyContinue"로 설정하면 오류 텍스트가 표시되지 않지만 예상대로 $ Error [0] 지점에 오류가 추가되지 않습니다. -ErrorAction "Ignore"도 마찬가지입니다. 이 코드에서는 오류가 발생할 때마다 오류를 $ 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" 
     } 
    } 
+0

2010 년 제품이 PowerShell 2.0을 사용하고있을 가능성이 있습니다. PowerShell 3.0은 Server 2012에 포함되었습니다. 고객이 열어두고있는 모든 보안 허점을 생각해보십시오. https://biztechmagazine.com/article/2017/01/how-guard-against-threats-microsoft-powershell-exploits – lit

+0

다행히도이 모든 작업은 Exchange Online으로 이전하여 모든 이전 서버를 제거합니다. . 위험은 확실히 나를 잃지 않습니다. –

답변

-1

사용 "계속 -ErrorAction"이 오류를 억제하지 않습니다하지만 스크립트가 계속 확인하고는 $ 오류 변수에 오류를 배치합니다.

$ Error.clear() 명령을 사용하여 세션에서 저장된 오류를 제거 할 수도 있습니다.

+0

'cmdlet을 -ErrorAction "SilentlyContinue"로 설정하면 오류 텍스트가 표시되지 않지만 예상대로 $ Error [0] 스팟에 오류가 추가되지 않습니다. - ErrAction "Ignore"에 대해서도 마찬가지입니다. - 그의 질문에서 말 그대로 그대로 복사됩니다. – TheIncorrigible1

+0

죄송합니다. 제목을 읽고 OP가 오류를 표시하고 오류가 표시되기를 원합니다. 나는 $ error 변수에서 오류를 포착 한 -Continue를 테스트했다. Powershell의 어떤 버전이 환경입니까? – Tristan

+0

걱정할 필요가 없습니다. Tristan은 오류를 무시하고 나중에 확인하기 만하면 대답이 효과가있었습니다. 불행히도 여기서 요구되는 것은 그 모든 못생긴 적색 오류 텍스트를 억제해야한다는 것을 의미합니다. –

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

시도/캐치

$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" 
      } 
     } 
    } 
} 
+0

이것은 유사한 코드 블록을 작성하는 다른 방법에 대한 유용한 데모이지만 내 문제를 해결하지는 못합니다. 그리고 필자는 왜 PowerShell 환경과 관련이 있는지 의심 스럽습니다.+ MyError 코드를 시도하면 오류가 발생합니다 : '제한된 언어 모드에서 참조 할 수없는 변수 또는 데이터 섹션이 참조되고 있습니다. ' 그리고 하나의 루프 이후에 실행을 중지합니다. 스위치 블록. 이 Exchange cmdlet이 약간 버그가있는 것 같습니다. –

+0

@CharlieKing Try/Catch 메서드를 사용해 보셨습니까? – TheIncorrigible1

관련 문제