Skip to content

Instantly share code, notes, and snippets.

@star-crossed
Last active August 12, 2017 19:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save star-crossed/bf063a6364d7bbc82b3360c8f6ad24cb to your computer and use it in GitHub Desktop.
Save star-crossed/bf063a6364d7bbc82b3360c8f6ad24cb to your computer and use it in GitHub Desktop.
Recursively gets RoleAssignments for a SharePoint site and subsites
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, HelpMessage="This is the path to the CSV file.")]
[string]$CSVFile,
[Parameter(Mandatory=$true, HelpMessage="This is the URL to the SharePoint Online site.")]
[string]$Url,
[Parameter(Mandatory=$false, HelpMessage="This is the path to the DLLs for CSOM.")]
[string]$CSOMPath
)
Set-Strictmode -Version 1
If ($CSOMPath -eq $null -or $CSOMPath -eq "") { $CSOMPath = "." }
Add-Type -Path "$CSOMPath\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$CSOMPath\Microsoft.SharePoint.Client.Runtime.dll"
function getWeb ([Microsoft.SharePoint.Client.Web]$currentWeb) {
$context.Load($currentWeb)
$context.Load($currentWeb.RoleAssignments)
$context.Load($currentWeb.Webs)
$context.ExecuteQuery()
Write-Host $currentWeb.Title: $currentWeb.Url
$currentWeb.RoleAssignments | ForEach-Object {
$context.Load($_.Member)
$context.Load($_.RoleDefinitionBindings)
}
$context.ExecuteQuery()
$currentWeb.RoleAssignments | ForEach-Object {
$loginName = $_.Member.LoginName
$_.RoleDefinitionBindings | ForEach-Object {
$csvLine = New-Object -TypeName PSCustomObject -Property @{
'WebTitle'=$currentWeb.Title;
'WebUrl'=$currentWeb.Url;
'MemberLogin'=$loginName;
'RoleDefinition'=$_.Name;
}
Export-CSV -Append -NoTypeInformation -InputObject $csvLine -Path $CSVFile
}
}
$currentWeb.Webs | ForEach-Object {
getWeb($_)
}
}
$psCredentials = Get-Credential
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($psCredentials.UserName, $psCredentials.Password)
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = $spoCredentials
If ($context.ServerObjectIsNull.Value) {
Write-Error "Could not connect to SharePoint Online site collection: $Url"
} Else {
Write-Host "Connected to SharePoint Online site collection: " $Url -ForegroundColor Green
$web = $context.Web
getWeb($web)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment