How to Setup Unified Tenant Configuration Management

Unified Tenant Configuration Management is Microsoft’s new native configuration drift monitor solution that allows administrators to efficiently manage changes to the desired state within their Microsoft tenant. I summarised what this is in detail in my previous article, which introduces you to UTCM.

In this article, I will walk you through configuring Unified Tenant Configuration Management with PowerShell.

Prerequisites

To run the commands in this article, you will need to ensure you have PowerShell v7 installed. Guidance for this can be found on Microsoft Learn.

Also, when you connect to Microsoft Graph, you will need to do so with the following scopes, using a Global Administrator account: ConfigurationMonitoring.ReadWrite.All

Use the example below to connect to Microsoft Graph:

Connect-MgGraph -Scopes 'ConfigurationMonitoring.ReadWrite.All'

Methology

There are 3 main components of Unified Tenant Configuration Management: Snapshots, Monitors & Drifts. Snapshots are point-in-time captures of a set of resources in your tenant, like Conditional Access policies or Exchange transport rules. Monitors are like an engine, which contains a specific snapshot and periodically compares your current tenant configuration to the snapshot. Drifts then represent any changes within your tenant which differ from the snapshot, for example, the displayName of a specific Conditional Access policy which has changed.

These three components make up the core of UTCM; each has some limitations which can be seen in the API limits documentation on Microsoft Learn. But ultimately, they are APIs which you must wrap something around to make them meaningful to the user, which is what I aim to do with PowerShell in this article.

Setup authentication for UTCM

Unified Tenant Configuration Management uses a dedicated service principal which enables monitors to function. The monitor essentially impersonates this service principal to access the resources it is monitoring. As such, if the monitor is monitoring a Conditional Access policy, the service principal will need the Policy.Read.ConditionalAccess permissions assigned. This service principal is referred to as the UTCM service principal.

While UTCM is in public preview, you will first need to manually create this service principal and grant it the necessary permissions. Follow the steps below to create and grant permissions to the UTCM service principal:

1. Open PowerShell 7 and install the required Graph PowerShell modules:

Install-Module Microsoft.Graph.Authentication

2. Connect to Microsoft Graph with the Application.ReadWrite.All permission:

Connect-MgGraph -Scopes 'Application.ReadWrite.All'

3. Run the following commands: (You might notice that in the official Microsoft documentation, their example uses the New-MgServicePrincipalAppRoleAssignment cmdlet. This works, but I like to remove dependencies on other modules in my scripts.)

# Create a new service principal
$body = @{
    appId = "03b07b79-c5bc-4b5e-9bfa-13acf4a99998"
}
Invoke-MgGraphRequest -Uri "v1.0/servicePrincipals" -Method POST -Body $body

# Grant admin consent for the application
$permissions = @('Policy.Read.All', 'Policy.ReadWrite.ConditionalAccess')
$graph = Invoke-MgGraphRequest -Uri "v1.0/servicePrincipals?`$filter=appId eq '00000003-0000-0000-c000-000000000000'" -Method GET -OutputType PSObject | Select -Expand Value
$utcm = Invoke-MgGraphRequest -Uri "v1.0/servicePrincipals?`$filter=appId eq '03b07b79-c5bc-4b5e-9bfa-13acf4a99998'" -Method GET -OutputType PSObject | Select -Expand Value

foreach ($requestedPermission in $permissions) {
    $AppRole = $Graph.AppRoles | Where-Object { $_.Value -eq $requestedPermission }
    $body = @{
        appRoleId = $AppRole.Id
        resourceId = $Graph.Id
        principalId = $UTCM.Id
    } | ConvertTo-Json 

    Invoke-MgGraphRequest -Uri "/v1.0/servicePrincipals/$($UTCM.Id)/appRoleAssignments" -Method POST -Body $body
}

Once this is complete, we can be confident that the UTCM service can create snapshots of and monitor the Conditional Access policies in our tenant.

Capturing a baseline / snapshot

Snapshots (or otherwise “baselines”) in UTCM are an extract of the current tenant configuration settings for a particular service. These essentially represent the desired state, so it is important that snapshots are taken when the resource (or resources) you want to capture are in the desired state. In this example, I am going to snapshot the Conditional Access policies in my tenant; however, there are over 300 different resource types you can snapshot in your tenant, as I depicted in my previous article. 

To create a snapshot of your current Conditional Access policies, use the code example below:

$uri = "beta/admin/configurationManagement/configurationSnapshots/createSnapshot"
$body = @{
    displayName = "Conditional Access Baseline"
    description = "Criticial CA policies"
    resources = @(
        "microsoft.entra.conditionalaccesspolicy"
    )
}
Invoke-MgGraphRequest -Uri $uri -Method POST -Body $body

If you want to take a snapshot of multiple resources, add multiple lines to the resources collection, like so:

