In this earlier article, We saw how to get the lists having unique permission using C#. In the same manner, let us see, how to get the unique permission lists using PowerShell.
The script is straight forward and does not require much explanation.
cls
#Import the required client dlls
Import-Module 'C:SATHISHPRACTICE SOURCE CODESOffice365.ConsolepackagesMicrosoft.SharePointOnline.CSOM.16.1.6420.1200libnet45Microsoft.SharePoint.Client.dll'
Import-Module 'C:SATHISHPRACTICE SOURCE CODESOffice365.ConsolepackagesMicrosoft.SharePointOnline.CSOM.16.1.6420.1200libnet45Microsoft.SharePoint.Client.Runtime.dll'
#Generic method to load the properties
Function Invoke-LoadMethod() {
param(
[Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),
[string]$PropertyName
)
$ctx = $Object.Context
$load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load")
$type = $Object.GetType()
$clientLoad = $load.MakeGenericMethod($type)
$Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
$Expression = [System.Linq.Expressions.Expression]::Lambda(
[System.Linq.Expressions.Expression]::Convert(
[System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),
[System.Object]
),
$($Parameter)
)
$ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
$ExpressionArray.SetValue($Expression, 0)
$clientLoad.Invoke($ctx,@($Object,$ExpressionArray))
}
#Mysite URL
$site = 'https://sppalsmvp.sharepoint.com/sites/DeveloperSite/'
#Admin User Principal Name
$admin = 'sathish@sppalsmvp.OnMicrosoft.Com'
#Get Password as secure String
$password = Read-Host 'Enter Password' -AsSecureString
#Get the Client Context and Bind the Site Collection
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
$context.Credentials = $credentials
$list = $context.Web.Lists.GetByTitle('D1')
$context.Load($list)
Invoke-LoadMethod -Object $list -PropertyName "HasUniqueRoleAssignments"
$context.ExecuteQuery()
Write-Host $list.HasUniqueRoleAssignments
Happy Coding,
Sathish Nadarajan.
Leave a comment