forked from 12Knocksinna/Office365itpros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindWhoCreatedGuestsThroughSPOSharing.ps1
29 lines (29 loc) · 1.73 KB
/
FindWhoCreatedGuestsThroughSPOSharing.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
# A script to find who created new guest accounts in an Office 365 tenant through SharePoint Online sharing invitations
$EndDate = (Get-Date).AddDays(1); $StartDate = (Get-Date).AddDays(-90); $NewGuests = 0
$Records = (Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations "SharingInvitationCreated" -ResultSize 2000 -Formatted)
If ($Records.Count -eq 0) {
Write-Host "No Sharing Invitations records found." }
Else {
Write-Host "Processing" $Records.Count "audit records..."
$Report = @()
ForEach ($Rec in $Records) {
$AuditData = ConvertFrom-Json $Rec.Auditdata
# Only process the additions of guest users to groups
If ($AuditData.TargetUserOrGroupName -Like "*#EXT#*") {
$TimeStamp = Get-Date $Rec.CreationDate -format g
# Try and find the timestamp when the invitation for the Guest user account was accepted from AAD object
Try {$AADCheck = (Get-Date(Get-AzureADUser -ObjectId $AuditData.TargetUserOrGroupName).RefreshTokensValidFromDateTime -format g) }
Catch {Write-Host "Azure Active Directory record for" $AuditData.UserId "no longer exists" }
If ($TimeStamp -eq $AADCheck) { # It's a new record, so let's write it out
$NewGuests++
$ReportLine = [PSCustomObject][Ordered]@{
TimeStamp = $TimeStamp
InvitingUser = $AuditData.UserId
Action = $AuditData.Operation
URL = $AuditData.ObjectId
Site = $AuditData.SiteUrl
Document = $AuditData.SourceFileName
Guest = $AuditData.TargetUserOrGroupName }
$Report += $ReportLine }}
}}
$Report | Format-Table TimeStamp, Guest, Document -AutoSize