/
Search-AdminRolesChanges.ps1
132 lines (107 loc) · 4.43 KB
/
Search-AdminRolesChanges.ps1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<#
.SYNOPSIS
Search-AdminRolesChanges.ps1 - Reports on Office 365 Admin Role
.DESCRIPTION
This script produces a report of the membership of Office 365 admin role groups.
By default, the report contains only the groups with members.
To get all the role, included empty roles, add -IncludeEmptyRoles $true
.OUTPUTS
The report is output to an array contained all the audit logs found.
To export in a csv, do Search-AdminRolesChanges | Export-CSV -NoTypeInformation "$(Get-Date -Format yyyyMMdd)_adminRolesChange.csv"
.EXAMPLE
Search-AdminRolesChanges
.LINK
https://itpro-tips.com/2020/get-the-office-365-admin-roles-and-track-the-changes/
https://github.com/itpro-tips/Microsoft365-Toolbox/blob/master/Audit/Search-AdminRolesChanges.ps1
.NOTES
Written by Bastien Perez (ITPro-Tips.com)
For more Office 365/Microsoft 365 tips and news, check out ITPro-Tips.com.
Version history:
V1.0, 17 august 2020 - Initial version
V.1.1, 4 january 2020 - Add ObjectUserPrincipalName
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
#>
# Admin roles list: https://docs.microsoft.com/en-us/microsoft-365/compliance/search-the-audit-log-in-security-and-compliance?view=o365-worldwide
function Search-AdminRolesChanges {
[CmdletBinding()]
param (
[string[]]$ObjectIDs
)
try {
Import-Module exchangeonlinemanagement -ErrorAction stop
}
catch {
Write-Warning 'First, install the official Microsoft Exchange Online Management module : Install-Module ExchangeOnlineManagement'
return
}
try {
$null = Get-Command Search-UnifiedAuditLog -ErrorAction Stop
}
catch {
Write-Host 'Connect to Exchange Online' -ForegroundColor Cyan
try {
Connect-ExchangeOnline -ShowBanner:$false -ErrorAction Stop
}
catch {
Write-Warning 'Unable to connect to Exchange Online'
return
}
}
try {
#$maxAdminLogAge = [System.TimeSpan]::Parse((Get-AdminAuditLogConfig).AdminAuditLogAgeLimit).Days
$maxAdminLogAge = 365
Write-Host 'Search Add/Remove Member to Role actions logs' -ForegroundColor Cyan
if ($ObjectIDs) {
$objects = New-Object System.Collections.Generic.List[String]
foreach ($obj in $ObjectIDs) {
$user = Get-User $obj
$null = $objects.Add($user.UserPrincipalName)
$null = $objects.Add($user.ExternalDirectoryObjectId)
}
}
else {
# Set tp $null to search All
$objects = $null
}
$records = Search-UnifiedAuditLog -StartDate (Get-Date).AddDays(-$maxAdminLogAge) -EndDate (Get-Date).AddDays(1) -Operations ('Add Member to Role', 'Remove Member From Role') -ResultSize 2000 -Formatted -ObjectIds $objects
}
catch {
Write-Warning "Unable to gather information $($_.Exception.Message)"
return
}
if ($records.Count -eq 0) {
Write-Host 'No audit logs found' -ForegroundColor Green
}
else {
Write-Host "Processing $($Records.Count) audit records..." -ForegroundColor Cyan
$report = New-Object System.Collections.Generic.List[Object]
ForEach ($record in $records) {
$auditData = ConvertFrom-Json $record.Auditdata
$timeStamp = Get-Date $record.CreationDate -format g
# Test if ObjectID is GUID or the UserPrincipalName
if ($auditData.ObjectID -match '(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$') {
$objectID = $auditData.ObjectID
}
else {
$objectID = ($auditData.Target | Where-Object { $_.Type -eq 2 -and $_.ID -match '(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$' }).ID # several values are with Type=2 ('User_GUID', 'GUID', 'User', 'NetID') The good value is a GUID so we filter one the value which matches GUID
}
$object = [PSCustomObject]@{
TimeStamp = $timeStamp
ObjectId = $objectID
ObjectUserPrincipalName = ($auditData.Target | Where-Object { $_.Type -eq 5 }).Id # value is @{ID=xxx@domain; Type=5
RoleName = $auditData.modifiedproperties.newvalue[1]
Action = $auditData.Operation
Actor = $auditData.UserId
ActorIpAddress = $auditData.ActorIpAddress
}
$report.Add($object)
}
return $report
}
}