I need to get a list of all files, older then 5 years, from few thousands sharepoint online sites and I am trying to do it with powershell script. The problem I am having is that I have to reconnect with Disconnect/Connect-PnPOnline for each site which causes server to stop responding after few hundreds sites.
foreach($site in $sites){
$surl = $site.SiteUrl ; $sid = $site.ID
$gcon = Connect-PnPOnline -Url $surl -ClientId $GLClientId -ClientSecret $GLSecret -ReturnConnection # Connection via Global SP app to read current site
Get-PnPListItem -List $folder -PageSize 500 -Connection $gcon | ForEach-Object{
if($_.FileSystemObjectType -like "*File*"){ $f=$_;
if($t_TooOldDate -gt $f.FieldValues.Last_x0020_Modified){ ###
$a_files+=[pscustomobject]@{'File'=$f.FieldValues.FileRef; 'Size'= $f.FieldValues.File_x0020_Size ; 'LastModified'=$f.FieldValues.Last_x0020_Modified}
$co++ ; $ts+= $f.FieldValues.File_x0020_Size
}
}
}
Disconnect-PnPOnline -Connection $gcon
}
So I am wandering if I could use same connection for the whole script and just keep changing site name? Or maybe there is a better way to do it?
Thanks
Connect-PnPOnline
again.