PowerShell script notifies Office365 of synchronization error
Douzi Company is a listed company and needs to be audited every year. Therefore, the information of outgoing users can not be deleted, but can only be deleted by disable. Sometimes, the desktop needs to re-add one outgoing user's message to another user's alias in order to continue to receive mail. However, under the default configuration of Office365, a user who has lost disable will not continue to synchronize no matter how he changes it. As a result, the desktop is often modified in the wrong order, resulting in that the local AD has been changed, but the modified things will not be synchronized to office365, or office365 directly thinks that there are already records and refuses to add new records.
In view of the unreliable desktop support, Douzi needs to check the synchronization status every day and then notify the desktop to make changes. Log in to the main screen and click DirSync Errors to see it.
Conflicting smtp address records
How can I get this interface automatically? Beans had just been looking for API for a long time, but they never found it, and even began to pay attention to reptiles, and finally found the relevant orders.
Https://docs.microsoft.com/en-us/powershell/module/msonline/get-msoldirsyncprovisioningerror?view=azureadps-1.0
Here is the complete script
Get-PSSession | Remove-PSSession$username = "aaa@bbb.com" $secureStringPwd = ConvertTo-SecureString-AsPlainText "password"-Force$creds = New-Object System.Management.Automation.PSCredential-ArgumentList $username, $secureStringPwdConnect-MsolService-Credential$ UserCredential$Session = New-PSSession-ConfigurationName Microsoft.Exchange-ConnectionUri https://outlook.office365.com/powershell-liveid/-Credential$ creds-Authentication Basic-AllowRedirectionImport-PSSession $Session$result=Get-MsolDirSyncProvisioningError | select Displayname, LastDirSyncTime, ObjectId, ObjectType, @ {nailed error' E = {$_ .ProvisioningErrors.ErrorCategory}}, UserPrincipalName$from = "helpdesk@bbb.com" $to = "aaa@bbb.com" $smtp = "smtp.office365.com" $sub = "Office365 Sync Error" $secpasswd = ConvertTo-SecureString "Password"-AsPlainText-Force $mycreds = New-Object System.Management.Automation.PSCredential ($from, $secpasswd) $a = "$a = $a +" BODY {background-color:Lavender;} "$a = $a +" TABLE {border-width: 1pxbomborderstyle: solid;border-color black;border-collapse: collapse: } "$a = $a +" TH {border-width: 1pxtransferpadding: 0pxborderthumb style: solid;border-color: black;background-color:thistle} "$a = $a +" TD {border-width: 1pxtransferpadding: 0pxborderripple style: solid;border-color: black Background-color:PaleGoldenrod} "a = $a +" # import-csv C:\ scripts\ users.csv | ConvertTo-Html-Body "User List"-Head $a | out-file C:\ temp\ tt.html$htmlbody=$result | ConvertTo-Html-Body "Office365 DirSync Errors For Further details, please visit https://portal.office.com/adminportal/home#/dirsyncobjecterrors"-Head $aSend-MailMessage-To $to-From $from-Subject $sub-Body ($htmlbody | Out-String)-Credential $mycreds-SmtpServer $smtp-DeliveryNotificationOption Never-BodyAsHtml-UseSsl-port 587
Receive email notification
Set up a scheduled task after success
$settingspath='C:\ users\ yuan.li\ Documents\ GitHub\ Powershell\ 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'register-ScheduledTask-Action $Action-Trigger $Trigger-Settings (Settings-Settings) Win8)-User 'aa'-Password' pass'-RunLevel Highest-TaskName 'SyncNotification'Start-ScheduledTask-TaskName' SyncNotification
The results are as follows