We recently concluded development for a script for one of our clients who assigns dozens of E3 licenses per week. The challenge was that not all the products included in the E3 license was supported internally by the company.

This meant that every license assigned also had to be tweaked in portal.azure.com to turn off a number of products associated with each license.

This entire process is easily scripted via PowerShell, and once validated we converted the script into a PowerScript and pushed it down to the Service Desk. Now the Service Desk can assign licenses with ZERO training and ZERO mistakes.

We decided to include the script 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:

For those curious, here is the script itself:



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 $_
}
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 $_
}

Categories:

Tags:

Comments are closed