[玩转系统] SharePoint Online:使用 PowerShell 删除列表中所有项目的唯一权限
作者:精品下载站 日期:2024-12-14 14:11:37 浏览:15 分类:玩电脑
SharePoint Online:使用 PowerShell 删除列表中所有项目的唯一权限
要求:重置 SharePoint Online 文档库的所有文档中的所有自定义权限。
默认情况下,在 SharePoint 的所有级别中,权限都是从其父级继承的。例如,子网站从其父网站集(或父网站)继承权限,列表和库从网站继承权限,列表中的项目从列表继承权限。因此,如果您在父级级别对权限进行任何更改,则下面的任何子级都会自动继承您在父级中所做的权限更改,除非子级使用自己的唯一权限。
有时,您可能必须在粒度级别设置唯一的权限,当然,您可能还必须重置损坏的权限。
如何删除 SharePoint 中的唯一权限?
要重置 SharePoint 列表项或文档的自定义权限,请执行以下步骤:
好的,现在权限已设置为从文档的父库继承。但是等等,选择每个单独的文档并重复这些步骤来删除唯一的权限是很乏味的,您不同意吗?因此,我编写了此 PowerShell 脚本来重置 SharePoint 列表中所有项目的中断继承。
如何摆脱 SharePoint Online 中的访问限制?有限访问意味着用户有权访问底层对象。例如。如果您在文档库级别为文档提供唯一权限,则特定用户将具有“有限访问权限”。它为用户提供有限的权限来访问他有权访问的项目。如果要删除受限访问权限,则需要删除用户有权访问的项目的唯一权限。
PowerShell 从 SharePoint Online 列表项中删除唯一权限:
您需要在客户端计算机上安装 SharePoint Online 客户端 SDK 才能使用此代码:https://www.microsoft.com/en-us/download/details.aspx?id=42038。以下是如何删除 SharePoint Online 列表项中的所有唯一权限
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Function to remove unique permissions and inherit from the parent
Function Remove-ListItemUniquePermissions
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ItemID
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Remove unique permissions and reset inheritance
$List=$Ctx.Web.Lists.GetByTitle($ListName)
$ListItem=$List.GetItemByID($ItemID)
$ListItem.ResetRoleInheritance()
$Ctx.ExecuteQuery()
Write-Host "Unique Permissions are removed and inherited from the Parent!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Deleting Unique Permissions!" $_.Exception.Message
}
}
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="25"
#Call the function to remove unique permissions from a list
Remove-ListItemUniquePermissions -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID
用于删除 SharePoint Online 中所有列表项的唯一权限的 PowerShell 脚本
以下是用于重置唯一权限的 SharePoint Online PowerShell:
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName= "Documents"
$UserName= "[email protected]"
$Password ="Password goes here"
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
#Get All List Items
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$ListItems = $List.GetItems($Query)
$Context.Load($ListItems)
$Context.ExecuteQuery()
Write-host "Total Items Found:"$ListItems.Count
#Iterate through each list item
$ListItems | foreach {
#Delete Unique Permission
$_.ResetRoleInheritance()
}
$Context.ExecuteQuery()
Write-host "Broken Permissions are Deleted on All Items!" -ForegroundColor Green
我们可以对上面的脚本进行两处改进:
- 上面的脚本不能处理包含超过 5000 个项目的大型列表。
- 上面的脚本只是重置继承,而不检查列表项是否具有唯一权限。
那么,让我们解决上述问题,这是更新后的脚本:
使用 PowerShell 删除 SharePoint Online 中的独特权限
这是用于重置大型列表或文档库(超过 5000 个项目!)中的唯一权限的 PowerShell
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com"
$ListName= "Documents"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $Credentials
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
$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)
{
#Reset Permission Inheritance
$ListItem.ResetRoleInheritance()
Write-host -ForegroundColor Yellow "Inheritence Restored on Item:" $ListItem.ID
}
}
$Context.ExecuteQuery()
} While ($Query.ListItemCollectionPosition -ne $null)
Write-host "Broken Permissions are Deleted on All Items!" -ForegroundColor Green
PnP PowerShell 删除列表中所有项目的唯一权限
要重置列表项的权限,我们可以使用带有“InheritPermissions”开关的 Set-PnPListItemPermission cmdlet。
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get all list items in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 500
#Iterate through each list item
ForEach($ListItem in $ListItems)
{
#Check if the Item has unique permissions
$HasUniquePermissions = Get-PnPProperty -ClientObject $ListItem -Property "HasUniqueRoleAssignments"
If($HasUniquePermissions)
{
$Msg = "Deleting Unique Permissions on {0} '{1}' at {2} " -f $ListItem.FileSystemObjectType,$ListItem.FieldValues["FileLeafRef"],$ListItem.FieldValues["FileRef"]
Write-host $Msg
#Delete unique permissions on the list item
Set-PnPListItemPermission -List $ListName -Identity $ListItem.ID -InheritPermissions
}
}
使用 PnP PowerShell 重置文件夹中所有文件的唯一权限
这次,我们使用 PnP PowerShell 重置 SharePoint Online 文件夹中存储的所有文件的唯一权限:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$FolderServerRelativeURL = "/sites/Marketing/Branding/2018"
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Folder from given URL
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields.ParentList
$ParentList = $Folder.ListItemAllFields.ParentList.Title
#Get All Files from the Folder
$Files = Get-PnPListItem -List $ParentList -FolderServerRelativeUrl $Folder.ServerRelativeUrl | Where {$_.FileSystemObjectType -eq "File"}
#Traverse through each file in the folder
ForEach ($File in $Files)
{
#Check If File has Unique Permissions
$HasUniquePermissions = Get-PnPProperty -ClientObject $File -Property HasUniqueRoleAssignments
If($HasUniquePermissions)
{
#Reset Broken Inheritance
$File.ResetRoleInheritance()
$File.update()
Invoke-PnPQuery
Write-Host "Reset Unique Permissions on File $($File.FieldValues["FileRef"])" -ForegroundColor Green
}
}
同样,您可以使用 PowerShell 重置 SharePoint Online 网站、列表和文件夹中的唯一权限,如下所示:
- SharePoint Online:使用 PowerShell 从列表或文档库中删除唯一权限
- SharePoint Online:使用 PowerShell 删除文档库中所有文件夹的唯一权限
- SharePoint Online:使用 PowerShell 重置网站集的所有唯一权限
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag