﻿===============================================================================
 SOPHOS ITDR BILLABLE USER COUNT: MICROSOFT ENTRA ID
 Get-ITDRBillableUsers.ps1, version 1.6
===============================================================================

Get-ITDRBillableUsers.ps1 estimates how many Sophos ITDR licenses a customer
needs by counting billable users in their Microsoft Entra ID tenant. It applies
the same criteria Sophos ITDR uses to calculate licensed usage.

The script is read-only. It does not change anything in the tenant.


-------------------------------------------------------------------------------
 1. LICENSING SUMMARY
-------------------------------------------------------------------------------

Sophos ITDR is licensed per user, for both ITDR Standalone and ITDR as an
XDR/MDR add-on. Order quantity equals the billable user count.

A user is billable when ALL of the following are true:

  - The account is enabled in the Identity Provider
      (accountEnabled = true)

  - The account is not a guest (B2B) user
      (userType is not "Guest")

  - The account has signed in within the last 90 days
      (see section 6, "How sign-in activity is measured")

Accounts that do not meet all three are not billable. This includes disabled
accounts, guest users, accounts with no successful sign-in, and accounts with
no sign-in in the last 90 days.


-------------------------------------------------------------------------------
 2. REQUIREMENTS
-------------------------------------------------------------------------------

  - Windows PowerShell 5.1 or PowerShell 7+

  - Microsoft Graph PowerShell modules:
      Microsoft.Graph.Authentication
      Microsoft.Graph.Users

  - An active Microsoft Entra ID P1 or P2 license on the tenant. Sign-in
    activity data is not available on Entra ID Free, or when the P1/P2
    license is expired or suspended. In that case the script still runs,
    but reports an upper bound instead of a billable count (see section 5).

  - An account that can consent to the delegated Graph permissions
    User.Read.All and AuditLog.Read.All, and that holds a role allowed to
    read sign-in activity, for example Global Reader or Reports Reader. An
    administrator may need to grant consent.


-------------------------------------------------------------------------------
 3. INSTALLATION
-------------------------------------------------------------------------------

Install the two required modules for the current user:

    Install-Module Microsoft.Graph.Authentication, Microsoft.Graph.Users -Scope CurrentUser

You may see a prompt that PSGallery is an untrusted repository. PSGallery is
Microsoft's official PowerShell Gallery; this prompt is the default
confirmation on new machines. Answer Y to continue.

If script execution is blocked, allow it for the current session only:

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass


-------------------------------------------------------------------------------
 4. RUNNING THE SCRIPT
-------------------------------------------------------------------------------

Basic run against the tenant of the account you sign in with:

    .\Get-ITDRBillableUsers.ps1

Run against a specific customer tenant and export per-user detail:

    .\Get-ITDRBillableUsers.ps1 -TenantId contoso.onmicrosoft.com -ExportCsv .\contoso-itdr.csv

A browser sign-in window opens. Sign in with an account that has access to
the target tenant and accept the permission prompt.

Parameters:

  -TenantId    (Recommended)
               Tenant domain (for example contoso.onmicrosoft.com) or tenant
               ID (GUID). If omitted, the script uses the home tenant of the
               account you sign in with.

  -ExportCsv   (Optional)
               Path for a CSV file with one row per user, showing billable
               status and reason.

  -Days        (Optional)
               Sign-in lookback window in days. Default is 90. Do not change
               this for licensing purposes.

IMPORTANT: Partners working in a customer tenant through GDAP or a guest
administrator account should always pass -TenantId. Otherwise the script may
count the partner's own tenant.


-------------------------------------------------------------------------------
 5. READING THE OUTPUT
-------------------------------------------------------------------------------

The script prints a breakdown by category and the total billable count.
Example:

    Sophos ITDR billable user count (Entra ID)
    Sign-in cutoff: 2026-08-01 UTC (90 days)

    Category                  Count
    --------                  -----
    Billable                   1000
    Guest (B2B)                1200
    Disabled                    400
    No sign-in in 90 days       350
    No successful sign-in        50

    Total directory users : 3000
    Billable users        : 1000

Use the "Billable users" number as the order quantity.

The CSV export includes UserPrincipalName, DisplayName, AccountEnabled,
UserType, LastSignInUtc, Billable and Reason for each user. LastSignInUtc is
in ISO 8601 UTC format, for example 2026-11-09T19:13:55Z. Use the CSV to
review the count with the customer.

When sign-in activity is unavailable:

