Powershell foreach loop is not reaching the end of the array it is given

Viewed 39

Good afternoon Stack Overflow,

I have a powershell script written to move files from a large SharePoint document library into folders based on the file metadata for allowance of security controls (as it was missed before I did the migrations to SharePoint).

Currently the script terminates after the first 2500 items (roughly) when I need it to process 220,000 items, expecting it to move roughly half of those. I've checked and the $items variable does contain the entire 220k that I'm expecting but for some reason it terminates earlier than I want it to.

If anyone can spy why, or can recommend best practice to ensure the loop completes (or just in general as my code is often a bodge job being a self taught beginner) it would be much appreciated.

I have left out the full URL but it is present in the script.


$source = "HR_STAFF"
$targetHR = "HR_STAFF/School Section - Human Resources"
$targetHRContract = "HR_STAFF/Human Resources - Contractual"
$targetContract = "HR_STAFF/Category - Contractual"
Connect-PnPOnline -Interactive -Url $SiteURL

$items  = Get-PnPListItem -List $source -PageSize 1000

foreach ($item in $items)
{
    [System.GC]::Collect()
    
    if ($item.FieldValues.SER_SchoolSection -eq "Human Resources" -and $item.FieldValues.SER_Category -eq "Contractual") {
        $sourcefile = $item.FieldValues.FileRef
        $targetfile = $targetHRContract + "/" + $item.FieldValues.FileLeafRef
        Move-PnPFile -SourceUrl $sourcefile -TargetUrl $targetfile -Force
        Set-PnPListItem -Identity $item -Values @{SER_IssueMonth = $item.FieldValues.SER_Modified; Modified = $item.FieldValues.SER_IssueDate; Created = $Item.FieldValues.Created} -UpdateType SystemUpdate
    }
    if ($item.FieldValues.SER_SchoolSection -eq "Human Resources"  ) {
        $sourcefile = $item.FieldValues.FileRef
        $targetfile = $targetHR + "/" + $item.FieldValues.FileLeafRef
        Move-PnPFile -SourceUrl $sourcefile -TargetUrl $targetfile -Force
        Set-PnPListItem -Identity $item -Values @{SER_IssueMonth = $item.FieldValues.SER_Modified; Modified = $item.FieldValues.SER_IssueDate; Created = $Item.FieldValues.Created} -UpdateType SystemUpdate
    }
    if ($item.FieldValues.SER_Category -eq "Contractual"  ) {
        $sourcefile = $item.FieldValues.FileRef
        $targetfile = $targetContract + "/" + $item.FieldValues.FileLeafRef
        Move-PnPFile -SourceUrl $sourcefile -TargetUrl $targetfile -Force
        Set-PnPListItem -Identity $item -Values @{SER_IssueMonth = $item.FieldValues.SER_Modified; Modified = $item.FieldValues.SER_IssueDate; Created = $Item.FieldValues.Created} -UpdateType SystemUpdate
    }
}

0 Answers
Related