Marcel That's me: Marcel

Automatically add or change Azure Active Directory computer objects on-demand

Automatically add or change Azure Active Directory computer objects on-demand

Hybrid joined AVD session hosts are great for working with conditional access, Intune (MEM), access to Azure files, and other AAD integrated services.

But there are some challenges: One challenge is that it takes a while to see new session hosts after the deployment in AAD - which avoids that AAD services (like ca) can be used directly. One reason for the delay is that AD objects are synced every 30 minutes by default using ADConnect. In the worst case, it takes 30 minutes before the computer object is visible in AAD.

My approach to lowering the delay is to pimp ADConnect with a custom PowerShell tool (SyncOnDemand.ps1): The tool checks every 3 seconds for computer objects in AD. The tool triggers ADConnect to start a differential sync if a computer object is new or updated after the last 30 minutes. To avoid the sync being triggered for the same computer object multiple times, already detected computer objects are ignored for the next 30 minutes.

This should bring down the time for a host before the host is also visible in AAD.

How does it work? Copy the script to the ADConnect server. Modify line 31 ($OuPath = …) to match your OU and domain.

Run the script in an administrative PowerShell. The script checks every 3 seconds for changed computer objects. Change one of the computer objects in the right OU to test it (e.g., change the description). The script detects the change and triggers a delta sync to AAD.

If everything works as expected, you can install the script with:

SyncOnDemand.ps1 -Install

That creates a scheduled task to run the script on the startup of the ADConnect server. The scheduled task is started directly and runs in the background.

Update: To speed up the replication between AD sites, you can use (“Use Notify”)[]

Note: This is a workaround. Use at your own risk

Script (must be run as an administrator):

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

if ($Install) {
    Write-Host "Installing script to $env:windir"
    Copy-Item "$($MyInvocation.InvocationName)" -Destination ("$($env:windir)\SyncOnDemand.ps1")

	$action = New-ScheduledTaskAction -Execute "$env:windir\System32\WindowsPowerShell\v1.0\Powershell.exe" -Argument "-executionPolicy Unrestricted -File `"$($env:windir)\SyncOnDemand.ps1`""
	$trigger = New-ScheduledTaskTrigger	-AtStartup
	$principal = New-ScheduledTaskPrincipal 'NT Authority\SYSTEM'
	$settingsSet = New-ScheduledTaskSettingsSet
	$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settingsSet 
	Register-ScheduledTask -TaskName 'ITPC-ADConnect-OnDemand' -InputObject $task -ErrorAction Ignore | Out-Null
	Start-ScheduledTask -TaskName 'ITPC-ADConnect-OnDemand'
    Write-Host "Starting the task"

# Parameter
$OuPath = "LDAP://OU=Servers,OU=Sys,OU=Organisation,DC=ITProCloud,DC=test"
$RunEachSeconds = 3

# Initialize
$cycle = Get-Date

try {
    Get-ADSyncScheduler | Out-Null
} catch {
    Write-Error "Command Get-ADSyncScheduler not exist. Please check your configuration. This script must run on a computer with AD-Connect installed"

do {
    $lastStart = Get-Date
    # cleanup ignore-list
    $table.clone().GetEnumerator() | foreach {
        if ($_.Value -le $lastStart.AddMinutes(-$AcceptChangesForTheLastMinutes)) {
                Write-Host "Removing computer from the ignore-list: $($_.Name)"

    $search.SearchScope = "Subtree"
    $search.SearchRoot = $OuPath

    Write-Host "Computer objects found: $($obj2.Count)"
    $obj2 | foreach {
        if ($le -eq $null) {
            Write-Host "Found an updated computer: $($_.Path)"
        } else {
            Write-Host "Skipping computer while object is in the ignore-list for a while: $($_.Path)"

    Write-Host "Ignore list: $($table.Count)"
    Write-Host "New objects: $newObjects"
    if ($newObjects -gt 0) {
        Write-Host "Starting delta sync"
        try {
            do {
                Sleep -Seconds 1
            } while ((Get-ADSyncScheduler).SyncCycleInProgress)
            Start-ADSyncSyncCycle -PolicyType Delta | Out-Null
        } catch {
            Write-Error "Delta sync failed"
        do {
            Sleep -Seconds 1
        } while ((Get-ADSyncScheduler).SyncCycleInProgress)
        Write-Host "Delta sync done"
    Write-Host "Waiting for the next cycle"
    Sleep -Seconds $RunEachSeconds
} while ($true)