1

tl;dr version: Ultimately I can get a list of all Groups for a given Site Collection, but I can't tell which groups give access to which sites. Which is to say that, for a given subsite that has broken permission inheritance, I can't figure out how to get the names of that subsite's Groups (without getting the names of every other group in the site collection).


Hi folks, I'm trying to write a PowerShell script/function that connects to a SharePoint Online farm, iterates through each Site/SubSite/Etc and outputs the following for each:

URL to (sub)Site, Title of (Sub)Site , SharePoint Group Name , Group Access Level (Owner, Member, Visitor or similar)

When I use the function below to get the groups of a given site, it returns the groups for the entire site collection rather than just the groups for the subsite (that has broken inheritance). What I need is to just get the groups for just the subsite (url) passed to it.

function Get-Groups(){
param(
$Url = $(throw "Please provide a Site Collection or Sub-site Url who's groups you want to get."),
$Credential = $(throw "Please provide a Credentials")
)
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)  
    $context.Credentials = $Credential 
    $web = $context.Web
    $groups = $web.SiteGroups
    $context.Load($groups)
    $context.ExecuteQuery()
    return $groups
}

I've also tried using using permissions in the following code snippet to accomplish this, but I can't seem to get anything for web1.permissions (i.e. it's always null).

$web = Get-SPOWebs -Url 'urlOFMySite' -Credentials $credentialsDefineEarlier

foreach ($web1 in $web)
{
    #$webPerms = Get-WebPerms -Url $web.Url -Credential $SPOCredentials

    if (($web1.permissions -ne $null) -and ($web.hasuniqueroleassignments -eq "True"))
    {
    Write-Host "-----"
    Write-Host "Displaying site permissions for:  " $web1.title 
    Write-Host $web1.permissions | fl member, basepermissions
    }
    elseif ($web1.hasuniqueroleassignments -eq "False")
    {
    Write-Host "-----" 
    Write-Host "Displaying site permissions for: " $web1.title
    Write-Host $web1.title "inherits permissions from " $site.title
    }
}

function Get-SPOWebs(){
param(
    $Url = $(throw "Please provide a Site Collection Url"),
    $Credential = $(throw "Please provide a Credentials")
)
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)  
    $context.Credentials = $Credential 
    $web = $context.Web
    $context.Load($web)
    $context.Load($web.Webs)
    $context.ExecuteQuery()
    foreach($web in $web.Webs)
    {
        Write-Host "Outputing permsisions: " $web.permissions
        Get-SPOWebs -Url $web.Url -Credential $Credential 
        $web
    }
}

The Get-WebPerms (that I've tried to write) function completely doesn't work, so I've not included it here.

I've been banging my head against a wall (not literally) for a good 6 or 7 hours now with little to no progress. Any solutions/help anyone can provide would be very much appreciated. Thanks!

Your Answer

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

Browse other questions tagged or ask your own question.