Script Repository


Active users without Microsoft 365 licenses

April 28, 2020
1850

The script sends a report containing active user accounts that do not have Microsoft 365 (Office 365) licenses assigned. To run the script, create a Scheduled Task configured for the Domain-DNS object type.

To use the script, install the Microsoft Azure Active Directory PowerShell Module on the computer where Adaxes service runs.

Parameters:

  • $to - Specifies the recipient email address.
  • $subject - Specifies the email notification subject.
  • $reportHeader - Specifies the report header.
  • $reportFooter - Specifies the report footer.
Edit Remove
PowerShell
$to = "recipient@domain.com" # TODO: modify me
$subject = "Active users without Microsoft 365 licenses assigned" # TODO: modify me
$reportHeader = "<h2>Active users without Microsoft 365 licenses assigned</h2><br/>" # TODO: modify me
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me

$scriptBlock = {
    param($microsoft365Cred)
    Import-Module MsOnline
    
    Connect-MsolService -Credential $microsoft365Cred
    
    [System.Object[]]$unlicensedUsersOnly = Get-MsolUser -All -UnlicensedUsersOnly
    return ,$unlicensedUsersOnly
}

try
{
    $unlicensedUsersOnly = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $Context.GetOffice365Credential() -ErrorAction Stop
}
catch
{
    $Context.LogMessage("An error occurred when searching active users without Microsoft 365 licenses. Error: " + $_.Exception.Message, "Warning")
    $unlicensedUsersOnly = $NULL
}

# Add users to report
$report = New-Object "System.Text.StringBuilder"
[void]$report.Append($reportHeader)

if ($unlicensedUsersOnly -eq $NULL)
{
    $report.Append("<b>No users found</b>")
}

if ($unlicensedUsersOnly -ne $NULL)
{
    [void]$report.Append("<ol>")
    foreach ($user in $unlicensedUsersOnly)
    {
        [void]$report.Append("<li>$($user.UserPrincipalName)</li>")
    }
    [void]$report.Append("</ol>")
}
[void]$report.Append($reportFooter)

# Send mail
$Context.SendMail($to, $subject, $NULL, $report.ToString())


Comments ( 0 )
No results found.
Leave a comment