Bulk Policy Assignment in Microsoft Teams

In a very large Microsoft Teams environment, assigning, removing, or updating a user policy assignment for a large user base can be a frustrating process due to session time-out and the tenant level throttling limits. The traditional approach has been to loop through all users to assign, remove, or update the policy assignment as required. With the release of the “New-CsBatchPolicyAssignmentOperation” cmdlet in Microsoft Teams, the traditional approach can be replaced for bulk assignment. This cmdlet allows you to submit a batch of the user to Microsoft for policy assignment, where the assignment is performed as an asynchronous operation by Microsoft. It means you are not required to loop through all users. 
At the moment, the batch policy assignment is limited to the following Microsoft Teams policy types: 
  • CallingLineIdentity
  • ExternalAccessPolicy
  • OnlineVoiceRoutingPolicy
  • TeamsAppSetupPolicy
  • TeamsAppPermissionPolicy
  • TeamsCallingPolicy
  • TeamsCallParkPoliy
  • TeamsChannelsPolicy
  • TeamsEducationAssignmentsAppPolicy
  • TeamsEmergencyCallingPolicy
  • TeamsMeetingBroadcastPolicy
  • TeamsEmergencyCallRoutingPolicy
  • TeamsMeetingPolicy
  • TeamsMessagingPolicy
  • TeamsUpdateManagementPolicy
  • TeamsUpgradePolicy
  • TeamsVerticalPackagePolicy
  • TeamsVideoInteropServicePolicy
  • TenantDialPlan
A batch may contain up to 5,000 users. For over 5,000 users, multiple batches can be submitted. The function below can be extended to split the list of users passed into a batch of 5000 users for policy assignment, where the policy type can be one from the above list. For each submitted batch an operation ID will be generated.

Function BulkPolicyAssignment
{
Param($UserList)
$startrow = 0 ;
$counter = 1 ;
while ($startrow -lt $UserList.count)
{
$BulkBatch = $UserList | select-object -skip $startrow -First 5000
$startrow += 5000
New-CsBatchPolicyAssignmentOperation -PolicyType TeamsUpgradePolicy -PolicyName UpgradeToTeams -Identity $BulkBatch.UserPrincipalname -OperationName "$counter-BulkBatch"
$counter++ ;
}
}
Once a batch is submitted, the status for each batch can be monitored through the “Get-CsBatchPolicyAssignmentOperation” cmdlet. 
Bulk Batch Operation - Get-CsBatchPolicyAssignmentOperation
The code below can be extended to fetch a list of submitted bulk assignment batches between a time and get the results.

$GetBulkBatch = Get-CsBatchPolicyAssignmentOperation | Where-Object {$_.CreatedTime -gt
$datetimeToString1 -and $_.CreatedTime -lt $datetimeToString2}
foreach ($batch in $GetBulkBatch)
{
Get-CsBatchPolicyAssignmentOperation -Identity $batch.OperationId | Select-Object -ExpandProperty UserState
}

The UserState property offers Result (Success or an Error message) and State (Completed, InProgress, or Not Started) parameters and can be used to filter users from batches for any further action. 

Bulk Batch Operation - Get-CsBatchPolicyAssignmentOperation
In the future, the policy types that are supported will be extended for sure. Also, having the same functionality for other Microsoft 365 PowerShell modules (Exchange Online, MSOL, AzureAD, and more) will be a great addition. 

No comments:

Post a Comment