By: Anshuman Mansingh, Technology Specialist, Microsoft Corporation, https://www.linkedin.com/in/anshumanmansingh/ |
I have often come across this requirement where I am asked for a custom report that returns the following.
- A division of licenses based on domains
- Types of licenses assigned
Fortunately, we have Microsoft Excel that can do much of the filtering – if we can export a list of all the details required above. And so, I wrote two PowerShell scripts.
Please find them at the end of this article.
Note: This script has been designed for information retrieval and does not change anything on Office 365. Also, it is not covered under Microsoft support – please treat this as a sample.
Outputs
Script – Version 1
Script Version 1 Output:
This script will find the desktop and post a comma-delimited CSV file. This file can be opened in the notepad. However, to make better sense, you can open it in MS Excel and then press “Ctrl + T” (and select “My table has headers”), to format the output as a table.
Once converted to a table, you can click on the small drop-down (down-arrow) buttons next to each column-head to filter the table as you need.
References on dealing with CSV data in Excel
Columns in the output CSV:
- Display Name
- User Principal Name
-
Each SKU or License-type (in the tenant) has a column in the CSV
Below is a sample.
Script Version 1 Time Taken:
This script returns value of the time taken. Below are some test numbers on time taken to connect to office 365, retrieve data, analyze and export to a csv file.
8000 users – 2 minutes 13 seconds
20000 users – 6 minutes 28 seconds
This looks like an acceptable time investment considering the once-in-a-month-type nature of the report.
However, the time taken increases with increasing user base and slower connection and is dependent on the client system configuration.
Script – Version 2
Script Version 2 Output:
This script will find the desktop and post multiple comma-delimited CSV files.
(You can format and filter these CSVs as tables – as explained above in output description of script-version-1.)
Columns in the first CSV:
- Display Name
- User Principal Name
- Each SKU or License-type (in the tenant) has a column in the CSV
Columns in the second CSV:
- Display Name
- User Principal Name
-
Each Office 365 Service (in the tenant) has a column in the CSV
Below is a sample from the second CSV
Script Version 2 Time Taken:
The script returns the value of time taken.
8000 users – 2 minutes 31 seconds
20000 users – 6 minutes 48 seconds
Steps to Run
Step 1:
Only for the first run: Prepare PowerShell to run Office 365 related scripts.
- Open Windows PowerShell as an administrator
- Run the command: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
- Confirm “Yes” or “Yes to All” when prompted.
- Close PowerShell Window.
Step 2:
Only for the first run: Download and install the necessary PowerShell modules.
- Microsoft Online Services Sign-In Assistant for IT Professionals RTW and
-
Azure Active Directory Module for PowerShell
Step 3:
Only for the first run:
Ready the script
- Copy the script on to notepad.
- Save the file as a “.ps1” file on the desktop.
Step 4:
Running the script – There are two options.
-
Action Item: Starting the script
Option 1: Right click on the .ps1 file and click on “Run with PowerShell”
Option 2: Open Windows PowerShell > Type in the path of the file at the prompt – (e.g. “c:usersanshumanscript.ps1”) > hit Enter key.
- Action Item: The script will prompt for credentials. Please enter your Office 365 Global Administrator credentials.
-
Wait & Watch: The script will then keep you waiting as it starts to connect and retrieve users.
-
Wait & Watch: It will then start reading into each user’s licensing information. And while it does that, you will see a progress bar.
- Wait & Watch: Once, the processing is over, this script is going to write the licensing information on to a CSV file – and post it on your desktop.
Script Version 1
#Editables $NameOfOutputFile_LicenseDetails $NameOfOutputFile_Errors $PathOfOutputFiles #ErrorsRefreshed $Error.Clear() #Prompts for Office 365 Administrator Credential if(!$cred){$cred = $TimeFlag1 #Initiates Remote PowerShell connection Write-Progress Import-Module #Start of Script Write-Progress $AllUsers $AccountSkuId $paras $List $DesktopPath #Start of User Loop Write-Progress $AllUsers
}
} #End of User Loop $List $Error $TimeFlag2 $host.EnterNestedPrompt();#$host.ExitNestedPrompt() #End of Script
|
Script Version 2
#Editables $NameOfOutputFile_LicenseDetails $NameOfOutputFile_ServiceStatus $NameOfOutputFile_Errors $PathOfOutputFiles #ErrorsRefreshed $Error.Clear() #Prompts for Office 365 Administrator Credential if(!$cred){$cred = $TimeFlag1 #Initiates Remote PowerShell connection Write-Progress Import-Module #Start of Script Write-Progress $AllUsers #ColumnsStart $AccountSkuId $paras $ServicePlans $paras2 #ColumnsEnd $List $DesktopPath #Start of User Loop Write-Progress $AllUsers
}
}
} #End of User Loop $List $List2 $Error $TimeFlag2 $host.EnterNestedPrompt();#$host.ExitNestedPrompt() #End of Script |