User Tools

Site Tools


powershell:o365:syncazure

How to sync Azure AD verified

If you ever need to script against both local AD and Azure AD, you will need to Sync. However the sync command (Start-ADSyncCycle) does not contain any method to report when sync is complete (it's a fire and forget command).
You can however check the eventlog to see when the sync is complete.
This is an example script for checking the Eventlog.

function SyncToAzure
{
    Write-Host "Starting Azure AD Sync..." -ForegroundColor Yellow
    [bool]$RetVal = $false;
    try
    {
        $SyncStartTime = Get-Date
        Start-ADSyncSyncCycle -PolicyType Delta
        Write-Host "Waiting for AzureADSync to complete..."
        $event = $null
        [int]$counter = 1
        while  (-not $event)
        {
            $event = Get-EventLog -ComputerName $AzureADSyncServer -LogName Application -InstanceId 904 -Newest 1 -Message "Scheduler::SchedulerThreadMain : Completed configured scheduler operations." | ? {$_.TimeGenerated -gt $SyncStartTime}
            Start-Sleep -Seconds 5
            Write-Host "`rStill syncing... $counter" -NoNewline
            $counter++;
        }
        Write-Host "                                            " -NoNewline;
 
    }
    catch
    {
        Write-Host = $_.Exception.Message
        return [bool]$false
 
    }
    return [bool]$true
}
powershell/o365/syncazure.txt ยท Last modified: 2018/11/29 12:28 by admin