Skip to content

Instantly share code, notes, and snippets.

@technion
Created April 30, 2020 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technion/79a3286a576acfc7e49834a14ed4d31b to your computer and use it in GitHub Desktop.
Save technion/79a3286a576acfc7e49834a14ed4d31b to your computer and use it in GitHub Desktop.
Adds all AzureAD users with a certain license to a group
$skus = Get-AzureADSubscribedSku
# Exchange Online E1
$skue1 = ( $skus | where { $_.skupartnumber -eq 'EXCHANGESTANDARD' } ).SkuID
$members = Get-AzureADUser -All $true
foreach($member in $members) {
if ($member.ImmutableId -eq $null) {
# Cloud user - skip
continue
}
$userlicenses = $member | Select -ExpandProperty AssignedLicenses
#Note and add to group if they have an additional E1
if( $userlicenses | where { $_.Skuid -eq $skue1 } ) {
# Necessary because Get-ADGroupMember doesn't fetch the UserPrincipalName
$guid = ([GUID][System.Convert]::FromBase64String($member.ImmutableID)).Guid
$ADuser = Get-ADUser -Filter "objectGUID -eq '$guid' "
write-host "Adding user $($ADuser.UserPrincipalName) to group $($member.MailNickName)"
Add-ADGroupMember -Identity "LIC_E1" -Members $ADuser
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment