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

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

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

SharePoint Online:使用 PowerShell 更改组权限


要求: SharePoint Online 编辑组权限。

如何更新 SharePoint 组的权限?

在这篇博文中,我们将了解如何逐步更改 SharePoint Online 中的组的权限。我们还将向您展示如何使用 PowerShell 更改组权限,如果您需要快速授予或撤销一组用户的权限,这将非常有用。

要在 SharePoint Online 中编辑组权限,请执行以下步骤:

  1. 以管理员身份登录到您的 SharePoint Online 网站 >> 在网站集主页上,单击“设置”图标 >> 单击“网站设置”。
  2. 在“站点设置”页面上的“用户和权限”下,单击“站点权限”。
  3. 选中要更改权限的组的复选框(授予附加权限或撤销现有权限)。
  4. 单击功能区“修改”部分中的“编辑用户权限”按钮。

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

  5. 根据您的需要,在“编辑权限”页面上选择/取消选择组权限复选框。您可以勾选权限级别旁边的复选框,例如“贡献”以授予权限,或取消选中以从组中删除权限。

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

  6. 单击“确定”将权限更改保存到组。

就这样!我们已完成为 SharePoint Online 中的组分配权限级别。现在,让我们使用 PowerShell 编辑组权限。

SharePoint Online:使用 PowerShell 更改组权限级别

Set-SPOSiteGroup cmdlet 允许您修改网站集中现有 SharePoint Online 安全组的属性。例如,如果您可能希望编辑特定组的组权限,则需要使用此 cmdlet 来执行此操作。

您可以添加/删除网站集中某个组的权限:


#Variables for Admin Center & Site Collection URL
$AdminCenterURL = "https://Crescent-admin.sharepoint.com/"
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)

#sharepoint online change group permissions
Set-SPOSiteGroup -Site $SiteURL -Identity "Marketing Managers" -PermissionLevelsToRemove "Edit" -PermissionLevelsToAdd "Contribute"

此 PowerShell 脚本通过删除该组当前拥有的“编辑”权限并向其授予“贡献”权限,修改 SharePoint Online 网站集中名为“营销经理”的自定义安全组的权限级别。

用于更改 SharePoint Online 中的组权限的 PowerShell CSOM 脚本:

对于网站成员组,我们删除“编辑”权限并添加“贡献”权限。


#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"
$GroupName="Marketing Team Site Members"
$PermissionToRemove="Edit"
$PermissionToAdd="Contribute"

#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 groups of the site
    $Groups = $Ctx.Web.SiteGroups
    $Ctx.load($Groups)
    $Ctx.ExecuteQuery()
    
    #Get Group Names
    $GroupNames =  $Groups | Select -ExpandProperty Title
    
    #Check if the given group exists
    If($GroupNames -contains $GroupName)
    {
        #Get the Group
        $Group = $ctx.Web.SiteGroups.GetByName($GroupName)

        #Get Permission Levels to add and remove
        $RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
        $RoleDefToRemove = $Ctx.web.RoleDefinitions.GetByName($PermissionToRemove)
        
        #Get the Group's role assignment on the web
        $RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($Group)
        
        #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 Group permissions updated Successfully!"
    }
    else
    {
        Write-host -f Yellow "Group Doesn't exist!"
    }
}
Catch {
    write-host -f Red "Error Changing Group Permissions!" $_.Exception.Message
}

PnP PowerShell 更改 SharePoint Online 中的组权限

让我们添加“贡献”权限并从组中删除“编辑”权限:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/Sales"
$GroupName="Sales Portal Members"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Set Group Permissions: Remove "Edit" and Add "Contribute"
Set-PnPGroup -Identity $GroupName -AddRole "Contribute" -RemoveRole "Edit" 

您还可以使用 Set-PnPGroupPermissions 设置 SharePoint Online 组的权限:


#Connect to the Site
Connect-PnPOnline -Url "https://Crescent.sharepoint.com/sites/Purchase"

#Get the Associated - Default Members Group
$MembersGroup = Get-PnPGroup -AssociatedMemberGroup

#Change Group Permissions  - Replace Edit with Contribute
Set-PnPGroupPermissions -Identity $MembersGroup -RemoveRole "Edit" -AddRole "Contribute"

同样,要添加或删除 SharePoint 用户的权限,可以参考 SharePoint Online:使用 PowerShell 更改用户权限

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

取消回复欢迎 发表评论:

关灯