resources = @(
        "microsoft.entra.conditionalaccesspolicy",
        "microsoft.entra.authenticationmethodpolicy"
    )

You just need to consider that if you have multiple resources, you need to add the additional relevant permissions to the UTCM service principal. For example, where I have added the microsoft.entra.authenticationmethodpolicy resource, I would need to add the Policy.Read.AuthenticationMethod permission to the UTCM service principal. Unfortunately, there is no public mapping between resources and Graph API permissions, so you need to make an educated guess in some instances.

Creating a configuration monitor

Once the snapshot has been captured, you need to configure monitoring for the resources captured within the snapshot. To do this, you need to first obtain the settings captured within the snapshot, which are stored within a location defined in the resourceLocation property of the snapshot configuration. I have provided some example code below and added comments to help you understand it.

# The filter query to locate the desired snapshot
$filter = "displayName eq 'Conditional Access Baseline'"
$uri = "beta/admin/configurationManagement/configurationSnapshotJobs/?`$filter=$filter"
# The request to retrieve the snapshot
$snapshot = Invoke-MgGraphRequest -Uri $uri -Method GET -OutputType PSObject | Select -Expand Value
# Get the URI to obtain the snapshot configuration settings
$resourceLocation = $snapshot[0].resourceLocation
# The request to get the snapshot configuration
$resources = Invoke-MgGraphRequest -Uri $resourceLocation -Method GET
$fineResources = $resources | Select displayName, description, resources

The code above finishes with the $fineResources variable, which contains only the necessary information required for the monitor. Specifically, the display name of the snapshot, the description, and all the configurations of the target resources. Once we have that information, we can use it to populate the request to create the monitor, like I have done below. Consider the displayName and description properties below and rename them appropriately.

$uri = "beta/admin/configurationManagement/configurationMonitors"
$body = @{
    displayName = "Conditional Access Monitor"
    description = "Criticial CA policies Monitor"
    baseline = @{
        displayName = $fineResources.displayName
        description = $fineResources.description
        resources = ($fineResources.resources | Select-Object -Property displayName, resourceType, properties)
    }
} | ConvertTo-Json -Depth 10
Invoke-MgGraphRequest -Uri $uri -Method POST -Body $body

Viewing the configuration monitor job status

Once your monitor is created, it will run by default on a schedule of every 6 hours. Every time it runs, it will produce a job which you can view the status of. To find jobs relating to your monitor, you first need to find the ID of your monitor. You can do this by filtering the monitors via the displayName of the monitor you just created.

$filter = "displayName eq 'Conditional Access Monitor'"
$uri = "beta/admin/configurationManagement/configurationMonitors/?`$filter=$filter"
$monitorJob = Invoke-MgGraphRequest -Uri $uri -Method GET -OutputType PSObject | Select -Expand Value

Now you can use this information to filter for all jobs relating to your monitor:

$filter = "monitorId eq '$($monitorJob[0].id)'"
$uri = "/beta/admin/configurationManagement/configurationMonitoringResults?`$filter=$filter"
$monitorResults = Invoke-MgGraphRequest -Uri $uri -Method GET -OutputType PSObject | Select -expand Value

When viewing $monitorResults in your PowerShell console, you will see related information about each job that runs every 6 hours. Notably, the driftsCount property highlights the number of drifts that have been detected from the baseline configuration.

Configuration Monitor Results

Viewing configuration drift

When your monitor job status reports that you have some configuration drift to review, you can query the configurationDrifts endpoint and filter on your monitorId to find all drifts relating to your monitor:

$filter = "monitorId eq '$($monitorJob[0].id)'"
$uri = "/beta/admin/configurationManagement/configurationDrifts/?`$filter=$filter"
$configurationDrifts = Invoke-MgGraphRequest -Uri $uri -Method GET -OutputType PSObject | Select -expand Value

If you have drifts, your results will look similar to the below:

Configuration Drift Results
Configuration Drift Results

To help you view the information in each drift, I recommend you convert the results to JSON, so the information isn’t collapsed within the nested objects.

$configurationDrifts | ConvertTo-Json -Depth 10

You can now clearly see which configurations have drifted:

View drift details
View drift details

Wrapping up

Overall, I love this new feature, as it really appeals to the day-to-day struggles organisations have where their tenant drifts away from its desired state. Unfortunately, right now UTCM is just a set of APIs, which still makes it difficult to use, unless you have skills in PowerShell (or follow this article in detail). You still need to incorporate the commands I have shown you into an automated solution so you don’t have to resort to manually identifying drifts each day.

Daniel Bradley

Daniel Bradley is a UK-based technical architect and Microsoft MVP in Security. He specialises in Microsoft Entra, Intune, and Microsoft Graph PowerShell and is the author of Microsoft Graph PowerShell for Administrators. He writes and edits Our Cloud Network using experience gained from working with Microsoft cloud environments.

Leave a Reply