如何使用 PnP Powershell 删除所有列表项权限

How to remove all list item permissions using PnP Powershell

我想使用 PnP Powershell 删除列表项的所有权限

我试过这个命令:

Set-PnPListItemPermission -Identity $item.id -User 'user@contoso.com' -AddRole "Contribute"

但是用户 运行 script/command 也添加了完全控制权限。

是否有任何其他方法可以使用 PnP Powershell 删除列表项的所有现有权限?

谢谢

我测试了在 PnP PowerShell 中使用读取权限用户连接到 SharePoint Online 网站,然后 运行 Set-PnPListItemPermission 命令,它会抛出拒绝访问错误,而不是添加完全控制权限:

总而言之,要为列表项设置权限,需要运行正在执行脚本的用户在站点级别拥有完全控制权限。否则,将抛出拒​​绝访问错误。

完全控制权限应与站点组一起应用,在列表中,尝试打破权限继承并删除组:

# Provide credentials over here
$creds = (New-Object System.Management.Automation.PSCredential "<<UserName>>",(ConvertTo-SecureString "<<Password>>" -AsPlainText -Force))
 
# Provide URL of the Site over here
# If you do not wish to pass credentials hard coded then you can use: -Credentials (Get-Credential). This will prompt to enter credentials
Connect-PnPOnline -Url http://MyServer/sites/MySiteCollection -Credentials $creds
 
# Get Context
$clientContext = Get-PnPContext
 
$targetWeb = Get-PnPWeb
 
# Get the list object
$targetList = $targetWeb.Lists.GetByTitle("List Name")
 
# Load List object
$clientContext.Load($targetList)
$clientContext.ExecuteQuery()
 
# This method will work only if the role inheritence is broken(list has unique role assignments) on the list
$targetList.RoleAssignments.Groups.RemoveByLoginName("test Visitors")
 
$clientContext.ExecuteQuery()
 
Disconnect-PnPOnline