I have the following code that updates contact records in MS exchange. My goal is that if it fails to update the record, it goes into my catch block to log that there was an error. Instead, it's just returning the following in the log and moving on, not going to the catch block. I'm newer to powershell but i'm assuming the error msg is not a ps exception, and is reporting the error on the external cmdlet. Is there any way to check that the cmdlet encountered and error so I can log it/go down an error path?
snippet of code I'm running:
try{
$msxchangeInfo = Get-Contact | Where-Object {$_.WindowsEmailAddress -eq "$user_search_email"}
if ($msxchangeInfo)
{
write-host "$(Get-TimeStamp) $user_search_email found...."
$msxchangeInfo = Get-Contact -Filter "WindowsEmailAddress -eq '$($user_search_email)'" |
Select-Object -Property WindowsEmailAddress, Manager, FirstName, LastName, MobilePhone,
DisplayName, Company, Department, Identity, StreetAddress, StateOrProvince, PostalCode,
CountryOrRegion, City, Phone
$msxchangeInfoCountryOrRegion = $msxchangeInfo | Select -ExpandProperty CountryOrRegion
if($msxchangeInfoCountryOrRegion -ne $officeCountry -and $officeCountry -ne "")
{
/*CODE THAT IS RETURNING ERROR MESSAGE BUT NOT THROWING EXCEPTION!*/
Set-Contact -Identity $msxchangeInfoIdentity -CountryOrRegion $officeCountry}
}
}
catch
{
write-host("$(Get-TimeStamp) *****ERROR*****: ERROR ATTEMPTING TO CREATE NEW CONTACT RECORD:
$($displayName) . CHECK ERROR BELOW")
write-host($_.Exception|format-list -force)
write-host("$(Get-TimeStamp) Adding user to error ticket list")
$errMsg = $error[0].ToString() + $error[0].InvocationInfo.PositionMessage
$errMsg -replace ',','|'
}
However the code is never hitting the catch block. It is not throwing an error, it's just logging the cmdlet error message and continuing. How do I break it on the cmdlet error of set-contact?
This is the output in the log (as you can see, not logging my custom error message noted above):
Account Environment TenantId TenantDomain AccountType
------- ----------- -------- ------------ -----------
gggggggggggggg-gggggg-ggggggg-gg AzureCloud gggggggggggg-ggggg-gggg-gg TestTenant.com ServicePrincipal
Cannot process argument transformation on parameter 'CountryOrRegion'. Cannot convert value "Not Found" to type
"Microsoft.Exchange.Data.Directory.CountryInfo". Error: "The ISO-3166 2-letter country/region code or friendly name
"Not Found" does not represent a country/region."
+ CategoryInfo : InvalidData: (:) [Set-Contact], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Contact
+ PSComputerName : outlook.office365.com
You can force a specific call to a cmdlet to halt on error via the -ErrorAction
common parameter:
Set-Contact -Identity $msxchangeInfoIdentity -CountryOrRegion $officeCountry -ErrorAction Stop
You can also set this as the default behavior for any call in the current scope by modifying the $ErrorActionPreference
variable at the start of your script:
$ErrorActionPreference = 'Stop'
... or, alternatively use the $PSDefaultParameterValues
variable to make specific cmdlets in the current scope always take on this behavior on error:
$PSDefaultParameterValues['Set-Contact:ErrorAction'] = 'Stop'