-
Notifications
You must be signed in to change notification settings - Fork 247
Report tenant licenses
directorcia edited this page Dec 7, 2024
·
2 revisions
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:
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.
- Includes a disclaimer, description, source URL, and documentation URL.
- Mentions a prerequisite: ensuring the MS Graph module is installed.
- Initializes variables for message colors:
$systemmessagecolor$processmessagecolor$errormessagecolor$warningmessagecolor
- Sets the output file path to
..\graph-licenses.csv.
- If the
$debugparameter is set, logs activity tograph-licenses-get.txtusingStart-Transcript.
- Clears the host screen with
Clear-Host. - Prints a message indicating the start of the tenant license report script.
- 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-MgGraphwith the specified scope. - Retrieves the context of the connected graph using
Get-MgContext. - Prints the account information of the connected context.
- If the
$promptparameter is set:- Prompts the user to confirm the connected account.
- If the response is not "Y" or "y", disconnects from Microsoft Graph and exits.
- Prints a message indicating it is fetching product codes via a web request.
- Sends a
GETrequest 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
$skulistvariable.
- Prints a message indicating it is making a request to Microsoft Graph for all licenses.
- Sends a
GETrequest to the Microsoft Graph API endpoint for subscribed SKUs, storing the results in the$resultsvariable. - Catches exceptions if an error occurs during the request:
- Prints the error message in red.
- Exits the script gracefully.
- Initializes an empty array
$licenseSummaryto 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
$licenseSummaryarray.
- Creates a custom object with:
- Sorts the
$licenseSummaryarray byskupartnumber. - Selects key properties:
- License
- Name
- Available
- Assigned
- Formats and displays the sorted summary as a table.
- If the
$csvparameter is set:- Prints a message indicating that data will be output to a CSV file.
- Exports the
$licenseSummaryarray to the specified$outputFilein CSV format (without type information).
- Prints a message indicating that the script has completed.
- If the
$debugparameter is set, stops the transcript logging.
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.