If the tenant has no active Entra ID P1/P2 license, or the signed-in account
cannot read sign-in activity, the script shows a warning and continues
without sign-in data. Example:

    WARNING: Sign-in activity is unavailable. The tenant does not have an
    active Entra ID P1/P2 license (Free, expired or suspended).
    WARNING: Continuing without sign-in data. Results show an UPPER BOUND,
    not a billable count.

    Sophos ITDR billable user count (Entra ID)
    Sign-in cutoff: not applied (sign-in activity unavailable)

    Category                              Count
    --------                              -----
    Enabled member (sign-in unknown)       1400
    Guest (B2B)                            1200
    Disabled                                400

    Total directory users : 3000
    Billable users        : UNKNOWN
    Upper bound           : 1400 (enabled, non-guest; 90-day sign-in filter not applied)

The upper bound counts enabled, non-guest users. The true billable count is
at or below it, because users with no sign-in in the last 90 days cannot be
excluded. In the CSV, LastSignInUtc shows "Unavailable" and Billable shows
"Unknown" for every user.

If the warning names permissions rather than licensing, re-run with an
account that has the required consent and role (see section 2) to get the
billable count. If it names licensing, use the upper bound as a conservative
estimate, or re-run once the license is active.


-------------------------------------------------------------------------------
 6. HOW SIGN-IN ACTIVITY IS MEASURED
-------------------------------------------------------------------------------

The script uses the same sign-in field Sophos ITDR uses:

  lastSuccessfulSignInDateTime: the most recent successful sign-in,
  interactive or non-interactive.

Only successful sign-ins count. Failed attempts, for example an account
blocked by MFA or targeted by password spraying, do not make an account
billable. Accounts with no successful sign-in appear in the output as
"No successful sign-in". These may be unused accounts, or accounts that are
being targeted, and are worth reviewing with the customer.


-------------------------------------------------------------------------------
 7. THINGS TO KNOW
-------------------------------------------------------------------------------

  - Service accounts and shared mailboxes: an account's type and Microsoft
    license assignment do not affect the count. An enabled, non-guest account
    that has signed in within 90 days is billable.

  - External members: users from another organization added as members (not
    guests) are counted if they meet the criteria. Users invited as B2B
    guests are not counted.

  - Point-in-time count: the result reflects the tenant at the moment you run
    the script. Sophos ITDR measures usage on an ongoing basis, so the
    in-product count may differ slightly, mostly for users near the 90-day
    boundary.

  - Large tenants: retrieving all users can take several minutes in tenants
    with tens of thousands of accounts.

  - Customers with on-premises Active Directory as well as Entra ID: when
    Entra ID is connected, Sophos ITDR counts from Entra ID only, so use this
    script. Use Get-ITDRBillableUsersAD.ps1 only for customers with
    on-premises Active Directory and no Entra ID.


-------------------------------------------------------------------------------
 8. TROUBLESHOOTING
-------------------------------------------------------------------------------

Symptom:     Warning "Sign-in activity is unavailable. The tenant does not
             have an active Entra ID P1/P2 license", and the output shows
             "Billable users : UNKNOWN" with an "Upper bound" figure
Cause:       The tenant is on Entra ID Free, or its P1/P2 license is expired
             or suspended. Microsoft blocks sign-in activity data in this
             case (error Authentication_RequestFromNonPremiumTenantOrB2CTenant).
Resolution:  The upper bound counts enabled, non-guest users without the
             90-day sign-in filter, so the true billable count is at or
             below it. Use the upper bound as a conservative estimate, or
             re-run once the license is active. Sophos ITDR itself also
             relies on this data, so check the customer's Entra licensing
             before deployment.

Symptom:     Warning "Sign-in activity is unavailable. The signed-in account
             lacks AuditLog.Read.All consent", with an upper bound shown
Cause:       Missing AuditLog.Read.All consent, or the account's role cannot
             read sign-in activity. Microsoft returns a 403 with one of:
             Authorization_RequestDenied,
             Authentication_MSGraphPermissionMissing, or
             Authentication_RequestFromUnsupportedUserRole.
Resolution:  Run the script with an account that holds Global Reader or
             Reports Reader, and ask a Global Administrator or Privileged
             Role Administrator to grant consent if prompted.

Symptom:     "Microsoft Graph PowerShell modules not found"
Cause:       Modules not installed
Resolution:  Run the installation command in section 3.

Symptom:     Count is for the wrong tenant
Cause:       -TenantId not specified
Resolution:  Re-run with -TenantId.
