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

[玩转系统] SharePoint Online:使用 PowerShell 更改用户权限

作者:精品下载站 日期:2024-12-14 21:08:05 浏览:12 分类:玩电脑

SharePoint Online:使用 PowerShell 更改用户权限


要求: 在 SharePoint Online 中编辑用户权限。

如何更改 SharePoint Online 网站中的用户权限?

您想要更改用户对 SharePoint Online 中特定网站/库/项目的权限。例如,一旦特定项目完成,您不再希望团队成员添加或编辑项目的支持文档,而只是查看它们! SharePoint 提供了一种灵活的方式来管理用户或组权限更改。以下是如何在 SharePoint Online 中更新用户权限。

  1. 要编辑用户的网站权限,请导航到用户有权访问的 SharePoint Online 网站。单击“设置”齿轮,然后单击“站点设置”。
  2. 在“站点设置”页面上,单击“用户和权限”组下的“站点权限”链接。
  3. 在站点权限页面上,选中要编辑权限的用户旁边的复选框。单击功能区中的“编辑用户权限”按钮。

    [玩转系统] SharePoint Online:使用 PowerShell 更改用户权限

  4. 选择-取消选择相关权限复选框。在这种情况下,您必须取消选中“编辑”并勾选“阅读”。单击“确定”保存更改。

    [玩转系统] SharePoint Online:使用 PowerShell 更改用户权限

SharePoint Online 权限将默认为最高安全级别。例如。如果用户同时具有“编辑”和“读取”访问权限,SharePoint 会考虑“编辑”!

请注意,如果网站或库从父级继承权限,您可能必须停止继承权限才能为该项目提供唯一权限。

使用 PowerShell 在 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"
 
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/marketing"
$UserAccount="i:0#.f|membership|[email protected]"
$PermissionToRemove="Edit"
$PermissionToAdd="Read"

#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 all Users of the site
    $Users = $Ctx.Web.SiteUsers
    $Ctx.Load($Users)
    $Ctx.ExecuteQuery()
    
    #Get user accounts
    $UserAccounts =  $Users | Select -ExpandProperty LoginName

    #Check if the given user exists in the site
    If($UserAccounts -Contains $UserAccount)
    {
        #Get the User
        $User = $ctx.Web.SiteUsers.GetByLoginName($UserAccount)

        #Get Permission Levels to add and remove
        $RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
        $RoleDefToRemove = $Ctx.web.RoleDefinitions.GetByName($PermissionToRemove)
        
        #Get the User's role assignment on the web
        $RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($User)
        
        #Add/remove permission levels to the role assignment
        $RoleAssignment.RoleDefinitionBindings.Add($RoleDefToAdd)
        $RoleAssignment.RoleDefinitionBindings.Remove($RoleDefToRemove)
        $RoleAssignment.Update()
        $Ctx.ExecuteQuery() 

        write-host  -f Green "User permissions updated Successfully!"
    }
    else
    {
        Write-host -f Yellow "User Doesn't exist in the site!"
    }
}
Catch {
    write-host -f Red "Error Updating User Permissions!" $_.Exception.Message
} 

PnP PowerShell 更改列表上的用户权限

以下是用于删除“贡献”权限并向列表中的用户添加“读取”权限的 PnP PowerShell:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ListName = "Invoices"
$UserID = "[email protected]"
 
#Remove Contribution Role
Set-PnPListPermission -Identity $ListName -User $UserID -RemoveRole "Contribute"

#Grant Read Role
Set-PnPListPermission -Identity $ListName -User $UserID -AddRole "Read"

要使用 PowerShell 添加或删除 SharePoint Online 组的权限级别,请使用:使用 PowerShell 更改 SharePoint 组的权限级别

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

取消回复欢迎 发表评论:

关灯