|
#inspired from |
|
# https://www.sharepointdiary.com/2018/03/sharepoint-online-get-all-documents-using-powershell.html |
|
# https://www.sharepointdiary.com/2018/03/sharepoint-online-powershell-to-get-folder-in-document-library.html |
|
|
|
#Load SharePoint CSOM Assemblies |
|
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" |
|
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" |
|
|
|
#Function to Generate Report on all documents in a SharePoint Online Site Collection |
|
Function Get-SPODocumentInventory($SiteURL) |
|
{ |
|
Try { |
|
#Setup the context |
|
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) |
|
$Ctx.Credentials = $Credentials |
|
|
|
#Get the web from given URL |
|
$Web = $Ctx.web |
|
$Ctx.Load($Web) |
|
$Ctx.executeQuery() |
|
Write-host -f Yellow "Connected to Site: $SiteURL" |
|
|
|
#Get the Folder object by Server Relative URL |
|
$Root = $Web.GetFolderByServerRelativeUrl($ServerRelativeUrl) |
|
$Ctx.Load($Root) |
|
$Ctx.ExecuteQuery() |
|
Write-host -f Yellow "Got to directory : $ServerRelativeUrl" |
|
|
|
|
|
#Filter Document Libraries to Scan |
|
$Ctx.Load($Root.Folders) |
|
$Ctx.ExecuteQuery() |
|
Write-host -f Cyan "`t Loaded root directory $($Root.Name) with $($Root.ItemCount) Item(s)" |
|
#Write-Output $Root.Folders |
|
|
|
#$Lists = $Folder.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $SystemLists -notcontains $_.Title -and $_.ItemCount -gt 0} |
|
#Loop through each Folder |
|
Foreach ($Folder in $Root.Folders) |
|
{ |
|
Write-host -f Cyan "`t Processing Folder $($Folder.Name) with $($Folder.ItemCount) Item(s)" |
|
|
|
#Iterate through each file and get data |
|
$Ctx.Load($Folder.Files) |
|
$Ctx.ExecuteQuery() |
|
$DocumentInventory = @() |
|
Foreach($File in $Folder.Files) |
|
{ |
|
#Write-host "`t new File : $($File.Name)" |
|
|
|
$DocumentData = New-Object PSObject |
|
$DocumentData | Add-Member NoteProperty SiteURL($SiteURL) |
|
$DocumentData | Add-Member NoteProperty FileURL($File.ServerRelativeUrl) |
|
$DocumentData | Add-Member NoteProperty Name($File.Name) |
|
|
|
#Add the result to an Array |
|
$DocumentInventory += $DocumentData |
|
} |
|
#Export the result to CSV file |
|
$DocumentInventory | Export-CSV $ReportOutput -NoTypeInformation -Append |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
Catch { |
|
write-host -f Red "Error Generating Document Inventory!" $_.Exception.Message |
|
} |
|
} |
|
|
|
#Config Parameters |
|
#$SiteCollURL="https://crescent.sharepoint.com/sites/marketing" |
|
$SiteCollURL="https://colabor01.sharepoint.com/sites/Alimentations" |
|
$ServerRelativeUrl= "/sites/Alimentations/Documents%20partages/Mara%C3%AEchage/Stunden/Demande%20de%20cong%C3%A9%202023" |
|
$ReportOutput="C:\temp\DemandesConges-scans.csv" |
|
$BatchSize = 500 |
|
|
|
#Setup Credentials to connect |
|
$Cred= Get-Credential |
|
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) |
|
|
|
#Delete the Output Report, if exists |
|
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput } |
|
|
|
#Call the function |
|
Get-SPODocumentInventory $SiteCollURL |