Report tenant licenses - directorcia/Office365 GitHub Wiki
Script = graph-licenses-get.ps1
Microsoft 365 License Report PowerShell Script
This PowerShell script generates a report on the licenses available in a Microsoft 365 tenant using the Microsoft Graph API. Below is a detailed explanation of its operation and purpose:
Parameters
The script accepts three optional parameters:
$debug
: Enables debug mode, logging script activity.$csv
: Exports the report to a CSV file.$prompt
: Prompts the user for input at various stages.
Metadata and Documentation
- Includes a disclaimer, description, source URL, and documentation URL.
- Mentions a prerequisite: ensuring the MS Graph module is installed.
Variable Initialization
- Initializes variables for message colors:
$systemmessagecolor
$processmessagecolor
$errormessagecolor
$warningmessagecolor
- Sets the output file path to
..\graph-licenses.csv
.
Debug Logging
- If the
$debug
parameter is set, logs activity tograph-licenses-get.txt
usingStart-Transcript
.
Clear Host and Initial Messages
- Clears the host screen with
Clear-Host
. - Prints a message indicating the start of the tenant license report script.
Connecting to Microsoft Graph
- Prints a message indicating it is connecting to MS Graph.
- Defines the required scope for accessing license information:
LicenseAssignment.Read.All
. - Connects to Microsoft Graph using
Connect-MgGraph
with the specified scope. - Retrieves the context of the connected graph using
Get-MgContext
. - Prints the account information of the connected context.
Prompt Handling
- If the
$prompt
parameter is set:- Prompts the user to confirm the connected account.
- If the response is not "Y" or "y", disconnects from Microsoft Graph and exits.
Fetching Product Codes
- Prints a message indicating it is fetching product codes via a web request.
- Sends a
GET
request to a specified URL to retrieve a list of product codes in JSON format. - Converts the JSON content to a PowerShell object and stores it in the
$skulist
variable.
Fetching License Information
- Prints a message indicating it is making a request to Microsoft Graph for all licenses.
- Sends a
GET
request to the Microsoft Graph API endpoint for subscribed SKUs, storing the results in the$results
variable. - Catches exceptions if an error occurs during the request:
- Prints the error message in red.
- Exits the script gracefully.
Processing the Results
- Initializes an empty array
$licenseSummary
to store license information. - Iterates over each item in
$results
:- Creates a custom object with:
- License Part Number (
skupartnumber
) - Product Name (from
$skulist
) - Available Licenses (
prepaidunits.enabled
) - Assigned Licenses (
consumedunits
)
- License Part Number (
- Adds the custom object to the
$licenseSummary
array.
- Creates a custom object with:
Displaying the License Summary
- Sorts the
$licenseSummary
array byskupartnumber
. - Selects key properties:
- License
- Name
- Available
- Assigned
- Formats and displays the sorted summary as a table.
Output to CSV
- If the
$csv
parameter is set:- Prints a message indicating that data will be output to a CSV file.
- Exports the
$licenseSummary
array to the specified$outputFile
in CSV format (without type information).
Finishing Up
- Prints a message indicating that the script has completed.
- If the
$debug
parameter is set, stops the transcript logging.
Purpose
The script's primary goal is to:
- Connect to Microsoft Graph.
- Retrieve license information from a Microsoft 365 tenant.
- Display or export this information in a user-friendly format.