Last active
August 12, 2017 19:42
-
-
Save star-crossed/bf063a6364d7bbc82b3360c8f6ad24cb to your computer and use it in GitHub Desktop.
Recursively gets RoleAssignments for a SharePoint site and subsites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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