SharePoint Online:使用 PowerShell 从所有列表项中删除用户权限
要求:从列表中的所有项目权限中删除用户。
如何从 SharePoint Online 中的文档或项目中删除用户权限?
当用户不再需要访问文档库中的文档时,可能需要删除其权限。要从 SharePoint Online 中的文档中删除用户的权限,您可以按照以下步骤操作:
- 转到 SharePoint Online 网站并导航到存储文档的文档库。
- 右键单击该文档,然后从上下文菜单中选择“管理访问”。
在“管理访问”弹出窗口中,单击用户名旁边的小下拉菜单,然后选择“停止共享”。这将从特定文档中删除用户。
- 您还可以单击“管理访问”弹出窗口中的“高级”链接>>单击“停止继承权限”>>从用户列表中,选择要删除其权限的用户。
-
单击“删除用户权限”按钮即可删除用户。
- 单击“确定”确认操作。
- 单击“保存”按钮保存更改。
通过执行以下步骤,您可以从 SharePoint Online 中的特定文档中删除用户的权限。对每个文档手动执行此操作可能非常耗时,尤其是在有很多文档的情况下。让我们讨论使用 PowerShell 从 SharePoint Online 文档库中的所有文档中删除用户权限的步骤。
SharePoint Online:使用 PowerShell 删除所有项目的用户权限
SharePoint Online 列表中有多个项目具有特定用户有权访问的唯一权限。需要将特定用户从他有权访问的所有列表项中删除。以下是用于删除项目级权限的 SharePoint Online PowerShell:
#Load SharePoint Online 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"
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListName= "Migration Documents"
$UserAccount = "i:0#.f|membership|salaudeen@crescent.com"
#Get Credentials to connect
$Cred= Get-Credential
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
#Get the User
$User = $Context.Web.EnsureUser($UserAccount)
$Context.Load($User)
$Context.ExecuteQuery()
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit>2000</RowLimit></View>"
#Batch process list items - to mitigate list threshold issue on larger lists
Do {
#Get items from the list in batches
$ListItems = $List.GetItems($Query)
$Context.Load($ListItems)
$Context.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Loop through each List item
ForEach($ListItem in $ListItems)
{
$ListItem.Retrieve("HasUniqueRoleAssignments")
$Context.ExecuteQuery()
If($ListItem.HasUniqueRoleAssignments -eq $true)
{
#Get List Item Permissions
$Context.Load($ListItem.RoleAssignments)
$Context.ExecuteQuery()
Foreach($RoleAssignment in $ListItem.RoleAssignments)
{
$Context.Load($RoleAssignment.Member)
$Context.executeQuery()
#Is the current user is the user we search for?
If($RoleAssignment.Member.LoginName -eq $User.LoginName)
{
#Remove User from List Item Permissions
$ListItem.RoleAssignments.GetByPrincipal($User).DeleteObject()
$Context.ExecuteQuery()
Write-host -ForegroundColor Green ("User Removed from List Item Permissions ID {0} at {1}" -f $ListItem.ID,$ListItem["FileRef"])
}
}
}
}
$Context.ExecuteQuery()
} While ($Query.ListItemCollectionPosition -ne $null)
此 PowerShell 脚本将用户从该用户有权访问的所有项目中删除。
PnP PowerShell 从文档库中的所有文件和文件夹中删除用户
这次,让我们借助 PnP PowerShell 从所有文件和文件夹中删除用户(只要该用户有权限)。
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$ListName="Branding"
$UserAccount = "i:0#.f|membership|steve@Crescent.com"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the User
$User = Get-PnPUser -Identity $UserAccount -ErrorAction Stop
#Get all list items
$ListItems = Get-PnPListItem -List $ListName -PageSize 500 -Fields ID
$ItemCount = $ListItems.Count
#Iterate through each list item
$Counter=1
ForEach($ListItem in $ListItems)
{
#Display a progress bar
Write-Progress -PercentComplete ($Counter / $ItemCount * 100) -Activity "Processing Items from List:" -Status "Checking Item '$($ListItem.FieldValues.FileRef)' ($Counter of $ItemCount)"
#Check if the Item has unique permissions
$HasUniquePermissions = Get-PnPProperty -ClientObject $ListItem -Property "HasUniqueRoleAssignments"
If($HasUniquePermissions)
{
#Get Permissions Assigned to the Item
$RoleAssignments = Get-PnPProperty -ClientObject $ListItem -Property RoleAssignments
#Remove user from Item permissions - If Found!
[Bool]$UserFound = $false
ForEach($RoleAssignment in $RoleAssignments)
{
$Member = Get-PnPProperty -ClientObject $RoleAssignment -Property Member
If($Member.LoginName -eq $User.LoginName)
{
$UserFound = $True
$ListItem.RoleAssignments.GetByPrincipal($User).DeleteObject()
Invoke-PnPQuery
}
}
If($UserFound) { Write-host -f Green "Removed user from $($Listitem.FileSystemObjectType) at '$($ListItem.FieldValues.FileRef)' Permissions!" }
}
$Counter++
}
}
Catch {
write-host -f Red "Error Removing user from List Items:" $_.Exception.Message
}
此脚本连接到 SharePoint Online 网站。然后,它检索指定的列表或文档库及其所有项目。对于具有唯一权限的每个项目,脚本检查用户是否具有权限。如果是,脚本会删除用户对该项目的权限。通过使用本指南中的 PowerShell 脚本,您可以删除用户对 SharePoint Online 文档库中所有文档的权限。对于撤销不再需要的用户的访问权限,或者当您想要同时更改多个文档的权限时,这可能是一个有用的工具。


