Skip to content

Report tenant licenses

directorcia edited this page Dec 7, 2024 · 2 revisions

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 to graph-licenses-get.txt using Start-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

  1. Prints a message indicating it is connecting to MS Graph.
  2. Defines the required scope for accessing license information: LicenseAssignment.Read.All.
  3. Connects to Microsoft Graph using Connect-MgGraph with the specified scope.
  4. Retrieves the context of the connected graph using Get-MgContext.
  5. 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

  1. Prints a message indicating it is making a request to Microsoft Graph for all licenses.
  2. Sends a GET request to the Microsoft Graph API endpoint for subscribed SKUs, storing the results in the $results variable.
  3. Catches exceptions if an error occurs during the request:
    • Prints the error message in red.
    • Exits the script gracefully.

Processing the Results

  1. Initializes an empty array $licenseSummary to store license information.
  2. 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)
    • Adds the custom object to the $licenseSummary array.

Displaying the License Summary

  1. Sorts the $licenseSummary array by skupartnumber.
  2. Selects key properties:
    • License
    • Name
    • Available
    • Assigned
  3. 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

  1. Prints a message indicating that the script has completed.
  2. 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.