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

[玩转系统] SharePoint Online:如何使用 PowerShell 创建组?

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

SharePoint Online:如何使用 PowerShell 创建组?


要求:使用 PowerShell 在 SharePoint Online 中创建一个新组。

创建 SharePoint Online 组

SharePoint 组是共享一组相似权限的用户的容器。 SharePoint 组提供了一种集中管理组安全性的便捷方法,而不是管理单个用户。我们可以将用户组织成组(最多 5,000 个),在站点级别为组设置权限,然后在任何基础对象(如子站点、列表和列表项)中使用它们。

默认情况下,SharePoint 在创建网站时会创建三个用户组:所有者、成员和访问者,您需要成为网站所有者/网站集管理员才能向该组授予权限。那么,如何在SharePoint Online中创建用户组呢?本文分享了在 SharePoint Online 中创建组的 PowerShell 脚本。

如何在 SharePoint Online 中创建组?

可以创建 SharePoint Online 组以满足任何自定义安全要求。以下是创建 SharePoint Online 组的方法:

  1. 导航到 SharePoint Online 网站,单击“网站设置”齿轮图标,然后选择“网站设置”>> 在“网站设置”页面中,单击“用户和权限”部分中的“网站权限”。
  2. 在“权限”页面中,单击“权限”选项卡下功能区中的创建组图标。

    [玩转系统] SharePoint Online:如何使用 PowerShell 创建组?

  3. 在“创建组”页面上,提供名称以及组的其他设置(可选),例如描述和所有者。设置群组设置,例如谁可以查看/编辑群组成员身份、允许用户请求成员身份以及离开群组。所有请求都将发送到指定的电子邮件。
  4. 指定组所有者。默认情况下,它被设置为创建 SharePoint 组的用户。
  5. 在“授予组对此站点的权限”部分中,选中相应的复选框以将权限级别分配给您的新组。

    [玩转系统] SharePoint Online:如何使用 PowerShell 创建组?

  6. 单击“创建”。

创建组后,您可以对其进行重命名、在一处更改组的权限以及向其中添加/删除人员。让我们看看如何使用 SharePoint Online 的 PowerShell 创建组。

使用 PowerShell 在 SharePoint Online 中创建组

New-SPOSiteGroup PowerShell cmdlet 允许您在 SharePoint Online 网站集中创建新的 SharePoint 组。此 cmdlet 需要您希望在其中创建此新 SharePoint 组的网站集的 URL、新组的名称以及您希望为其分配的权限级别。下面的 PowerShell 脚本显示了如何使用此 cmdlet 在 SharePoint Online 网站中创建一个名为“营销”的新安全组“营销人员”,并具有编辑和设计权限。

以下是使用 PowerShell 在 SharePoint Online 中创建新用户组的示例:


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

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

#sharepoint online powershell add group to site
New-SPOSiteGroup -Site $SiteCollectionURL -Group "Marketing Staff" -PermissionLevels "Design", "Edit"

此 cmdlet 在 SharePoint Online 网站集中创建一个新组。您可以使用 SharePoint Online Management Shell PowerShell 控制台运行此脚本。

SharePoint Online PowerShell 将组添加到网站

让我们使用 PowerShell CSOM 在 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/Sales"
$GroupName="Sales Managers"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#sharepoint online create group powershell
$GroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
$GroupInfo.Title = $GroupName      
$Group = $Ctx.web.SiteGroups.Add($GroupInfo)
$Ctx.ExecuteQuery()

简单明了吧?让我们添加一些错误处理代码并为创建的组分配权限。

SharePoint Online 使用 PowerShell 创建组

以下是用于创建 SharePoint 组的 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/Sales"
$GroupName="Sales Managers"
$PermissionLevel="Edit"

#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 existing 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 doesn't exist already
    If($GroupNames -notcontains $GroupName)
    {
        #sharepoint online powershell create group
        $GroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
        $GroupInfo.Title = $GroupName      
        $Group = $Ctx.web.SiteGroups.Add($GroupInfo)
        $Ctx.ExecuteQuery()

        #Assign permission to the group
        $RoleDef = $Ctx.web.RoleDefinitions.GetByName($PermissionLevel)
        $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
        $RoleDefBind.Add($RoleDef)
        $Ctx.Load($Ctx.Web.RoleAssignments.Add($Group,$RoleDefBind))
        $Ctx.ExecuteQuery()

        write-host  -f Green "User Group has been Added Successfully!"
    }
    else
    {
        Write-host -f Yellow "Group Exists already!"
    }
}
Catch {
    write-host -f Red "Error Creating New user Group!" $_.Exception.Message
}

此脚本创建一个新的 SharePoint Online 组并向该组分配权限。

使用 PnP PowerShell 创建 SharePoint Online 组

PnP PowerShell 是创建和管理 SharePoint Online 组的有效方法。只需几个简单的 cmdlet,您就可以创建新组、添加用户并自定义设置以满足您的需求。下面是用于向网站添加组的 SharePoint Online PnP PowerShell。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$GroupName ="Contributors"
$Permissions="Contribute"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Create a new group in SharePoint Online
    $Group = New-PnPGroup -Title $GroupName -ErrorAction Stop

    #Set Group Permissions
    Set-PnPGroupPermissions -Identity $Group -AddRole $Permissions    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

此 PowerShell 将组添加到 SharePoint Online 网站。

PnP PowerShell 从 CSV 文件创建多个组

这次,我们使用 CSV 文件批量创建 SharePoint Online 组。


#Function to Create SharePoint Online Group
Function Add-PnPGroup($SiteURL, $GroupName, $Permission)
{
    Try {
        #Connect to SharePoint Online Site
        Connect-PnPOnline -url $SiteURL -Interactive
        $Web = Get-PnPWeb

        Write-host "Creating Group '$GroupName'..." -f Yellow -NoNewline
        #Check if the group exists already
        If((Get-PnPGroup | Where { $_.Title -eq $GroupName}) -eq $Null)
        {
            #Create SharePoint Online Group
            $Group = New-PnPGroup -Title $GroupName -ErrorAction Stop
 
            #Set Group Permissions on the Web
            Set-PnPGroupPermissions -Identity $GroupName -AddRole $Permission -Web $Web
            Write-host "Done!" -f Green
        }
        Else
        {
            Write-host "Group '$GroupName' already exists!" -f Yellow
        }
    }
    Catch {
        Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}

#Parameters
$CSVFile = "C:\Temp\Groups.csv"

#Get Groups Data from CSV
$GroupData = Import-Csv -Path $CSVFile | ForEach-Object {
        #Call function to Create Group
        Add-PnPGroup -SiteURL $_.SiteURL -GroupName $_.GroupName -Permission $_.Permission
}

这是我的 CSV 文件:

创建组后,您可以将用户添加到组中:SharePoint Online:使用 PowerShell 添加批量用户和组

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

取消回复欢迎 发表评论:

关灯