最近总公司要求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这个属性
因为上面这个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日志
收到的邮件通知
隔了2天,在https://securescore.office.com/#!/score 上确认一下状态已经改变!