Last active
March 29, 2024 17:03
-
-
Save joerodgers/46fc8ed370d7c1d4c40fda3807296981 to your computer and use it in GitHub Desktop.
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
#requires -modules "PnP.PowerShell" | |
function Get-ItemAnalytics | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter(mandatory=$true)] | |
[string] | |
$ItemDriveUrl, | |
[parameter(mandatory=$true,ParameterSetName="allTime")] | |
[switch] | |
$AllTime, | |
[parameter(mandatory=$true,ParameterSetName="lastSevenDays")] | |
[switch] | |
$LastSevenDays, | |
[parameter(mandatory=$true,ParameterSetName="interval")] | |
[DateTime] | |
$StartDate, | |
[parameter(mandatory=$true,ParameterSetName="interval")] | |
[DateTime] | |
$EndDate, | |
[parameter(mandatory=$true,ParameterSetName="interval")] | |
[ValidateSet("Hour", "Day", "Week", "Month")] | |
[string] | |
$Interval | |
) | |
begin | |
{ | |
$IntervalHours = switch( $Interval ) | |
{ | |
"Hour" { 1 } | |
"Day" { 24 } | |
"Week" { 168 } | |
"Month" { 720 } | |
} | |
} | |
process | |
{ | |
switch( $pscmdlet.ParameterSetName ) | |
{ | |
"allTime" | |
{ | |
$analyticsUrl = '{0}/analytics/allTime?$expand=activities($filter=access ne null)' -f $ItemDriveUrl | |
$analytics = Invoke-PnPSPRestMethod -Method Get -Url $analyticsUrl -ErrorAction Stop | |
} | |
"lastSevenDays" | |
{ | |
$analyticsUrl = '{0}/analytics/lastSevenDays?$expand=activities($filter=access ne null)' -f $ItemDriveUrl | |
$analytics = Invoke-PnPSPRestMethod -Method Get -Url $analyticsUrl -ErrorAction Stop | |
} | |
"interval" | |
{ | |
$analyticsUrl = '{0}/getActivitiesByInterval?startDateTime={1}&endDateTime={2}&interval={3}&$expand=activities($filter=access ne null)' -f $ItemDriveUrl, $StartDate.ToString("yyyy-MM-dd"), $EndDate.ToString("yyyy-MM-dd"), $IntervalHours | |
$analytics = Invoke-PnPSPRestMethod -Method Get -Url $analyticsUrl -ErrorAction Stop | Select-Object -ExpandProperty value | |
} | |
} | |
$analytics | Select-Object | |
@{Name='StartDateTime'; Expression={ $_.startDateTime }}, | |
@{Name='EndDateTime'; Expression={ $_.EndDateTime }}, | |
@{Name='ActorCount'; Expression={ $_.access.actorCount }}, | |
@{Name='ActionCount'; Expression={ $_.access.ActionCount }} | |
} | |
end | |
{ | |
} | |
} | |
# force a browser User-Agent value in PnP | |
[System.Environment]::SetEnvironmentVariable( "SharePointPnPUserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0", [System.EnvironmentVariableTarget]::Process ) | |
Connect-PnPOnline -Url 'https://contoso.sharepoint.com/sites/teamsite' -UseWebLogin -ForceAuthentication -ErrorAction Stop | |
$list = Get-PnPList -Identity "Documents" -ErrorAction Stop | |
$items = Get-PnPListItem -List $list -PageSize 5000 | |
$counter = 0 | |
$fileAnalytics = foreach( $item in $items ) | |
{ | |
$counter++ | |
Write-Host "[$(Get-Date)] - ($counter)/$($items.Count)) - Processing: $($item.FieldValues.FileRef)" | |
if( $item.FieldValues.FSObjType -ne [int]([Microsoft.SharePoint.Client.FileSystemObjectType]::File) ) | |
{ | |
continue | |
} | |
try | |
{ | |
$getSharingInformationUrl = "/_api/web/Lists('{0}')/GetItemById({1})/GetSharingInformation" -f $list.id, $item.Id | |
$listItemInformation = Invoke-PnPSPRestMethod -Method POST -Url $getSharingInformationUrl -Content "" -ErrorAction Stop | |
# $analytics = Get-ItemAnalytics -ItemDriveUrl $listItemInformation.itemUrl -AllTime | |
# $analytics = Get-ItemAnalytics -ItemDriveUrl $listItemInformation.itemUrl -LastSevenDays | |
$analytics = Get-ItemAnalytics -ItemDriveUrl $listItemInformation.itemUrl -StartDate ([DateTime]::Today.AddDays(-90)) -EndDate ([DateTime]::Today) -Interval Month | |
$analytics | Select-Object @{Name="File"; Expression={ $item.FieldValues.FileRef }}, * | |
} | |
catch | |
{ | |
Write-Host "Failed to retrieve analytics for $($item.FieldValues.FileRef). Error: $_" | |
} | |
} | |
$fileAnalytics |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment