PowerShell: Office 365 License Assignment Report


In the last post, we talked about reporting how many users you created yesterday or in last 7 days or may be in last six months. A similar requirement is to know how many Office 365 licenses you assigned in last 24 hrs or in last 7 days.

Not really perfect way of reporting the same but a workaround PowerShell script for the same purpose might be as given below:

# !Author! Nitish Kumar

$file = "c:\temp\NewUsersLicense_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv"
# If Okay with storing password in the script
$User = "DummyUserName"
$password = 'DummyPassword' | ConvertTo-SecureString -asPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ($User, $password )

# Uncomment the below line and comment three lines above it if want to take credentials as input
# $cred = Get-Credential
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic –AllowRedirection
Import-PSSession $s

Get-Mailbox -resultsize unlimited | where {$_.WhenMailboxCreated -gt (get-date).adddays(-1)} |Select-Object DisplayName,UserPrincipalName,WindowsEmailAddress,whenMailboxCreated,MailboxPlan | Export-Csv "$file" -noTypeInformation

$style = "
BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "

"

$smtpServer = "Your SMTP Server URL/ IP"
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "Your_From_Email_ID here"
$msg.To.Add("Your_To_Email_ID here")
$msg.Subject = "Report of Licenses assigned in last 24 hrs"
$body = "<b>Dear Team</b>"
$body += "Attached are the Report of Licenses assigned in last 24 hrs"
$body += Import-Csv -Path $file | ConvertTo-Html -Head $style
$body += ""
$body += "<b>Regards</b>"
$msg.Body = $body
$msg.IsBodyHTML = $true
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

#End of script

The issue with the above script is only one, it needs the password in Plain Text to be stored in the script to be automated and in case one doesn’t want it to be automated then can work with the alternate way mentioned in the script (Commented right now).

Advertisement

3 thoughts on “PowerShell: Office 365 License Assignment Report

  1. I think you used to blog about Nokia phones a few decades ago. Lol. Nice to stumble again. Which phone you have right now.?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.