I am trying to retrieve all documents from SharePoint online sites and export them to their own CSV file. The code works fine when explicitly stating the site URL in the original parameters. However it does not work when I try to import a list of sites from a CSV file and run it using a nested foreach statement. It does not process through the #Iterate through each site foreach statement.
I want to loop through all the sites in the CSV file, output all the documents and export the results to individual files using the Name column in the CSV as its filename.
Any help would be appreciate, thanks.
Original:
#Parameters
$SiteURL = "https://org.sharepoint.com/sites/TestSite1"
$ListName= "Documents"
$ReportOutput = "C:\Temp\TestSite1.csv"
$Pagesize = 2000
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
Modified: added #Iterate through each site
CSV input file is
Name URL
Site1 https://org.sharepoint.com/sites/Site2
Site2 https://org.sharepoint.com/sites/Site2
#Parameters
$SP = "https://org.sharepoint.com"
$SiteList = Import-Csv -Path "\Sites2-test.csv"
$SitePath = $SiteList.URL
$FileName = $SiteList.Name
$ListName= "Documents"
$Pagesize = 2000
$ReportOutput = "C:\Temp\Site-AllDocs_"+$FileName+".csv"
#Connect to SharePoint Online site
Connect-PnPOnline $SP -Interactive
#Iterate through each site
ForEach ($Site in $SitePath)
{
#Array to store results
$Results = @()
#Get all Documents from the document library
$List = Get-PnPList -Identity $ListName
$global:counter = 0;
$ListItems = Get-PnPListItem -List $ListName -PageSize $Pagesize -Fields Author, Editor, Created, File_x0020_Type -ScriptBlock `
{ Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
"Getting Documents from Library '$($List.Title)'" -Status "Getting Documents data $global:Counter of $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
$ItemCounter = 0
#Iterate through each item
Foreach ($Item in $ListItems)
{
$Results += New-Object PSObject -Property ([ordered]@{
Name = $Item["FileLeafRef"]
Type = $Item.FileSystemObjectType
FileType = $Item["File_x0020_Type"]
RelativeURL = $Item["FileRef"]
CreatedByEmail = $Item["Author"].Email
CreatedOn = $Item["Created"]
Modified = $Item["Modified"]
ModifiedByEmail = $Item["Editor"].Email
})
$ItemCounter++
Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Exporting data from Documents $ItemCounter of $($List.ItemCount)" -Status "Exporting Data from Document '$($Item['FileLeafRef'])"
}
#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
}
Write-host "Document Library Inventory Exported to CSV Successfully!"