I have the following PnP Power shell, to get all the root folders of a SharePoint document library:-
#Parameters
$SiteURL="https://***.sharepoint.com/"
$FolderSiteRelativeURL = "Shared Documents"
#Connect to the Site collection
Connect-PnPOnline -URL $SiteURL -UseWebLogin
#Get the Folder from site relative URL
$Folder = Get-PnPFolder -Url $FolderSiteRelativeURL
#Get all Subfolders of a folder - recursively
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder -Recursive
ForEach($SubFolder in $SubFolders)
{
If($SubFolder.ListItemAllFields.HasUniqueRoleAssignments)
{
Write-host "Folder is already with broken permissions!" -f Yellow
}
Else
{
Write-host "Else is running" -f Yellow
#Break Folder permissions - keep all existing permissions & keep Item level permissions
$SubFolder.ListItemAllFields.BreakRoleInheritance($True,$True)
Invoke-PnPQuery
$RoleAssignments = $SubFolder.ListItemAllFields.RoleAssignments
ForEach($RoleAssignment in $RoleAssignments)
{
#RoleAssignment.Remove();
}
Write-host "Folder's Permission Inheritance is broken!!" -f Green
}
}
but i got this error:-
The collection has not been initialized. It has not been requested or the request has not been executed. It may need
to be explicitly requested.
At line:14 char:9
+ ForEach($RoleAssignment in $RoleAssignments)
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Folder's Permission Inheritance is broken!!
Else is running
The collection has not been initialized. It has not been requested or the request has not been executed. It may need
to be explicitly requested.
At line:14 char:9
+ ForEach($RoleAssignment in $RoleAssignments)
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Folder's Permission Inheritance is broken!!
Else is running
The collection has not been initialized. It has not been requested or the request has not been executed. It may need
to be explicitly requested.
At line:14 char:9
+ ForEach($RoleAssignment in $RoleAssignments)
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Folder's Permission Inheritance is broken!!
Else is running
Invoke-PnPQuery : Cannot invoke method or retrieve property from null object. Object returned by the following call
stack is null. "ListItemAllFields
"
At line:12 char:1
+ Invoke-PnPQuery
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-PnPQuery], ServerException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.ServerException,PnP.PowerShell.Commands.Base.InvokeQuery
The collection has not been initialized. It has not been requested or the request has not been executed. It may need
to be explicitly requested.
At line:14 char:9
+ ForEach($RoleAssignment in $RoleAssignments)
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Folder's Permission Inheritance is broken!!
Else is running
Invoke-PnPQuery : Cannot invoke method or retrieve property from null object. Object returned by the following call
stack is null. "ListItemAllFields
"
At line:12 char:1
+ Invoke-PnPQuery
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-PnPQuery], ServerException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.ServerException,PnP.PowerShell.Commands.Base.InvokeQuery
The collection has not been initialized. It has not been requested or the request has not been executed. It may need
to be explicitly requested.
At line:14 char:9
+ ForEach($RoleAssignment in $RoleAssignments)
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
any advice? Thanks