Hi,
How can I catch an error thrown by the microsoft teams module and store it in a log file? For example this:

I have a script to bulky assign number to users, but sometimes their profiles take time to have the proper license activated or there's any other issue. Since I'll be adding a few thousands of users, I'd like to isolate these errors to solve them instead of scrolling up and down in my powershell window to find out which users failed. If it helps find the script I use below. The $_.Exception.Message doesn't work.
Store the data from UserList.csv in the $List variable
$List = Import-CSV "UserList_test.csv"
logfile
$logfile = "logteams.csv"
"User,Name,Fail Reason" | Add-Content $logfile
Count success
$success = 0
Count failures
$fails = 0
Loop through users in the CSV
ForEach ($User in $List) {
Assign number
Set-CsPhoneNumberAssignment -Identity $User.user -PhoneNumber $User.didNumber -PhoneNumberType DirectRouting
$check = Get-CsPhoneNumberAssignment -AssignedPstnTargetId $User.user
if ($check -ne $null) {
# If the number assignment works, I also add the user in a bunch of policies
Write-Host DONE for $User.displayName with number $User.didNumber -foregroundcolor green -BackgroundColor Black
$success++
} else {
# Unspecified Error - Log errors
"$($User.user),$($User.displayName),**$($\_.Exception.Message)**" | Add-Content $logfile
$fails++
}
}
Write-Host $success USERS DEPLOYED -ForegroundColor Green -BackgroundColor Black
if ($fails -gt 0) {
Write-Host $fails USERS FAILED -ForegroundColor red -BackgroundColor Black
Write-Host Check log file created in $logfile -ForegroundColor red -BackgroundColor Black
}
THANK YOU IN ADVANCE FOR YOUR HELP