powershellrecursionsharepoint-onlinebatching

Batch PnP PowerShell command in a recursive function


Currently in the nightly version of PnP.PowerShell, we can batch multiple PnP requests(mentioned below).

$batch = New-PnPBatch

1..100 | ForEach-Object{ Add-PnPListItem -List "ItemTest" -Values @{"Title"="Test Item Batched $_"} -Batch $batch }
Invoke-PnPBatch -Batch $batch

But if I need to batch the command from a recursive function how we can perform that? My requirement is to get Folders and Sub Folders in a Document Library. The code is given below.

Function GetFolders($folderUrl)
{
    $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder
    # Loop through the folders
    foreach($folder in $folderColl)
    {
        $newFolderURL= $folderUrl+"/"+$folder.Name
        Write-Host $folder.Name " - " $newFolderURL
        GetFolders($newFolderURL)
    }
}

GetFolders($FolderPath)

How I can make the above code to use Batching


Solution

  • According to the documentation: https://pnp.github.io/powershell/articles/batching.html

    Get-PnPFolderItem cmd doesnot support batching.

    enter image description here