您好,登錄后才能下訂單哦!
豆子公司是上市公司,每年都需要審計。因此離職用戶的信息不能刪掉,只能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 = "aaa@bbb.com"
$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 = "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 = "<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\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 (New-ScheduledTaskSettingsSet -Compatibility Win8) -User 'aa' -Password 'pass' -RunLevel Highest -TaskName 'SyncNotification'
Start-ScheduledTask -TaskName 'SyncNotification
結果如下
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。