溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PowerShell 腳本通知Office365 同步錯(cuò)誤

發(fā)布時(shí)間:2020-06-28 11:13:12 來源:網(wǎng)絡(luò) 閱讀:3426 作者:beanxyz 欄目:系統(tǒng)運(yùn)維

豆子公司是上市公司,每年都需要審計(jì)。因此離職用戶的信息不能刪掉,只能disable掉。有的時(shí)候,桌面需要把一個(gè)離職用戶的郵件重新添加到另一個(gè)用戶的別名,以便繼續(xù)接收郵件。但是Office365默認(rèn)配置情況下 一個(gè)已經(jīng)disable掉的用戶,不管怎么改他都不會(huì)繼續(xù)同步,這樣造成的結(jié)果就是桌面經(jīng)常修改的順序不對(duì),造成了本地的AD已經(jīng)改了,但是修改的東西不會(huì)同步到office365, 或者直接office365認(rèn)為已經(jīng)有記錄了,拒絕添加新的記錄。

鑒于桌面支持的不靠譜,豆子每天都需要看看同步狀態(tài),然后通知桌面修改。登錄主界面,然后點(diǎn)擊DirSync Errors就能看見了

PowerShell 腳本通知Office365 同步錯(cuò)誤

沖突的smtp地址記錄

PowerShell 腳本通知Office365 同步錯(cuò)誤

如何能自動(dòng)獲取這個(gè)界面呢?豆子剛開始找了半天的API,始終沒找到,甚至都開始打爬蟲的注意了,后來終于找到了相關(guān)的命令

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 

收到郵件通知

PowerShell 腳本通知Office365 同步錯(cuò)誤

成功之后設(shè)置一個(gè)計(jì)劃任務(wù)

$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

結(jié)果如下

PowerShell 腳本通知Office365 同步錯(cuò)誤

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI