Skip to content

Instantly share code, notes, and snippets.

@pmatthews05
Last active April 6, 2021 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmatthews05/19888fbcb161fd36b5d83c95c2fb8815 to your computer and use it in GitHub Desktop.
Save pmatthews05/19888fbcb161fd36b5d83c95c2fb8815 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Need to use PowerShell version 7.1
PNP needs to be above version 1.4
Need to use the new PNP.PowerShell "Install-Module PnP.Powershell"
Loops through Library obtaining all items that were modified either before or after a given date.
You need to connect to the site first using PNP
Connect-PnPOnline -url:https://[tenant].sharepoint.com/sites/[siteURL] -PnPManagementShell
.EXAMPLE
To get all Files/Folder and pump to a CSV.
.\Get-AllItemsMetadata.ps1 -ListName:"Documents" | Export-csv .\filetosave.csv -Encoding:UTF8 -NoTypeInformation
.EXAMPLE
To get all Files only.
.\Get-AllItemsMetadata.ps1 -ListName:"Documents" -IncludeFolders:$false
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[string]$ListName,
[bool]$IncludeFolders = $true,
[bool]$Recursive = $true
)
function Get-ListDataAsStream {
param (
[Parameter(Mandatory)]
[string]
$SiteUrl,
[Parameter(Mandatory)]
[string]
$ListName,
[Parameter(Mandatory)]
[string]
$CAMLQuery,
[Parameter(Mandatory = $false)]
[string]
$NextLink
)
$QueryUrl = "$SiteUrl/_api/web/lists/GetbyTitle('$ListName')/RenderListDataAsStream"
Write-Information -MessageData:"QueryUrl:$QueryUrl"
if ($NextLink -and $NextLink.StartsWith('?')) {
$NextLink = $NextLink.Substring(1)
Write-Information -MessageData:"Paging:$NextLink"
}
$body = @{
parameters = @{
Paging = $NextLink
RenderOptions = 2
ViewXml = $CAMLQuery
}
}
return Invoke-PnPSPRestMethod -Url:"$QueryUrl" -Method:Post -ContentType:"application/json;odata=nometadata" -Content:$body
}
function Get-CAMLQuery {
param(
[Parameter(Mandatory)]
[Boolean]
$IncludeFolders,
[Parameter(Mandatory)]
[Boolean]
$Recursive
)
if ($Recursive) {
if ($IncludeFolders) {
$scope = "Scope='RecursiveAll'"
}
else {
$scope = "Scope='Recursive'"
}
}
$CAMLQuery = "<View $Scope>
<Query>
<OrderBy>
<FieldRef Name='Modified' Ascending='TRUE'/>
</OrderBy>
</Query>
<RowLimit Paged='TRUE'>2000</RowLimit>
</View>"
return $CAMLQuery
}
function Get-OutputInformation {
param(
[Parameter(Mandatory)]
[string]
$SiteUrl,
[Parameter(Mandatory)]
[string]
$ListTitle,
[Parameter(Mandatory)]
[object]
$ItemArray
)
Write-Information -MessageData "Processing $($ItemArray.Count) items..."
$ItemArray | ForEach-Object {
#Here you would loop round ItemArray and grab what you want.
Write-Output $PSItem
}
}
$ErrorActionPreference = 'Stop'
$InformationPreference = 'Continue'
$WarningPreference = 'Continue'
$library = Get-PnPList -Identity:$ListName -Includes RootFolder
if ($null -eq $library) {
throw "Library does not exist"
}
$nextLink = ""
$web = Get-PnPWeb
$webUrl = $web.url
$listTitle = $library.Title
$CAMLQuery = get-CAMLQuery -IncludeFolders:$IncludeFolders -Recursive:$Recursive
$startTime = Get-Date
$count = 0;
do {
$results = Get-ListDataAsStream -SiteUrl:$webUrl -ListName:$ListName -CAMLQuery:$CAMLQuery -NextLink:$nextLink
$count += $($results.Row.Count)
Get-OutputInformation -SiteUrl:$webServerRelatedUrl -ListTitle:$listTitle -ItemArray:$results.Row
$nextLink = ""
if ($results.NextHref) {
$nextLink = $results.NextHref
}
}while ("" -ne $nextLink)
$endTime = Get-Date
$timeTaken = New-TimeSpan -Start $startTime -End $endTime
Write-Information -MessageData:"Time taken to process $count items $($timeTaken -f 'c') "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment