Advertisement
Guest User

Licenses

a guest
Oct 17th, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <#
  2.     .SYNOPSIS
  3.         Script that assigns Office 365 licenses based on Group membership in AAD.
  4.     .NOTES
  5.         Author: Johan Dahlbom
  6.         Blog: 365lab.net
  7.         Email: johan[at]dahlbom.eu
  8.         The script are provided β€œAS IS” with no guarantees, no warranties, and they confer no rights.
  9.         Requires PowerShell Version 3.0!
  10.     #>
  11.  
  12.     #Import Required PowerShell Modules
  13.     Import-Module MSOnline
  14.     #Office 365 Admin Credentials
  15.     $CloudUsername = 'admin@tenant.onmicrosoft.com'
  16.     $CloudPassword = ConvertTo-SecureString 'password' -AsPlainText -Force
  17.     $CloudCred = New-Object System.Management.Automation.PSCredential $CloudUsername, $CloudPassword
  18.     #Connect to Office 365
  19.     Connect-MsolService -Credential $CloudCred
  20.     $UsageLocation = 'SE'
  21.  
  22.     $Licenses = @{
  23.                      'E3-ExchangeOnline' = @{
  24.                               LicenseSKU = 'acme103:ENTERPRISEPACK'
  25.                               EnabledPlans = 'EXCHANGE_S_ENTERPRISE'
  26.                               Group = 'E3-ExchangeOnline-Users'
  27.                             }                          
  28.                      'E3-LyncO365ProPlus' = @{
  29.                               LicenseSKU = 'acme103:ENTERPRISEPACK'
  30.                               EnabledPlans = 'MCOSTANDARD','OFFICESUBSCRIPTION'
  31.                               Group = 'E3-LyncO365ProPlus-Users'
  32.                             }  
  33.                      'E3' = @{
  34.                               LicenseSKU = 'acme103:ENTERPRISEPACK'
  35.                               Group = 'E3-Users'
  36.                             }
  37.                 }
  38.  
  39.  
  40.     foreach ($license in $Licenses.Keys) {
  41.         $GroupName = $Licenses[$license].Group
  42.         $GroupID = (Get-MsolGroup -All | Where-Object {$_.DisplayName -eq $GroupName}).ObjectId
  43.         $AccountSKU = Get-MsolAccountSku | Where-Object {$_.AccountSKUID -eq $Licenses[$license].LicenseSKU}
  44.    
  45.         Write-Output "Checking for unlicensed $license users in group $GroupName"
  46.      
  47.         #region Disable non specific plans
  48.         $EnabledPlans = $Licenses[$license].EnabledPlans
  49.         if ($EnabledPlans) {
  50.             $LicenseOptionHt = @{
  51.                 AccountSkuId = $AccountSKU.AccountSkuId
  52.                 DisabledPlans = (Compare-Object -ReferenceObject $AccountSKU.ServiceStatus.ServicePlan.ServiceName -DifferenceObject $EnabledPlans).InputObject
  53.             }
  54.             $LicenseOptions = New-MsolLicenseOptions @LicenseOptionHt
  55.         }
  56.         #endregion Disable non specific plans
  57.  
  58.         #Get all unlicensed group members - needs to be changed if a user should be able to have more than one license
  59.         $GroupMembers = Get-MsolGroupMember -GroupObjectId $GroupID -All | Select-Object EmailAddress,@{Name="Licenses";Expression={(Get-MsolUser -UserPrincipalName $_.EmailAddress).Licenses }} | ForEach-Object -Process {
  60.             if ($_.Licenses) {
  61.                 if ($EnabledPlans) {
  62.                     foreach ($plan in $EnabledPlans) {
  63.                     $ServiceStatus = ($_.Licenses.ServiceStatus | Where-Object {$_.ServicePlan.ServiceName -eq $plan}).Provisioningstatus
  64.                         switch ($servicestatus) {
  65.                             'Disabled' {
  66.                                 $AssignCustomLicense = $true
  67.                             }
  68.                             DEFAULT {
  69.                                 $AssignCustomLicense = $false
  70.                             }
  71.                         }
  72.                     }
  73.                
  74.                 }
  75.             }
  76.            [pscustomobject]@{
  77.                UserPrincipalName = $_.EmailAddress
  78.                BaseLicense = $_.Licenses.AccountSkuID
  79.                AssignCustomLicense = $AssignCustomLicense
  80.                Licenses = $_.Licenses
  81.            }    
  82.         }
  83.         #Warn if not enough licenses are available
  84.         if ($AccountSKU.ActiveUnits - $AccountSKU.consumedunits -lt $GroupMembers.Count) {
  85.             Write-Warning 'Not enough licenses for all users, please remove user licenses or buy more licenses'
  86.         }
  87.         foreach ($User in $GroupMembers | Where-Object {$_.BaseLicense -notcontains "$($AccountSKU.AccountSkuId)" -or $_.AssignCustomLicense}) {
  88.             try {
  89.                 #Set UsageLocation
  90.                 Set-MsolUser -UserPrincipalName $User.UserPrincipalName -UsageLocation $UsageLocation -ErrorAction Stop -WarningAction Stop        
  91.                 $LicenseConfig = @{
  92.                     UserPrincipalName = $User.UserPrincipalName
  93.                 }
  94.                 if (($user.BaseLicense) -notcontains "$($AccountSKU.AccountSkuId)") {
  95.                     $LicenseConfig['AddLicenses'] = $AccountSKU.AccountSkuId
  96.                 }
  97.                 if ($EnabledPlans) {
  98.                     $CurrentPlans = (($user.Licenses | Where-Object {$_.AccountSkuid -eq "$($accountsku.AccountSkuId)"}).Servicestatus | Where-Object {$_.ProvisioningStatus -eq 'Success' -or $_.ProvisioningStatus -eq 'PendingInput'}).ServicePlan.ServiceName
  99.                     if ($CurrentPlans) {
  100.                         $ActualPlans = @()
  101.                         $ActualPlans += $CurrentPlans
  102.                         $ActualPlans += $EnabledPlans
  103.                         $LicenseOptions.DisabledServicePlans = ((Compare-Object -ReferenceObject $AccountSKU.ServiceStatus.ServicePlan.ServiceName -DifferenceObject $ActualPlans)).inputobject
  104.                     }
  105.                
  106.                     $LicenseConfig['LicenseOptions'] = $LicenseOptions
  107.                 }        
  108.                 Set-MsolUserLicense @LicenseConfig -ErrorAction Stop -WarningAction Stop
  109.                 Write-Output "SUCCESS: licensed $($User.UserPrincipalName) with $license"
  110.             } catch {
  111.                 Write-Warning "Error when licensing $($User.UserPrincipalName)`r`n$_"
  112.             }
  113.         }
  114.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement