Script repository
The script updates Microsoft 365 services enabled for user to match tenant configuration. To execute the script, create a scheduled task configured for the Domain object type and add a managed domain to the Activity Scope of the task. The domain will only be used to trigger execution of the scheduled task.
In the script, the $licenseSku variable specifies the SKU Part Number of the Microsoft 365 license whose services should be updated. If you want to update services for all licenses, set the variable to $null.
$licenseSkuToCheck = "ENTERPRISEPACK" # TODO: modify me
# Get Microsoft 365 Object ID.
try
{
$objectId = [Guid]$Context.TargetObject.Get("adm-AzureId")
}
catch [System.Runtime.InteropServices.COMException]
{
if ($_.Exception.ErrorCode -ne 0x8000500D)
{
throw $_.Exception
}
return
}
# Get Microsoft 365 properties.
$microsoft365Properties = $Context.TargetObject.GetOffice365Properties2()
# Get enabled Microsoft 365 licenses.
$licenseInfos = @{}
foreach ($license in $microsoft365Properties.Licenses)
{
if ($licenseSkuToCheck -ne $NULL)
{
if ($license.Sku.SkuPartNumber -ne $licenseSkuToCheck)
{
continue
}
if (!$license.Assigned)
{
return
}
$licenseInfos.Add($license.Sku.SkuPartNumber, $license)
break
}
if (!$license.Assigned)
{
continue
}
# Assigned services
$licenseInfos.Add($license.Sku.SkuPartNumber, $license)
}
if ($licenseInfos.Count -eq 0)
{
return
}
# Bind to the Microsoft 365 tenant of the user.
$tenantDN = $Context.TargetObject.Get("adm-AssociatedO365Tenant")
$tenant = $Context.BindToObjectByDN($tenantDN)
# Get services enabled in the tenant settings.
foreach ($sku in $tenant.Skus)
{
if (-not ($licenseInfos.ContainsKey($sku.SkuPartNumber)))
{
continue
}
$userLicense = $licenseInfos[$sku.SkuPartNumber]
foreach ($tenantService in $sku.Services)
{
if ($tenantService.IsAutoAssignable)
{
continue
}
$userService = $userLicense.Services | Where {$_.Service.ServiceName -eq $tenantService.ServiceName}
if ($tenantService.Enabled -eq $userService.Assigned)
{
continue
}
$userService.Assigned = $tenantService.Enabled
$userLicense.AssignedModificationEnabled = $True
}
}
if (!$microsoft365Properties.ContainsModifications)
{
return
}
# Update Microsoft 365 properties of the user.
$Context.TargetObject.SetOffice365Properties($microsoft365Properties)
$Context.TargetObject.SetInfo()
Comments 0
You must be signed in to comment.