EntraAccessAudit.psm1

# EntraAccessAudit 1.0.0, built by tools/Build-Module.ps1 from the script
# beside this file. The command runs the script; change the script, not this.

function Invoke-EntraAccessAudit {
<#
.SYNOPSIS
    Audits who can get into a Microsoft Entra ID tenant and how: privileged
    roles, stale and guest accounts, MFA coverage, app credentials and
    permissions, service accounts, Conditional Access gaps and tenant-wide
    settings. Read-only.

.DESCRIPTION
    Read-only by design. Every call to Microsoft Graph goes through one function
    that sends GET requests only, and the scopes it asks for are all .Read.
    scopes. It changes nothing in the tenant.

    It works in two stages so that every rule can be tested without a tenant:
        1. Collect: read the tenant from Microsoft Graph into a snapshot.
        2. Evaluate: judge the snapshot against each check.
    A saved snapshot can be evaluated again later, on any machine, with
    -FromSnapshot. The checks read only the snapshot, never the tenant.

    Each check gives one of these results:
        Fail evidence collected, and it shows a gap
        Warn evidence collected, and it shows something to review
        Pass evidence collected, and it shows no gap
        Skipped the data the check needs wasn't collected; the reason says why
                 (usually a licence the tenant doesn't have)
        Error the check threw; the message is in the report
    Nothing passes by default. A check whose data is missing is Skipped and
    counted, never passed.

    Some data needs a premium licence. Sign-in activity, the MFA registration
    report and Conditional Access need Microsoft Entra ID P1; PIM eligibility,
    assignment schedules and activation settings need P2. The directory audit
    log needs no licence, but goes back 7 days without P1 and 30 with it. When the tenant has no licence for them,
    the checks that need them are Skipped, or say what they couldn't see.
    TEN-07 warns before a P1 or P2 trial ends, while those checks still run.

    Writes these files into -OutputPath, named by collection time (UTC):
        entra-access-audit-<time>.html a self-contained report
        entra-access-audit-<time>.csv one row per finding
        entra-access-audit-<time>.json tenant, results, dataset status and
                                         thresholds; Power BI reads these
    The reports name real accounts and apps. Keep them somewhere private.

    Exit codes: 0 no Fail and no Error; 1 at least one Fail; 2 at least one
    Error, a dataset that couldn't be read for a reason other than licensing,
    or the audit could not start.

    Graph permissions (delegated, read-only):
        User.Read.All users, guests, account state
        AuditLog.Read.All sign-in activity, MFA registration report
        RoleManagement.Read.Directory role assignments, PIM eligibility
        Application.Read.All app registrations, enterprise apps,
                                       granted permissions
        Policy.Read.All Conditional Access, security defaults
        DelegatedPermissionGrant.Read.All
                                       delegated permissions that admins
                                       or users consented to
        GroupMember.Read.All members of groups that hold a role
        Organization.Read.All the tenant's subscriptions: which
                                       licences it has and when they end
    The Global Reader role is enough to run it. For unattended runs, grant an
    app registration the same permissions as Application permissions and use
    -ClientId, -TenantId and -CertificateThumbprint.

.PARAMETER FromSnapshot
    Evaluate a saved snapshot instead of reading the tenant. Works on any
    operating system and needs no Graph module.

.PARAMETER SnapshotPath
    Also save the collected snapshot to this JSON file. The snapshot names real
    accounts and apps.

.PARAMETER OutputPath
    Folder for the report files. Defaults to a Reports folder in the current
    PowerShell folder, created if needed.

.PARAMETER ProfilePath
    Thresholds file to use instead of the defaults (see
    EntraAccessAudit.example.psd1). Without it, an EntraAccessAudit.local.psd1
    beside the script is used if present. The module is replaced on every
    update, so with the module, keep the file elsewhere and name it here.

.PARAMETER TenantId
    The tenant to sign in to. Required with -CertificateThumbprint.

.PARAMETER ClientId
    The app registration to sign in with. Without it, Microsoft Graph
    PowerShell's own app is used.

.PARAMETER CertificateThumbprint
    Sign in as the app (-ClientId) with this certificate from the current
    user's or local machine's certificate store, for unattended runs.

.PARAMETER PassThru
    Also write the check results to the pipeline.

.EXAMPLE
    Invoke-EntraAccessAudit
    Signs in interactively with the read-only scopes and audits the tenant.

.EXAMPLE
    Invoke-EntraAccessAudit -SnapshotPath .\tenant.snapshot.json
    Audits the tenant and keeps the snapshot.

.EXAMPLE
    Invoke-EntraAccessAudit -FromSnapshot .\tests\fixtures\westmark.snapshot.json
    Evaluates the fictional Westmark tenant that the tests use. No sign-in.

.NOTES
    Version notes (newest last):
    0.1.0 2026-11-10 First release: privileged roles, stale accounts, MFA
                       coverage, app credentials and permissions, and
                       Conditional Access checks; HTML, CSV and JSON reports.
    0.2.0 2026-11-10 Roles held through a group count for each member
                       (GroupMember.Read.All). APP-06 and APP-07: high-risk
                       delegated permissions consented for every user or by
                       users (DelegatedPermissionGrant.Read.All). Snapshot
                       schema 2; schema 1 snapshots still evaluate. An empty
                       MFA registration report skips ROLE-05 and MFA-01 to
                       MFA-03 instead of passing them, and accounts with no
                       record make them Warn.
    0.3.0 2026-11-12 Service accounts area: SVC-01 enterprise app
                       credentials, SVC-02 apps in privileged roles, SVC-03
                       apps from other organizations with high-risk
                       permissions, SVC-04 disabled apps still holding
                       access. Snapshot schema 3.
    0.3.1 2026-11-13 No obsolete-parameter warning when writing the CSV
                       report on PowerShell 7. Test and lint scripts end with
                       a summary banner, table and RESULT bar.
    0.4.0 2026-11-13 ROLE-07 other privileged roles held permanently,
                       ROLE-08 eligible roles that never expire, ROLE-09
                       roles granted to oneself (audit log), ROLE-10 weak PIM
                       activation rules, ROLE-11 owners of role-holding
                       groups, SVC-05 owners of apps with high-risk access.
                       No new scope. Snapshot schema 4.
    0.4.1 2026-11-13 PIM policies keep only the four activation rules and
                       the fields ROLE-10 reads, so a snapshot is about a
                       third of the size.
    0.5.0 2026-11-16 Tenant settings area: TEN-01 user consent, TEN-02
                       app registration and tenant creation, TEN-03 guest
                       access and invitations, TEN-04 SMS and voice, TEN-05
                       cross-tenant defaults, TEN-06 break-glass accounts.
                       ROLE-12 privileged roles assigned outside PIM. ROLE-09
                       no longer lists a self-grant twice when PIM logs it
                       too. No new scope. Snapshot schema 5.
    0.5.1 2026-11-16 Test and lint summaries use the JobSweep layout: one
                       banner line with script, status and counts, a PS line,
                       and a Notes column only when a row needs one.
    0.6.0 2026-11-23 Break-glass checks from a live lab. TEN-06 fails a
                       break-glass account that an enforced Conditional
                       Access policy reaches, and warns when one hasn't
                       signed in within 90 days. CA-05 names who an
                       excluded group lets through, and flags a group that
                       isn't role-assignable or has owners. ROLE-13
                       privileged roles held by personal or external
                       accounts. Fix: TEN-01 called Microsoft's managed
                       consent policies custom. No new scope. Snapshot
                       schema 6.
    0.7.0 2026-11-24 TEN-07 Entra ID P1 or P2 ending: warns before a trial
                       that is the only source of P1 or P2 ends, and fails
                       on one that has expired or been suspended, naming
                       the checks that will then be Skipped. New scope
                       Organization.Read.All, to read the subscriptions.
                       Snapshot schema 7.
    0.8.0 2026-11-24 Power BI template, built from the project in
                       powerbi/: the latest run per tenant, trends across
                       runs, and every finding. The JSON report names the
                       tenant. Fix: SVC-03 called Microsoft Graph Command
                       Line Tools another organization's app when its owner
                       tenant wasn't one it knew; Microsoft's documented
                       app IDs now count as Microsoft's.
    0.9.0 2026-11-25 Also a PowerShell module, EntraAccessAudit, built by
                       tools/Build-Module.ps1 for the PowerShell Gallery:
                       Invoke-EntraAccessAudit runs this script with the
                       same parameters and help, and sets $LASTEXITCODE.
    1.0.0 2026-11-25 First public release, and on the PowerShell Gallery:
                       Install-Module EntraAccessAudit.
#>

[CmdletBinding()]
param(
    [string]$FromSnapshot,
    [string]$SnapshotPath,
    [string]$OutputPath,
    [string]$ProfilePath,
    [string]$TenantId,
    [string]$ClientId,
    [string]$CertificateThumbprint,
    [switch]$PassThru
)

    & (Join-Path $PSScriptRoot 'Invoke-EntraAccessAudit.ps1') @PSBoundParameters
}

Export-ModuleMember -Function Invoke-EntraAccessAudit