I had recently written a PowerShell script to manually kick off the DirSync Scheduled Task force changes I made in AD up to Office 365. It was easy, make sure the Scheduled Task isn’t in a Running State (if it is, wait a bit and check again), kick off the scheduled task, then wait for it’s State to change back to Ready. This solution worked great… until I upgraded to the latest Azure AD Connect client (version 1.1). Now the Scheduled Task was gone and I was left wondering how I was supposed to kick of the sync via PowerShell.
Searching around the Internet revealed that the new version replaced the Scheduled Task with AD Connect’s own sync engine scheduling, controlled via PowerShell (YAY!). Since my script was already written in PowerShell this was going to make the process much easier, just run the new sync cmdlet (Start-ADSyncSyncCycle) and voila! But that was the easy part. The hard part was figuring out how to tell when the sync was running and when it finished, in the event of the rare chance it was already running when I tried to kick it off and risk my changes not being synced.
Do{
#Try to start DirSync. If it is already running, we have to wait until it is done and kick it off again
$DirSyncRunning = $false
try{
Start-ADSyncSyncCycle
}
catch{
$DirSyncRunning = $true
Start-Sleep 10
}
} While($DirSyncRunning)
Start-Sleep 5
$DirSyncEvents = $false
$dirSyncStartTime = Get-Date
Do {
#Wait for our instance of DirSync to finish by monitoring for Event ID 904 in the logs with the message “Scheduler::SchedulerThreadMain : Completed configured scheduler operations.” generated after we started
Start-Sleep 10
$DirSyncEvents = Get-EventLog -LogName Application -InstanceId 904 -Newest 1 -Message “Scheduler::SchedulerThreadMain : Completed configured scheduler operations.” | ? {$_.TimeGenerated -gt $dirSyncStartTime}
} While(-not $DirSyncEvents)
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.