Bulk Add Phone Numbers and Routin Policy

Use PowerShell to add phone numbers and routing policy to users.

# Connect to Microsoft Teams
# Connect-MicrosoftTeams

# Define the paths
$csvPath = Join-Path -Path $PSScriptRoot -ChildPath "usersp.csv"
$errorLogPath = Join-Path -Path $PSScriptRoot -ChildPath "error.log"

# Start transcript for logging
Start-Transcript -Path $errorLogPath -Append

# Read the CSV file
$users = Import-Csv $csvPath

# Voice Routing Policy name
$voiceRoutingPolicyName = "PolicyName"

# Initialize Arrays
$successReport = @()
$failedReport = @()

# Loop through each user in the CSV
foreach ($user in $users) {
    # Extract user details from the CSV
    $userEmail = $user.Email
    $phoneNumber = $user.PhoneNumber

    # Attempt to add phone number and assign voice routing policy
    try {
        # Add phone number for the user
        Set-CsPhoneNumberAssignment -Identity $userEmail -PhoneNumber $phoneNumber -PhoneNumberType DirectRouting -ErrorAction Stop

        # Assign common voice routing policy for the user
        Grant-CsOnlineVoiceRoutingPolicy -Identity $userEmail -PolicyName $voiceRoutingPolicyName -ErrorAction Stop

        # Record success
        $successReport += [PSCustomObject]@{
            Email = $userEmail
            Status = "Success"
        }

        Write-Host "Phone number and voice routing policy assigned for $($user.Email)"
    }
    catch {
        # Record failure
        $failedReport += [PSCustomObject]@{
            Email = $userEmail
            Status = "Failed"
            Error = $_.Exception.Message
        }

        Write-Host "Failed to process $($user.Email): $($_.Exception.Message)"
    }
}

# Stop transcript
Stop-Transcript

# Export success and failure reports to CSV files
$successReport | Export-Csv -Path (Join-Path -Path $PSScriptRoot -ChildPath "success_report.csv") -NoTypeInformation -Force
$failedReport | Export-Csv -Path (Join-Path -Path $PSScriptRoot -ChildPath "failed_report.csv") -NoTypeInformation -Force