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

[玩转系统] 使用 PowerShell 将 AD 组添加到 SharePoint Online 组

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

使用 PowerShell 将 AD 组添加到 SharePoint Online 组


要求:使用 PowerShell 将 Active Directory 安全组添加到 SharePoint Online 组。

[玩转系统] 使用 PowerShell 将 AD 组添加到 SharePoint Online 组

如何将 AD 组添加到 SharePoint Online 组?

将 Active Directory (AD) 组添加到 SharePoint Online 是管理用户权限的好方法。通过添加 AD 组,您可以授予用户对 SharePoint 网站、库、列表和项目的访问权限,而无需单独将每个用户添加到网站。在这篇博文中,我们将引导您完成将 AD 组添加到 SharePoint Online 的步骤。

  1. 以网站所有者或管理员身份登录到您的 SharePoint Online 网站。
  2. 单击设置齿轮>>站点权限>>共享站点。
  3. 输入要添加的 Active Directory 组的名称。设置组的权限。
  4. 最后,单击“添加”按钮保存更改。

    [玩转系统] 使用 PowerShell 将 AD 组添加到 SharePoint Online 组

该组现在将添加到您的网站,您可以根据需要开始管理该组的权限。您还可以转到高级权限设置并将 AD 组添加到相关的 SharePoint Online 网站组。

[玩转系统] 使用 PowerShell 将 AD 组添加到 SharePoint Online 组

在组连接的站点上,您可以单击“添加成员”,然后选择“仅共享站点”选项,以向 Sharepoint Online 站点授予对 Active Directory 组的访问权限。 “将成员添加到组”将不起作用,因为 Office 365 组不能包含 AD 安全组。

PowerShell 将 Active Directory 组添加到 SharePoint Online 组

让我们看看如何使用 PowerShell 将 Active Directory 安全组添加到 SharePoint Online。只需几个快速步骤,您就可以添加安全组并授予他们所需的权限。通过将 AD 安全组添加到 SharePoint Online 网站,您可以授予该组的成员访问该网站及其内容的权限。以下是 PowerShell 将 AD 组添加到 SharePoint Online 中的 SharePoint 组:


#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"

Function Add-ADGroupToSP($SiteURL,$ADGroupName,$SPGroupName)
{
    #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 Web and SharePoint Group
        $Web = $Ctx.Web
        $Group= $Web.SiteGroups.GetByName($SPGroupName)
 
        #Resolve the AD Security Group
        $ADGroup = $web.EnsureUser($ADGroupName)
 
        #sharepoint online powershell add AD group to sharepoint group
        $Result = $Group.Users.AddUser($ADGroup)
        $Ctx.Load($Result)
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "Active Directory Group '$ADGroupName' has been added to '$SPGroupName'"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sales"
$ADGroupName = "Marketing Managers"
$SPGroupName="Sales Portal Members"

#Call the function to add AD group to SharePoint Group
Add-ADGroupToSP -SiteURL $SiteURL -ADGroupName $ADGroupName -SPGroupName $SPGroupName

这会将给定的 Active Directory 安全组作为 SharePoint 用户添加到 SharePoint Online 组中。您还可以使用 AD 组的登录 ID(例如 c:0t.c|tenant|915xnusf-fbb3-7da1-k252-33e0de69f19")将 AD 组添加到 SharePoint Online 组。

SharePoint Online:PnP PowerShell 将安全组添加到站点

也可以使用 PnP PowerShell 将安全组添加到 SharePoint Online。以下是将 Active Directory 安全组添加到 SharePoint Online 组的 PnP PowerShell:


#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/hr"
$AdGroupID = "c:0t.c|tenant|798cb3d4-7ca8-4567-adb5-916bc496d7cd"
$GroupName = "HR Owners"

#Connect to site
Connect-PnPOnline $SiteURL -Interactive

#SharePoint Online powershell add security group
Add-PnPGroupMember -LoginName $AdGroupID -Identity $GroupName

AD 组的成员现在可以访问授予 SharePoint 组的资源和权限。要获取所有安全组及其 ID,请使用以下命令:


#Connect to Azure AD
Connect-AzureAD

#Get All Security Groups
Get-AzureADGroup -Filter "SecurityEnabled eq true" | Select DisplayName,ObjectID

我的另一篇文章中解释了获取 Active Directory 组的 ID:如何在 SharePoint Online 中添加 AD 安全组作为网站集管理员?

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

取消回复欢迎 发表评论:

关灯