使用 PowerShell 在 SharePoint Online 中获取用户有效权限
要求:使用 PowerShell 获得在 SharePoint Online 网站上向用户授予的有效权限。
PowerShell获取用户在SharePoint Online中的有效权限
以下是如何使用“GetUserEffectivePermissions”方法检索特定用户在给定 SharePoint 网站上拥有的所有有效权限。
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Parameters
$SiteUrl = "https://salaudeen.sharepoint.com/sites/Retail"
$AdminAccount = "Admin@salaudeen.com"
$UserName = "Steve@salaudeen.com"
Try {
# Connect to SharePoint Online
$password = Read-Host -Prompt "Enter password" -AsSecureString
$credential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminAccount, $password)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = $credential
#Get the site
$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()
#get the User
$User=$web.EnsureUser($UserName)
$ctx.Load($User)
$ctx.ExecuteQuery()
# Retrieve the user permissions on the site
$Permissions = $web.GetUserEffectivePermissions($user.LoginName)
$ctx.ExecuteQuery()
#get all base permissions granted to the user
$PermissionKindObj=New-Object Microsoft.SharePoint.Client.PermissionKind
$PermissionKindType=$PermissionKindObj.getType()
ForEach ($PermissionKind in [System.Enum]::GetValues($PermissionKindType))
{
$hasPermisssion = $permissions.Value.Has($PermissionKind)
if ($hasPermisssion)
{
Write-host $permissionKind.ToString()
}
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
此脚本获取给定站点上给定用户的所有权限,例如“AddListItems”、“EditListItems”、“DeleteListItems”等。
我们可以使用此方法来检查用户对任何 SharePoint 对象(例如网站、列表或库、文件夹、列表项或文件)的权限。
