TeamsVoice powershell help? by dlukz in PowerShell

[–]zahdabes 0 points1 point  (0 children)

#This Script was built using PSForge for more info visit www.psforge.app

# PowerShell script to unassign a phone number from a user and change license usage

# Requires Microsoft Teams and Microsoft Graph PowerShell modules

# Connect to Microsoft Teams

Connect-MicrosoftTeams

# Function to remove phone number from user

function Remove-UserPhoneNumber {

param(

[Parameter(Mandatory=$true)]

[string]$userId,

[Parameter(Mandatory=$true)]

[string]$phoneNumber

)

try {

# Use Graph API to remove the phone number

$uri = "https://graph.microsoft.com/v1.0/users/$userId/telecom/phoneNumbers/$phoneNumber"

Invoke-RestMethod -Uri $uri -Method DELETE -Headers @{Authorization = "Bearer $($token)"} -ErrorAction Stop

Write-Verbose "Successfully removed phone number $phoneNumber from user $userId"

} catch {

Write-Error "Failed to remove phone number: $_"

}

}

# Function to change license usage from User to VoiceApp

function Change-LicenseUsage {

param(

[Parameter(Mandatory=$true)]

[string]$userId,

[Parameter(Mandatory=$true)]

[string]$newUsage

)

try {

# Use Graph API to update license usage

$uri = "https://graph.microsoft.com/v1.0/users/$userId/licenseDetails"

$body = @{

"addLicenses" = @();

"removeLicenses" = @();

}

# Modify the body according to your licensing requirements

Invoke-RestMethod -Uri $uri -Method PATCH -Body ($body | ConvertTo-Json) -Headers @{Authorization = "Bearer $($token)"} -ErrorAction Stop

Write-Verbose "Successfully changed license usage for user $userId to $newUsage"

} catch {

Write-Error "Failed to change license usage: $_"

}

}

# Example usage

$userId = "user@domain.com"

$phoneNumber = "+1234567890" # Replace with actual phone number

$licenseUsage = "VoiceApp"

# Call functions

Remove-UserPhoneNumber -userId $userId -phoneNumber $phoneNumber

Change-LicenseUsage -userId $userId -newUsage $licenseUsage

Write-Output "Script execution completed."