-
Notifications
You must be signed in to change notification settings - Fork 239
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
$debug
parameter is set, logs activity tograph-licenses-get.txt
usingStart-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-MgGraph
with the specified scope. - Retrieves the context of the connected graph using
Get-MgContext
. - Prints the account information of the connected context.
- 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.
- 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.
- 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.
- 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:
- Sorts the
$licenseSummary
array byskupartnumber
. - Selects key properties:
- License
- Name
- Available
- Assigned
- Formats and displays the sorted summary as a table.
- 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).
- Prints a message indicating that the script has completed.
- If the
$debug
parameter 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.