当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 从列表权限中删除用户或组

作者:精品下载站 日期:2024-12-14 14:58:18 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 从列表权限中删除用户或组


要求:使用 PowerShell 从 SharePoint Online 中的列表权限中删除用户或组。

如何从 SharePoint Online 列表中删除用户权限?

您是否想要从 SharePoint Online 列表权限中删除用户或组?也许您需要快速撤消用户或组对 SharePoint Online 列表或文档库的访问权限。您可以使用 SharePoint Online UI 或 PowerShell 来执行此操作。在这篇博文中,我们将引导您完成使用这两种方法从 SharePoint Online 列表中删除用户或组的步骤。

要从 SharePoint Online 列表权限中删除用户或组,请执行以下步骤:

  1. 导航到您的 SharePoint Online 列表或库。
  2. 单击“设置”齿轮 >> 选择“列表设置”。
  3. 在列表设置页面中,单击“权限和管理”组下的“此列表的权限”。
  4. 如果列表未使用唯一权限,请单击“停止继承权限”按钮并确认提示。

    [玩转系统] SharePoint Online:使用 PowerShell 从列表权限中删除用户或组

  5. 在用户和组列表中找到该用户,然后单击其名称旁边的复选框。然后,您可以通过单击功能区中的“删除用户权限”按钮从列表权限中删除用户或组。

PowerShell 从 SharePoint Online 的列表权限中删除用户

以下是用于删除权限的 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"

#Remove User from List Permissions
Function Remove-SPOUserFromListPermission($SiteURL,$ListName,$UserAccount)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred
     
        #Get the List
        $List=$Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()
        
        #Get the User
        $User = $ctx.Web.EnsureUser($UserAccount) 
        $Ctx.Load($User)
        $Ctx.ExecuteQuery()
 
        #Break Permission Inheritance
        $List.BreakRoleInheritance($true, $false)
        $Ctx.ExecuteQuery()

        #Get List Permissions
        $Ctx.Load($List.RoleAssignments)
        $ctx.ExecuteQuery()
        
        #Remove Group from List Permissions
        $List.RoleAssignments.GetByPrincipal($User).DeleteObject()
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "User '$UserAccount' has been Removed from List '$ListName'"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sales"
$ListName = "Documents"
$UserAccount = "[email protected]"
 
#Call the function to Remove user from List Permissions
Remove-SPOUserFromListPermission -SiteURL $SiteURL -ListName $ListName -UserAccount $UserAccount

PnP PowerShell 从列表权限中删除用户:

让我们从列表的权限中删除用户:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/Sales"
$ListName ="Documents"
$UserID= "i:0#.f|membership|[email protected]"

#Connect PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Context = Get-PnPContext
 
#Get the list & User objects
$List = Get-PnPList -Identity $ListName
$User = Get-PnPUser -Identity $UserID

#Break Permission Inheritance
Set-PnPList -Identity $ListName -BreakRoleInheritance -CopyRoleAssignments

#Remove User from List Permissions
$List.RoleAssignments.GetByPrincipal($User).DeleteObject()
$Context.ExecuteQuery()

SharePoint Online:使用 PowerShell CSOM 从列表权限中删除组

同样,要从列表中删除 SharePoint 组,请使用以下 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"

#Remove Group from List Permissions
Function Remove-SPOGroupFromListPermission($SiteURL,$ListName,$GroupName)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred
     
        #Get the List
        $List=$Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()
        
        #Get the Group
        $Group = $Ctx.Web.SiteGroups.GetByName($GroupName) 
        $Ctx.Load($Group)
        $Ctx.ExecuteQuery()
 
        #Break Permission Inheritance
        $List.BreakRoleInheritance($true, $false)
        $Ctx.ExecuteQuery()

        #Get List Permissions
        $Ctx.Load($List.RoleAssignments)
        $ctx.ExecuteQuery()
        
        #Remove Group from List Permissions
        $List.RoleAssignments.GetByPrincipal($Group).DeleteObject()
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "Group '$GroupName' has been Removed from List '$ListName'"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sales"
$ListName = "Documents"
$GroupName = "Sales Portal Members"
 
#Call the function to Remove group from SharePoint List permissions
Remove-SPOGroupFromListPermission -SiteURL $SiteURL -ListName $ListName -GroupName $GroupName

PnP PowerShell 从列表权限中删除组

以下是用于从列表权限中删除 SharePoint 组的 PnP PowerShell:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/Sales"
$ListName ="Documents"
$GroupName= "Sales Portal Members"

#Connect PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Context = Get-PnPContext
 
#Get the list
$List = Get-PnPList -Identity $ListName
$Group = Get-PnPGroup -Identity $GroupName

#Break Permission Inheritance
Set-PnPList -Identity $ListName -BreakRoleInheritance -CopyRoleAssignments

#sharepoint online powershell remove group permissions
$List.RoleAssignments.GetByPrincipal($Group).DeleteObject()
$Context.ExecuteQuery()

通过执行这些步骤,您可以从 SharePoint Online 的列表或文档库中删除用户或组。这可能是撤销用户权限并限制对列表或库的访问的有用方法。将用户或组添加到 SharePoint Online 列表或库权限:SharePoint Online:使用 PowerShell 授予列表或库权限

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