0

I have created a power-shell script to list all the subsites and users who have full control to that individual subsite however it's coming up with an error - The collection has not been in itialized. It has not been requested or the request has not been executed. Can someone please have a look at my script? I need it to look like this:

URL Users dsff.com joe bloggs, ava who, nerida may gfhsdk.com tim ha, mary boo

My code is attached! Thanks, Clare

'$webs = Get-pnpsubwebs -recurse 
foreach ($web in $webs) {
    $users = $web.siteusers
    write-host "Processing " $web.url -foregroundcolor cyan

    Write-host "Site Owners" -foregroundcolor green
    foreach ($user in $users) {
    if ($web.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask)) {
        write-host $user.Name
    }
    write-host $web.Url
 }
 }`
0

Please check if the script given in this blog post helps you. Adding the same script here in case the blog hyperlink is not accessible.

param (  [Parameter(Mandatory=$True)]
         [string]$TenantName )

#Form a SharePoint Admin URl
$AdminURL = "https://$tenantName" + "-admin.sharepoint.com"

#Add SharePoint PowerShell Snap-in and Importing the SharePoint Online module
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
Import-Module ‘C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell’ -DisableNameChecking

#Connect to SharePoint Online
Connect-SPOService -Url $AdminURL -Credential (Get-Credential)

#Headers for the data
"Site URL`tOwner`tUsers`tGroup`tGroup Members" | Out-File -FilePath .\SharePointSiteOwners.csv 

#Get all Sites (Root sites only)
$sites = Get-SPOSite -Limit All

#Process all sites
foreach($site in $sites)
{
    $siteURL = $site.Url 
    $siteOwner = $site.Owner

    Write-Host $siteURL -ForegroundColor Cyan
    Write-Host $siteOwner -ForegroundColor Cyan


    #Get all users with Site Owner permissions
    $siteAdmins = Get-SPOUser -Site $siteURL -Limit All | select LoginName,IsSiteAdmin | ? { $_.ISSiteAdmin } 

    #Collect all site owners (user accounts) to a variable $siteUsers
    $siteUsers = ""
    foreach($siteAdmin in $siteAdmins) { $siteUsers += $siteAdmin.LoginName + " , " }


    Write-Host $siteUsers -ForegroundColor Cyan

    #Get all Groups from the site permissions
    $sitegroups = Get-SPOSiteGroup -Site $siteURL

    #Get Group info and members that have site owners permissions
    foreach ($sitegroup in $sitegroups)
    {
        $i = 0
        foreach($role in $sitegroup.Roles)
        {
            if ( $role.Contains("Site Owner") -or $role.Contains("Full Control") )
            {
                $i = $i + 1
                Write-Host $sitegroup.Title -ForegroundColor "Yellow"
                Write-Host $sitegroup.Users -ForegroundColor "Blue"

                if ($i -gt 1)
                {
                    "`t`t`t" + $sitegroup.Title + "`t" + $sitegroup.Users | Out-File -FilePath .\SharePointSiteOwners.csv -Append
                }
                else
                {
                    "$siteURL`t$siteOwner`t$siteUsers`t" + $sitegroup.Title + "`t" + $sitegroup.Users | Out-File -FilePath .\SharePointSiteOwners.csv -Append
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.