I'm trying to write a powershell script to retrieve all site collections, their groups and subsites from our SharePoint Online tenancy. So far I've been able to get all sites, their groups and the users in each group. I'm just having trouble getting the subsites and identifying any lists with unique permissions (and retrieving those permissions hopefully!)
So far I have this to get the lists:
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("username", $password)
$siteURL = "https://site/sites/test"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = $credentials
$web = $ctx.Web
$lists = $web.Lists
$ctx.Load($lists)
$ctx.ExecuteQuery()
foreach($list in $lists)
{
Write-Host $list.Title
}
and this to get site groups and users:
Connect-SPOService –url https://site-admin.sharepoint.com
$sites = Get-SPOSite -Detailed
foreach ($site in $sites)
{
Write-Host $site.Title
$siteGroups = Get-SPOSiteGroup -Site $site.Url
foreach ($group in $siteGroups)
{
$users = Get-SPOUser -Site $site.Url -Group $group.Title -Limit All |ft -wrap
$url = $site.Url
$groupName = $group.Title
Write-Host $groupName + ' ' + $group.Users
}
}
So for the lists I tried using $list.HasUniqueRoleAssignments to determine if the list returned has unique permissions but it always seems to return false even if there are unique permissions. For the subsites I can get a count of the subsites, but how do I get the URL?
Thanks in advance!
EDIT: So I've been able to get the URL of the subsites using the client context code I used to get the lists:
$subs = $web.Webs
$ctx.Load($subs)
$ctx.ExecuteQuery()
foreach($sub in $subs)
{
Write-Host $sub.Url
}
Still stuck with getting permissions though...