Force an Azure AD Sync

In this post we’ll be discussing performing a sync with AzureAD in a few simple steps with increased convenience.

Lets start with a fairly common use case for forcing a sync with AzureAD. You make a change of some kind in your local AD and you want to test it right away, instead waiting 30-45 minutes for the next sync to run you decide to open up RDP, connect to your Sync server, type out your password, open up PowerShell, try and remember the Sync command, possibly fail, Google it, return to your RDP session and start the sync. Doesn’t that sound like fun?

Another option is to connect to your Sync server with a PSSession and repeat most of the above steps, which is slightly less not fun, there’s gotta be a better way, right? Lets continue below.

function Sync-AzureAD {
    $Session = New-PSSession -ComputerName 'AD-Sync' -Credential (Get-Credential)
    Import-Module (Import-PSSession -Session $Session -CommandName 'Start-ADSyncSyncCycle' -AllowClobber)
    try {
        Start-ADSyncSyncCycle -ErrorAction Stop
    }
    catch {
        Write-Warning $PSItem.Exception.Message
    }
    Disconnect-PSSession -Session $Session
}

Store this in your PowerShell profile or a Utility/Toolbox module, and just like that you have a single cmdlet that will start a sync for you.

Note this will still require a password of some kind but stay tuned for a soon to come post for safely storing credentials in scripts and modules.