We recently wrote a script for a client which we decided to include in the product so other people can use it as a starting point for their own license assignment script. The script content follows but what makes it interesting is the fact that it also turns off some of the features associated with the license. This was important to the client as the E3 license included software (“Microsoft Stream”) which the company did not want to support. The script also presents the user with a list of licenses to select from, as shown below:


param(
    [AutoPopulate(ObjectType='users', MappedField='user_principal_name')] 
    [string]$UserPrincipalName,

    [ValidateSet('Stream', 'Developer Pack','Windows Store','Flow','Power BI','Business Essentials')]
    [string] $License
)

try {

    $LicenseObjectArray = @(
        [PSCustomObject]@{
            FriendlyName = 'Stream'
            SkuPartNumber = 'STREAM'
            DisabledPlanNames = @('Microsoft Stream')   # There are multiple plans in a license. 
                                                        # This will disable the 'Microsoft Stream' plan inside this license
        },
        [PSCustomObject]@{
            FriendlyName = 'Developer Pack'
            SkuPartNumber = 'DEVELOPERPACK'
            DisabledPlanIds = @()                       # Leave this empty to disable no plans
        },
        [PSCustomObject]@{
            FriendlyName = 'Windows Store'
            SkuPartNumber = 'WINDOWS_STORE'
            DisabledPlanNames = @()
        },
        [PSCustomObject]@{
            FriendlyName = 'Flow'
            SkuPartNumber = 'FLOW_FREE'
            DisabledPlanNames = @()
        },
        [PSCustomObject]@{
            FriendlyName = 'Power BI'
            SkuPartNumber = 'POWER_BI_STANDARD'
            DisabledPlanNames = @()
        },
        [PSCustomObject]@{
            FriendlyName = 'Business Essentials'
            SkuPartNumber = 'O365_BUSINESS_ESSENTIALS'
            DisabledPlanNames = @()
        }
    )

    $LicenseObject = $LicenseObjectArray | ? { $_.FriendlyName -eq $License }

    if(-not $LicenseObject) {
        Write-Output 'License is not supported'
        return
    }

    $sku = Get-MgSubscribedSku -All -ErrorAction Stop | Where { $_.SkuPartNumber -eq $LicenseObject.SkuPartNumber }

    $disabledPlans = $sku.ServicePlans |  Where { $_.ServicePlanName -in $LicenseObject.DisabledPlanNames } | Select -ExpandProperty ServicePlanId

    if(-not $disabledPlans) { $disabledPlans = @()}
    
    $addLicenses = @(
        @{
            SkuId = $sku.SkuId
            DisabledPlans = $disabledPlans
        }
    )

    Set-MgUserLicense -UserId $UserPrincipalName -AddLicenses $addLicenses -RemoveLicenses @() -ErrorAction Stop | Out-Null

    Write-Output "Successfully assigned the license $License to the user $UserPrincipalName"
} catch {
    Write-Output "Error while executing the script"
    Write-Error $_
}
Category
Tags

Comments are closed