Skip to content

Who holds a licence, or what one person holds.

--user defaults to OFFICE365_USERNAME, so the script with no arguments answers "what do I hold". --sku starts from get_licensed() — the service answers which users hold anything — and keeps the users whose assigned SKU id matches. --user reads that person's licenseDetails, where every plan carries a provisioning status: a plan that is not Success is bought but not usable yet.

python whose.py
python whose.py --sku SPE_E5
python whose.py --user ada@contoso.com

Permissions

Requires User.Read.All and Organization.Read.All.

Reference

View source

import argparse

from office365.directory.licenses.tenant import LicencesReport
from office365.graph_client import GraphClient
from tests.settings import client_id, client_secret, tenant, user_principal


def main() -> None:
    parser = argparse.ArgumentParser(description="Who holds a SKU, or what one user holds")
    who = parser.add_mutually_exclusive_group()
    who.add_argument("--sku", help="SKU part number, e.g. SPE_E5")
    who.add_argument("--user", default=user_principal, help="User principal name (default: OFFICE365_USERNAME)")
    args = parser.parse_args()
    if not args.sku and not args.user:
        raise SystemExit("--user, or set OFFICE365_USERNAME")

    client = GraphClient(tenant=tenant).with_client_secret(client_id, client_secret)
    if args.sku:
        sku = client.subscribed_skus.find_by_part_number(args.sku, required=True).execute_query()
        licensed = client.users.select(["userPrincipalName", "assignedLicenses"]).get_licensed()
        licensed.get_all().execute_query()
        holders = [user for user in licensed if any(lic.skuId == sku.sku_id for lic in user.assigned_licenses)]
        print(f"{len(holders)} users hold {sku.sku_part_number}")
        for user in holders:
            print(f"  {user.user_principal_name}")
        return
    details = client.users.get_by_principal_name(args.user).license_details.get().execute_query()
    for licence in LicencesReport(skus=list(details)).skus:
        print(licence.sku_part_number)
        for plan in licence.service_plans:
            print(f"  {plan.service_plan_name} {plan.provisioning_status}")


if __name__ == "__main__":
    main()

← Back to Licensing