Office365 PowerShell打开邮箱审计功能

网友投稿 244 2022-10-04

Office365 PowerShell打开邮箱审计功能

最近总公司要求Office365需要在所有的邮箱上面打开审计功能。这个功能没法通过图形界面操作,只能通过powershell脚本实现。

微软提供了一个官方的脚本,不过里面有个小bug

但是!当我们使用Get-Mailbox XXXX | Set-mailbox的时候,不管XXX是什么,alias name, displayname 或者ID或者 name等等属性,他获取的值都是一样的,然后当他通过管道传递的时候,他传递的始终是displayname而不是其他值,这样一来,当有重名的displayname存在时候,系统就sb了,不知道该修改哪个,直接报错!

以我自己的邮箱为例,我故意修改了displayname的值,和其他属性不太一样,然后跟踪管道参数的变化

PS C:\temp> get-mailbox "yuan.li"| select name, displayname,id,alias Name    DisplayName Id      Alias   ----    ----------- --      -----   Yuan Li Yuan Lee    Yuan Li yuan.li

跟踪变化

Trace-Command -PSHost -name ParameterBinding -Expression { get-mailbox yuan.li |Set-Mailbox -AuditEnabled $true }

发现经过一大堆的验证和远程调用,最后他传入的参数是displayname,而不是我管道前面输入的属性。这里甭管我输入啥属性,获取到对象之后传给管道的始终是displayname这个属性

因为上面这个bug,豆子不建议直接用官方提供的get-mailbox | set-mailbox  修改数据,而是手动地写个for循环通过Distinguishedname之类的属性处理,避免意外冲突。

另外还有一个很2的地方是,Office365不能设置默认打开审计,因此所有的新账户都是没有打开的。豆子只能设置一个计划任务,让脚本每天自动执行来修改新账户的设定。

另外,执行完了之后,我希望把修改过的账户都给我发一份邮件通知一下,另外最后windows也给我写个日志,以便日后查看。

下面是完整的脚本

#Create a secure string of the your password #Read-Host -AsSecureString | ConvertFrom-SecureString > c:\temp\key.txt #Check if O365 session is setup, if not, create a new one $Sessions=Get-PSSession if (($Sessions.ComputerName -eq "outlook.office365.com") -and ($Sessions.State -ne 'Broken')){     write-host "Detect existing Office365 session, skip.." -ForegroundColor Cyan } else{          $username = "yuan.li@aus.ddb.com"     $secureStringPwd = gc C:\temp\key.txt | ConvertTo-SecureString     $creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd     $ExoSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection     Import-PSSession $ExoSession } #Find Mailboxes that haven't enabled auditing $users=get-mailbox -Filter {AuditEnabled -eq $false} | select name, alias, auditenabled, auditlogagelimit, distinguishedname foreach($user in $users){     try{         Set-Mailbox $user.distinguishedname -AuditEnabled $true -AuditLogAgeLimit 365 -AuditOwner Create,HardDelete,MailboxLogin,MoveToDeletedItems,SoftDelete,Update -ErrorAction Stop        # Create a Windows Eventlog if needed         $username=$user.name         Write-Eventlog  -Logname 'Application' -Source 'Application' -EventID 666 -EntryType Information -Message "$username Maibox Auditing is enabled"          }     catch{         Write-Eventlog  -Logname 'Application' -Source 'Application' -EventID 667 -EntryType Error -Message "$user Mailbox Auditing is failed to enable"      }    } #There are two ways to check the resut, Event Viewer or Email #Check again if the status is changed  $result=foreach($user in $users){     get-mailbox $user.name | select name, alias, auditenabled, auditlogagelimit, distinguishedname }  #Send Email to the admin $from = "yuan.li@syd.ddb.com" $to = "yuan.li@syd.ddb.com" $smtp = "smtp.office365.com"  $sub = "Auditing list"  $secureStringPwd = gc C:\temp\key.txt | ConvertTo-SecureString $creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd $date=get-date  $htmlbody=$result| ConvertTo-Html -Body " 

 $date Mailbox Auditing Enabled record 

" -CssUri C:\tmp\table.css  Send-MailMessage -To $to -From $from -Subject $sub -Body ($htmlbody|Out-String) -Credential $creds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml -UseSsl -port 587  #Check from Event Viewer try{     $eventcritea = @{logname='Application';id=666}     $Events =get-winevent -FilterHashtable $eventcritea -ErrorAction Stop     ForEach ($Event in $Events) {                          $eventXML = [xml]$Event.ToXml()                       $Event | Add-Member -MemberType NoteProperty -Force -Name  Information -Value $eventXML.Event.EventData.Data                      $Event.Information              }             }catch [system.Exception] {          "Couldn't fine any mailbox auditing logs" }      $events | select information, id, logname, timecreated| Out-GridView -Title Status

测试结果

获取的Windows日志

收到的邮件通知

隔了2天,在https://securescore.office.com/#!/score 上确认一下状态已经改变!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Office 365课程讲义
下一篇:SpringBoot项目引入第三方sdk jar包的解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~