Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Last active December 1, 2018 22:08
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 darrenjrobinson/04d6e719931f4e9cec20063bbcaefd70 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/04d6e719931f4e9cec20063bbcaefd70 to your computer and use it in GitHub Desktop.
User Office365 Licenses using PowerShell. Associated blog post can be found here https://blog.darrenjrobinson.com/adding-removing-user-office365-licences-using-powershell-and-the-azure-ad-graph-restapi/
<#
Get Active Users
#>
$Search = Invoke-RestMethod -Method Get -Headers @{
Authorization = $authenticationResult.CreateAuthorizationHeader()
'Content-Type' = "application/json"
} -Uri ('https://graph.windows.net/{0}/users?$filter=accountEnabled eq true &api-version=1.6' -f $authenticationResult.TenantId)
# Work out which ones aren't assigned the license we are looking to assign
$userlicensed = $false
$unlicencedUsers = @()
$licensedUsers = @()
foreach($user in $search.value){
# check license against users licenses
foreach ($license in $user.assignedLicenses) {
if ($license.skuId -eq $LicenceToAdd){
$userlicensed = $true
$licensedUsers += $user.userPrincipalName
}
}
if (!$userlicensed -or !$user.assignedLicenses ){
#Exclude the AADConnect Account
if (!$user.userPrincipalName.StartsWith("Sync_")){
$unlicencedUsers += $user.userPrincipalName
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment