Office365 PowerShell打开邮箱审计功能

简介:

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


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

https://technet.microsoft.com/en-us/library/dn879651.aspx#step2


我发现Office365的一个bug:我们有个别用户同时存在一个AD同步的账户,也有cloud创建的账户,而且两个账户都是用的同一个名字。Office365允许这么做,而且不会报错。 但是!当我们使用Get-Mailbox XXXX | Set-mailbox的时候,不管XXX是什么,alias name, displayname 或者ID或者 name等等属性,他获取的值都是一样的,然后当他通过管道传递的时候,他传递的始终是displayname而不是其他值,这样一来,当有重名的displayname存在时候,系统就sb了,不知道该修改哪个,直接报错!


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


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


跟踪变化

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


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


wKioL1mSnXORncY7AAFAJndIWmo322.jpg



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


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


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


下面是完整的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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 " <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日志

wKiom1mShjLBa8dSAAC8P4ocmk8510.jpg



收到的邮件通知

wKioL1mShjOQQ0OTAAGkn3iYXIs147.jpg


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






本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1956406,如需转载请自行联系原作者

目录
相关文章
|
10月前
网页编辑Office Word文档,开启修订功能,启用留痕、显示留痕并接受留痕
在日常办公环境场景下,有时候会遇到帮助他人修改文档或者为文档提供修改意见,如果我们在文档中直接修改,其他人很不容易看到我们修改了哪个部分,如果一旦你的修改意见不被采纳,原作者还需要恢复原来的文档,这样为别人带来了更多的工作。 如果用猿大师办公助手在网页中编辑Office Word文档,开启修订功能,启用留痕、显示留痕并接受留痕,就可以很好的来解决此问题。
407 7
|
存储 程序员 Python
1行Python代码,把PPT转成图片,python-office功能更新~
pip install python-office,自动化办公,赶紧试试~
255 0
1行Python代码,把PPT转成图片,python-office功能更新~
OFFICE软件有哪三大语言功能?
OFFICE软件有哪三大语言功能?
83 0
OFFICE拼写语法检查:全部忽略、全部更正的功能
OFFICE拼写语法检查:全部忽略、全部更正的功能
125 0
下一篇
无影云桌面