Share via

Catch MS Teams errors in powershell

Anonymous
2022-12-05T16:24:28+00:00

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

Microsoft Teams | Microsoft Teams for business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2023-02-07T20:54:02+00:00

    Ouch..

    for reference, i ended up doing a loop to just keep trying as below, i didnt care about a log file, as the do until will just keep trying until the test is no longer null, then continues... I do end up with a lot of errors down scrolling down the page, but it works (so far) and iv got a lot of these accounts to create...

        do {$test = get-csphonenumberassignment -AssignedPstnTargetId $UPN 

            Write-Host "Attempting to set users LineURI"

            Set-CsPhoneNumberAssignment -Identity $UPN -PhoneNumber $LineURI -PhoneNumberType DirectRouting

            Set-CsPhoneNumberAssignment -Identity $UPN -EnterpriseVoiceEnabled $true

            write-host "No Licence Found, waiting 10 seconds to try again" -ForegroundColor DarkRed

            start-sleep 10

        } until ( $null -ne $test )

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-02-07T20:45:42+00:00

    Thank you for replying Phillip, however as Aaron has pointed out, non variation of try-catch worked. For some reason these cmdlets can't be captured. I ended up giving up storing the specific errors in a log file. I just stored all failed users by GUID and DisplayName and went through the list one by one to solve license, duplications or any issues that arose.

    Thanks though.

    Uriel R

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-02-07T20:27:50+00:00

    Doesnt work, for some reason the Teams powershell cmdlet errors dont get caught by the try catch method... tried several variations, not a clue why !

    Was this answer helpful?

    0 comments No comments
  4. Emmanuel Santana 39,025 Reputation points Independent Advisor
    2022-12-05T16:56:24+00:00

    Hello Uriel,

    To catch and store an error thrown by the Microsoft Teams module, you can use a try-catch block to handle the error. In the catch block, you can use the Add-Content cmdlet to append the error message to a log file. Here's an example of how you can do this:

    # 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
        try {
            Set-CsPhoneNumberAssignment -Identity $User.user -PhoneNumber $User.didNumber -PhoneNumberType DirectRouting
        } catch {
            # Log errors
            "Error: $($_.Exception.Message)" | Add-Content $logfile
            continue
        }
    
        $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
            "Unspecified Error: $($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
    }
    

    In the example above, I added a try-catch block around the Set-CsPhoneNumberAssignment cmdlet. Any errors that are thrown by this cmdlet will be caught and logged in the catch block. I also added some additional error handling to log any unspecified errors that may occur.

    Was this answer helpful?

    0 comments No comments