-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #660 from reshmee011/main
cli for m365 version of the same sample for audit log using the office 365 activity feed
- Loading branch information
Showing
2 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,14 +37,14 @@ $logCollection = @() | |
while($days -ge $endDay){ | ||
if($days -eq 0) | ||
{ | ||
$activities = Get-PnPUnifiedAuditLog -ContentType SharePoint -ErrorAction Ignore | ||
$activities += Get-PnPUnifiedAuditLog -ContentType SharePoint -ErrorAction Ignore | ||
$activities += Get-PnPUnifiedAuditLog -ContentType AzureActiveDirectory -ErrorAction Ignore | ||
$activities += Get-PnPUnifiedAuditLog -ContentType DLP -ErrorAction Ignore | ||
$activities += Get-PnPUnifiedAuditLog -ContentType Exchange -ErrorAction Ignore | ||
$activities += Get-PnPUnifiedAuditLog -ContentType General -ErrorAction Ignore | ||
}else { | ||
$activities = Get-PnPUnifiedAuditLog -ContentType AzureActiveDirectory -ErrorAction Ignore -StartTime (Get-date).adddays(-$days) -EndTime (Get-date).adddays(-($days-1)) | ||
$activities += Get-PnPUnifiedAuditLog -ContentType AzureActiveDirectory -ErrorAction Ignore -StartTime (Get-date).adddays(-$days) -EndTime (Get-date).adddays(-($days-1)) | ||
$activities += Get-PnPUnifiedAuditLog -ContentType SharePoint -ErrorAction Ignore -StartTime (Get-date).adddays(-$days) -EndTime (Get-date).adddays(-($days-1)) | ||
$activities += Get-PnPUnifiedAuditLog -ContentType DLP -ErrorAction Ignore -StartTime (Get-date).adddays(-$days) -EndTime (Get-date).adddays(-($days-1)) | ||
$activities += Get-PnPUnifiedAuditLog -ContentType Exchange -ErrorAction Ignore -StartTime (Get-date).adddays(-$days) -EndTime (Get-date).adddays(-($days-1)) | ||
|
@@ -66,16 +66,73 @@ if($days -eq 0) | |
$logCollection | sort-object "Operation" |Export-CSV $OutPutView -Force -NoTypeInformation | ||
``` | ||
|
||
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] | ||
|
||
# [CLI for Microsoft 365](#tab/cli-m365-ps) | ||
|
||
|
||
```PowerShell | ||
$SiteUrl = "https://contoso.sharepoint.com/sites/test" | ||
$userId = "[email protected]" | ||
Write-Host "Ensure logged in" | ||
$m365Status = m365 status --output text | ||
if ($m365Status -eq "Logged Out") { | ||
Write-Host "Logging in the User!" | ||
m365 login --authType browser | ||
} | ||
$days = 3 | ||
$endDay = 0 | ||
$Operations = @() | ||
# Generate a unique log file name using today's date | ||
$dateTime = (Get-Date).toString("dd-MM-yyyy_HHmm") | ||
$invocation = (Get-Variable MyInvocation).Value | ||
$directorypath = Split-Path $invocation.MyCommand.Path | ||
$fileName = "logReport-" + $dateTime + ".csv" | ||
$OutPutView = $directorypath + "\Logs\"+ $fileName | ||
$logCollection = @() | ||
while($days -ge $endDay){ | ||
if($days -eq 0) | ||
{ | ||
$activities += m365 purview auditlog list --contentType SharePoint --output 'json' | ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType AzureActiveDirectory --output 'json' | ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType DLP --output 'json' | ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType Exchange --output 'json' | ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType General --output 'json' | ConvertFrom-Json | ||
}else { | ||
$activities += m365 purview auditlog list --contentType SharePoint --startTime ((Get-date).adddays(-$days) | Get-Date -uFormat '%Y-%m-%d') --endTime ((Get-date).adddays(-($days-1)) | Get-Date -uFormat '%Y-%m-%d') --output 'json' | ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType AzureActiveDirectory --startTime ((Get-date).adddays(-$days) | Get-Date -uFormat '%Y-%m-%d') --endTime ((Get-date).adddays(-($days-1)) | Get-Date -uFormat '%Y-%m-%d') --output 'json'| ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType DLP --startTime ((Get-date).adddays(-$days) | Get-Date -uFormat '%Y-%m-%d') --endTime ((Get-date).adddays(-($days-1)) | Get-Date -uFormat '%Y-%m-%d') --output 'json' | ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType Exchange --startTime ((Get-date).adddays(-$days) | Get-Date -uFormat '%Y-%m-%d') --endTime ((Get-date).adddays(-($days-1)) | Get-Date -uFormat '%Y-%m-%d') --output 'json' | ConvertFrom-Json | ||
$activities += m365 purview auditlog list --contentType General --startTime ((Get-date).adddays(-$days) | Get-Date -uFormat '%Y-%m-%d') --endTime ((Get-date).adddays(-($days-1)) | Get-Date -uFormat '%Y-%m-%d') --output 'json' | ConvertFrom-Json | ||
} | ||
if($activity.SiteUrl ){#-and $activity.SiteUrl | ||
if($activity.SiteUrl.ToLower() -eq $SiteUrl) #-$activity.UserId.ToLower() -eq $userId | ||
{ | ||
$logCollection += $activity | ||
} | ||
} | ||
$days = $days - 1 | ||
} | ||
$activities | sort-object "Operation" |Export-CSV $OutPutView -Force -NoTypeInformation | ||
``` | ||
|
||
[!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)] | ||
|
||
> [!Note] | ||
> SharePoint admin rights are required to run the script | ||
> SharePoint admin rights are required to run the script , | ||
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] | ||
> [!Note] | ||
> You may encounter error Error: The permission set (ActivityFeed.Read ServiceHealth.Read) sent in the request does not include the expected permission with contentType DLP and be mindful of the amount of data returned from a large tenant which may cause memory issues or lack of disk space to save the log file. | ||
*** | ||
|
||
## Source Credit | ||
|
||
Sample first appeared on [Unveiling Audit Logs with PnP PowerShell](https://reshmeeauckloo.com/posts/powershell-get-log-sharepoint-dlp-exchange-entra-pnpunifiedlog/) | ||
Sample first appeared on [Unveiling Audit Logs with PnP and Cli for M365 PowerShell](https://reshmeeauckloo.com/posts/powershell-get-log-sharepoint-dlp-exchange-entra-pnpunifiedlog/) | ||
|
||
## Contributors | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters