Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#   .SYNOPSIS      Script that assigns Office 365 licenses based on Group membership in WAAD. .DESCRIPTION     The script assigns of licenses for new users based on groups/licenseSKUs in the $licenses hashtable.     It switch licensetype if a user is moved from one group to Another.     It removes the license if the user no longer is a member in any of the license assignment Groups.     Updated 2015-03-25 to support multiple skus for each user.     The script REQUIRES PowerShell 3.0 or later! .NOTES      Author: Johan Dahlbom      Blog: 365lab.net      Email: johan[at]dahlbom.eu      The script are provided β€œAS IS” with no guarantees, no warranties, and they confer no rights.      #>
  2. #Import Required PowerShell Modules
  3. Import-Module MSOnline
  4.  
  5. #Office 365 Admin Credentials
  6. $CloudUsername = 'admin@365lab.net'
  7. $CloudPassword = ConvertTo-SecureString 'Password' -AsPlainText -Force
  8. $CloudCred = New-Object System.Management.Automation.PSCredential $CloudUsername, $CloudPassword
  9.  
  10. #Connect to Office 365
  11. Connect-MsolService -Credential $CloudCred
  12.  
  13. $Licenses = @{
  14.                  'E1' = @{
  15.                           LicenseSKU = 'mstlabs:STANDARDPACK'
  16.                           Group = 'E1_Users'
  17.                         }                        
  18.  
  19.                  'E3' = @{
  20.                           LicenseSKU = 'mstlabs:ENTERPRISEPACK'
  21.                           Group = 'E3_Users'
  22.                         }
  23.             }
  24.  
  25. $UsageLocation = 'SE'
  26.  
  27. #Get all currently licensed users and put them in a custom object
  28. $LicensedUserDetails = Get-MsolUser -All | Where-Object {$_.IsLicensed -eq 'True'} | ForEach-Object {
  29.  [pscustomobject]@{
  30.             UserPrincipalName = $_.UserPrincipalName
  31.             License = $_.Licenses.AccountSkuId
  32.             }
  33.  }
  34.  
  35. #Create array for users to change or delete
  36. $UsersToChangeOrDelete = @()
  37.  
  38. foreach ($license in $Licenses.Keys) {
  39.  
  40.   #Get current group name and ObjectID from Hashtable
  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 with ObjectGuid $GroupID..."
  46.   #Get all members of the group in current scope
  47.   $GroupMembers = (Get-MsolGroupMember -GroupObjectId $GroupID -All).EmailAddress
  48.   #Get all already licensed users in current scope
  49.   $ActiveUsers = ($LicensedUserDetails | Where-Object {$_.License -eq $licenses[$license].LicenseSKU}).UserPrincipalName
  50.   $UsersToHandle = ''
  51.  
  52.     if ($GroupMembers) {
  53.         if ($ActiveUsers) {
  54.             #Compare $Groupmembers and $Activeusers
  55.             #Users which are in the group but not licensed, will be added
  56.             #Users licensed, but not, will be evaluated for deletion or change of license
  57.             $UsersToHandle = Compare-Object -ReferenceObject $GroupMembers -DifferenceObject $ActiveUsers -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  58.             $UsersToAdd = ($UsersToHandle | Where-Object {$_.SideIndicator -eq '<='}).InputObject             $UsersToChangeOrDelete += ($UsersToHandle | Where-Object {$_.SideIndicator -eq '=>'}).InputObject
  59.         } else {
  60.             #No licenses currently assigned for the license in scope, assign licenses to all group members.
  61.             $UsersToAdd = $GroupMembers
  62.         }
  63.  
  64.     } else {
  65.       Write-Warning  "Group $GroupName is empty - will process removal or move of all users with license $($AccountSKU.AccountSkuId)"
  66.       #If no users are a member in the group, add them for deletion or change of license.
  67.       $UsersToChangeOrDelete += $ActiveUsers
  68.     }
  69.  
  70.   #Check the amount of licenses left...
  71.   if ($AccountSKU.ActiveUnits - $AccountSKU.consumedunits -lt $UsersToAdd.Count) {
  72.         Write-Warning 'Not enough licenses for all users, please remove user licenses or buy more licenses'
  73.   }
  74.  
  75.      foreach ($User in $UsersToAdd){
  76.  
  77.         #Process all users for license assignment, if not already licensed with the SKU in order.
  78.           if ((Get-MsolUser -UserPrincipalName $User).Licenses.AccountSkuId -notcontains $AccountSku.AccountSkuId) {
  79.             try {
  80.                   #Assign UsageLocation and License.
  81.                   Set-MsolUser -UserPrincipalName $User -UsageLocation $UsageLocation -ErrorAction Stop -WarningAction Stop
  82.                   Set-MsolUserLicense -UserPrincipalName $User -AddLicenses $AccountSKU.AccountSkuId -ErrorAction Stop -WarningAction Stop
  83.                   Write-Output "SUCCESS: Licensed $User with $license"
  84.             } catch {
  85.                   Write-Warning "Error when licensing $User"
  86.  
  87.             }
  88.  
  89.           }
  90.      }
  91. }
  92.  
  93. #Process users for change or deletion
  94. if ($UsersToChangeOrDelete -ne $null) {
  95.         foreach ($User in $UsersToChangeOrDelete) {
  96.           if ($user -ne $null) {
  97.  
  98.             #Fetch users old license for later usage
  99.             $OldLicense = ($LicensedUserDetails | Where-Object {$_.UserPrincipalName -eq $User}).License
  100.  
  101.              #Loop through to check if the user group assignment has been changed, and put the old and the new license in a custom object.
  102.              #Only one license group per user is currently supported.
  103.              $ChangeLicense = $Licenses.Keys | ForEach-Object {
  104.                   $GroupName = $Licenses[$_].Group
  105.                   if (Get-MsolGroupMember -All -GroupObjectId (Get-MsolGroup -All | Where-Object {$_.DisplayName -eq $GroupName}).ObjectId | Where-Object {$_.EmailAddress -eq $User}) {
  106.                      [pscustomobject]@{
  107.                         OldLicense = $OldLicense
  108.                         NewLicense = $Licenses[$_].LicenseSKU
  109.                      }
  110.                   }
  111.  
  112.               }
  113.  
  114.               if ($ChangeLicense) {
  115.                     #The user were assigned to another group, switch license to the new one.
  116.                     try {
  117.                           Set-MsolUserLicense -UserPrincipalName $User -RemoveLicenses $ChangeLicense.OldLicense -AddLicenses $ChangeLicense.NewLicense -ErrorAction Stop -WarningAction Stop
  118.                           Write-Output "SUCCESS: Changed license for user $User from $($ChangeLicense.OldLicense) to $($ChangeLicense.NewLicense)"
  119.                     } catch {
  120.                           Write-Warning "Error when changing license on $User`r`n$_"
  121.                     }
  122.  
  123.               } else {  
  124.  
  125.                     #The user is no longer a member of any license group, remove license
  126.                     Write-Warning "$User is not a member of any group, license will be removed... "
  127.                     try {
  128.                           Set-MsolUserLicense -UserPrincipalName $User -RemoveLicenses $OldLicense -ErrorAction Stop -WarningAction Stop
  129.                           Write-Output "SUCCESS: Removed $OldLicense for $User"
  130.                     } catch {
  131.                           Write-Warning "Error when removing license on user`r`n$_"
  132.                     }
  133.               }
  134.          }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement