Skip to content

Instantly share code, notes, and snippets.

@DaveRuijter
Last active April 11, 2024 12:54
Show Gist options
  • Save DaveRuijter/a8d1e9c5d66fe892e0ee47a10cdbe66d to your computer and use it in GitHub Desktop.
Save DaveRuijter/a8d1e9c5d66fe892e0ee47a10cdbe66d to your computer and use it in GitHub Desktop.

Revisions

  1. DaveRuijter revised this gist Nov 13, 2018. No changes.
  2. DaveRuijter created this gist Nov 13, 2018.
    44 changes: 44 additions & 0 deletions Assign AD Users Power BI Free license.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # install module
    Install-Module AzureAD -Scope CurrentUser # if already installed you can skip this line
    Import-Module AzureAD # if already imported you can skip this line

    # connect
    Connect-AzureAD # this will initiate a dialog to enter credentials

    # Find the SkuID of the license we want to add - in this case the Power BI Free license, which is called "POWER_BI_STANDARD"
    $PowerBIFreeSKUId = Get-AzureADSubscribedSku | Where-Object {$_.SkuPartNumber -eq "POWER_BI_STANDARD"} | Select -ExpandProperty SkuId

    # Check how many license units are available
    $ConsumedPowerBIFreeLicenses = (Get-AzureADSubscribedSku | Where-Object {$_.SkuPartNumber -eq "POWER_BI_STANDARD"}).ConsumedUnits
    $EnabledPowerBIFreeLicenses = (Get-AzureADSubscribedSku | Where-Object {$_.SkuPartNumber -eq "POWER_BI_STANDARD"} | Select -ExpandProperty PrepaidUnits).Enabled

    # Get all AD Users Accounts without a Power BI Free License
    $UsersWithoutPowerBIFree = Get-AzureADUser -All $true | Where-Object {($_.AssignedLicenses).SkuId -ne $PowerBIFreeSKUId -and $_.AccountEnabled -eq $true -and $_.UsageLocation -ne "" -and $_.UsageLocation -ne $null}
    $UsersWithoutPowerBIFree.Count #show count of users without free license

    # Check if there are enough licenses to cover the users without license, and stop if not the case
    if ( $UsersWithoutPowerBIFree.Count -gt ($EnabledPowerBIFreeLicenses-$ConsumedPowerBIFreeLicenses)){
    throw "Not enough licenses! There are $EnabledPowerBIFreeLicenses licenses in total, $ConsumedPowerBIFreeLicenses already consumed.. not enough for $($UsersWithoutPowerBIFree.Count) users!"
    }

    # Create the objects we'll need
    $Assignedlicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
    $Assignedlicenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses

    # Set the SkuId correctly
    $Assignedlicense.SkuId = $PowerBIFreeSKUId

    # Set the Power BI license as the license we want to add in the $licenses object
    $Assignedlicenses.AddLicenses = $Assignedlicense

    ####
    # THIS IS WHERE IT ALL HAPPENS :)
    #
    # For all the AD users without a license, call the Set-AzureADUserLicense cmdlet to set the license
    $UsersWithoutPowerBIFree | Set-AzureADUserLicense -AssignedLicenses $Assignedlicenses
    #
    ####

    # Doublecheck: Get all AD Users Accounts without a Power BI Free License again
    $UsersWithoutPowerBIFree = Get-AzureADUser -All $true | Where-Object {($_.AssignedLicenses).SkuId -ne $PowerBIFreeSKUId}
    $UsersWithoutPowerBIFree.Count #show count of users without free license