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 ?