forked from GoneGolfing/PoSH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recover-ADSync.ps1
48 lines (29 loc) · 1.71 KB
/
Recover-ADSync.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<#
email notification for when ad sync fails
9/2/2016
AlertTriggerEmail.ps1
v1.0 - create initial script
v1.1 - add restart of ad sync service if running and start adsync service if stopped
#>
#import ad sync powershell module
Import-Module ADSync
#variable that retrieves last successful time for ad connect sync
$syncTrigger = Get-EventLog -LogName Application -Source 'Directory Synchronization' -Message 'Import/Sync/Export cycle completed (Delta).' -ComputerName server1 -Newest 1 | Select-Object timegenerated
#convert system formatted date string to just hours and minutes
$dateConversion = $syncTrigger.TimeGenerated.ToShortTimeString()
#restart ad sync service if it is hung. start service if status is stopped
$adSyncService = Get-Service -Name ADSync
if ($adSyncService.Status -eq 'Running')
{
Restart-Service -Name ADSync -Force
}
if ($adSyncService.Status -eq 'Stopped')
{
Start-Service -Name ADSync
}
#check logs for successfull restart of service and converts system formatted date string to just hours and minutes
$checkServiceRestart = Get-EventLog -LogName Application -Source 'ADSync' -Message 'The service was started successfully.' -ComputerName server1 -Newest 1 | Select-Object timegenerated
$serviceRestartTime = $checkServiceRestart.TimeGenerated.ToShortTimeString()
Start-ADSyncSyncCycle -PolicyType delta
#email notification
Send-MailMessage -SmtpServer relay.test.com -From [email protected] -to [email protected] -Subject "AD Connect Sync Failure" -BodyAsHtml "AD Sync has reported a synchronization failure. The ADSync service was successfully restarted at $serviceRestartTime. The last successful sync was at $dateConversion."