I'm trying to create an array of all subsites in our tenant. I'm able to get the first level of subsites but not the next level in a recursive manner.
Here's my code so far:
Connect-PnpOnline -Url XXX -Credentials XXX
$allSubwebs = @()
$currentSubwebs = Get-PnPSubwebs
foreach ($currentSubweb in $currentSubWebs) {
$allSubwebs += $currentSubWeb.ServerRelativeUrl
}
So this gives me the first layer of sub sites. But I know that some sub sites contain sub sites themselves. Of course I could nest foreach() loops but for this I would have to know in advance how many layers of subsites there are.
So my question is how I can do this recursively, i.e. drill down and populate the array.
Thanks for your help.