0

This code writes errors in Catch block and stops executing

$Result=""   
$Results=@() 


    $Customers = Get-PartnerCustomer
Try{
    $Customers.ForEach({
                $CustId = $_.CustomerId
                $CustName =$_.Name
                $CustomerUser = Get-PartnerCustomerUser -CustomerId $CustId | Where-Object {
                $_.DisplayName -like 'name'
                } | ForEach-Object {
                $_.DisplayName
                #$_.UserPrincipalName
              }
              $Result=@{'Customer Name'=$CustName;'User Name'=$CustomerUser}
              $Results+= New-Object PSObject -Property $Result
                          
            })
    }
Catch{
      Write-Warning "Caught an exception:"
      Write-Warning "Exception Type: $($_.Exception.GetType().FullName)"
      Write-Warning "Exception Message: $($_.Exception.Message) - Tenant:$CustName"
}

When adding $ErrorActionPreference="Continue" at the top script continues after error but writes generic error message

Get-PartnerCustomerUser : Access denied.
At line:8 char:33
+ ...   $CustomerUser = Get-PartnerCustomerUser -CustomerId $CustId | Where ...
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-PartnerCustomerUser], PartnerException
    + FullyQualifiedErrorId : Microsoft.Store.PartnerCenter.PowerShell.Commands.GetPartnerCustomerUser

With $ErrorActionPreference="SilentlyContinue" it doesn't output any errors.

Is it possible to show my custom error message and not exiting from loop on first error ?

1 Answer 1

2

Solved, had to add Try/Catch inside the ForEach

$ErrorActionPreference="Stop"

    $Customers = Get-PartnerCustomer

    $Customers.ForEach({
    Try{
                $CustId = $_.CustomerId
                $CustName =$_.Name
                $CustomerUser = Get-PartnerCustomerUser -CustomerId $CustId | Where-Object {
                $_.DisplayName -like 'Name'
                } | ForEach-Object {
                $_.DisplayName
                #$_.UserPrincipalName
              }
              $Result=@{'Customer Name'=$CustName;'User Name'=$CustomerUser}
              $Results+= New-Object PSObject -Property $Result
                                        
            }
            
            Catch{
      Write-Warning "Caught an exception:"
      Write-Warning "Exception Type: $($_.Exception.GetType().FullName)"
      Write-Warning "Exception Message: $($_.Exception.Message) - Tenant:$CustName"
     }
   }
  )
  
Sign up to request clarification or add additional context in comments.

2 Comments

You can also reference $Error which holds all errors. Something like $Error | Select Exception should return the actual error type. Based off your first question
Thanks, code i posted gives me fine error output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.