PowerShell 腳本通知Office365 同步錯誤
阿新 • • 發佈:2018-03-19
powershell office365 dirsync 同步異常 豆子公司是上市公司,每年都需要審計。因此離職用戶的信息不能刪掉,只能disable掉。有的時候,桌面需要把一個離職用戶的郵件重新添加到另一個用戶的別名,以便繼續接收郵件。但是Office365默認配置情況下 一個已經disable掉的用戶,不管怎麽改他都不會繼續同步,這樣造成的結果就是桌面經常修改的順序不對,造成了本地的AD已經改了,但是修改的東西不會同步到office365, 或者直接office365認為已經有記錄了,拒絕添加新的記錄。
鑒於桌面支持的不靠譜,豆子每天都需要看看同步狀態,然後通知桌面修改。登錄主界面,然後點擊DirSync Errors就能看見了
沖突的smtp地址記錄
如何能自動獲取這個界面呢?豆子剛開始找了半天的API,始終沒找到,甚至都開始打爬蟲的註意了,後來終於找到了相關的命令
https://docs.microsoft.com/en-us/powershell/module/msonline/get-msoldirsyncprovisioningerror?view=azureadps-1.0
下面是完整的腳本
Get-PSSession | Remove-PSSession $username = "[email protected]" $secureStringPwd = ConvertTo-SecureString -AsPlainText "password" -Force $creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd Connect-MsolService -Credential $UserCredential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection Import-PSSession $Session $result=Get-MsolDirSyncProvisioningError | select Displayname, LastDirSyncTime, ObjectId, ObjectType, @{n=‘Error‘;e={$_.ProvisioningErrors.ErrorCategory}}, UserPrincipalName $from = "[email protected]" $to = "[email protected]" $smtp = "smtp.office365.com" $sub = "Office365 Sync Error" $secpasswd = ConvertTo-SecureString "Password" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ($from, $secpasswd) $a = "<style>" $a = $a + "BODY{background-color:Lavender ;}" $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}" $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}" $a = $a + "</style>" #import-csv C:\scripts\users.csv | ConvertTo-Html -Body "<H1> User List </H1>" -Head $a | out-file C:\temp\tt.html $htmlbody=$result| ConvertTo-Html -Body "<H1> Office365 DirSync Errors </H1> <H2>For Further details, please visit https://portal.office.com/adminportal/home#/dirsyncobjecterrors</H2>" -Head $a Send-MailMessage -To $to -From $from -Subject $sub -Body ($htmlbody|Out-String) -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml -UseSsl -port 587
收到郵件通知
成功之後設置一個計劃任務
$settingspath=‘C:\users\yuan.li\Documents\GitHub\Powershell\AD and Office365\SyncErrorNotification.ps1‘ if (Get-ScheduledTask -TaskName ‘SyncNotification‘ -ErrorAction SilentlyContinue){ Unregister-ScheduledTask -TaskName ‘SyncNotification‘ -Confirm:$false } $Action = New-ScheduledTaskAction -Execute ‘C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe‘ -Argument "-executionpolicy bypass -File ‘$settingspath‘" $Trigger = New-ScheduledTaskTrigger -Daily -At ‘10AM‘ $Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet) $Task | Register-ScheduledTask -TaskName ‘SyncNotification‘ -User ‘admin‘ -Password ‘password‘
結果如下
PowerShell 腳本通知Office365 同步錯誤