Office365 PowerShell打开邮箱审计功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Office365 PowerShell打开邮箱审计功能相关的知识,希望对你有一定的参考价值。
最近总公司要求Office365需要在所有的邮箱上面打开审计功能。这个功能没法通过图形界面操作,只能通过powershell脚本实现。
微软提供了一个官方的脚本,不过里面有个小bug
https://technet.microsoft.com/en-us/library/dn879651.aspx#step2
当多个用户账户存在相同的alias的时候,他会很奇怪的认为是重名的账户,然后无法修改对应的几个账户,因此不建议直接用get-mailbox | set-mailbox 修改数据,而是手动地for循环处理。
另外还有一个很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 = "[email protected]" $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 = "[email protected]" $to = "[email protected]" $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 " <H1> $date Mailbox Auditing Enabled record </H1>" -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 上确认一下状态已经改变!
本文出自 “麻婆豆腐” 博客,请务必保留此出处http://beanxyz.blog.51cto.com/5570417/1956406
以上是关于Office365 PowerShell打开邮箱审计功能的主要内容,如果未能解决你的问题,请参考以下文章
powershell 批量获取office365用户邮箱使用容量
office 365通过PowerShell批量添加共享邮箱成员
powershell [PS:设置OWA邮箱策略]命令将特定OWA策略设置为用户#PowerShell#Office365