I need a method to get all user accounts in Office365 (synced from on-prem AD with AADSync), that are NOT shared mailboxes. I also need to know if they have a license, but I already know that bit.
I need this to potentially assign them a license.
The flow:
- A new AD account is created on premises
- It is synced with AADSync to AAD/Office365
- A process queries AAD for users and uses custom logic to determine, if a user should get a license assigned or not. The newly created account is returned and listed as having no license
- Process assigns license to new account, because a business rule says so
How do I do step 3 here?
I currently use something like this:
Get-MsolUser -MaxResults Unlimited | Select-Object UserPrincipalName,IsLicensed,UsageLocation,Licenses
...But this also returns shared mailboxes, which I do not want.
PS C:\> Get-MsolUser | Select-Object UserPrincipalName,IsLicensed,UsageLocation,Licenses | foreach {$_.UserPrincipalName} {Get-Mailbox -ErrorAction SilentlyContinue -identity $_.UserPrincipalName -Filter {(-not(RecipientTypeDetailsValue -eq 'SharedMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'RoomMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'EquipmentMailbox'))}}