[玩转系统] 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 组的方法:
- 导航到 SharePoint Online 网站,单击“网站设置”齿轮图标,然后选择“网站设置”>> 在“网站设置”页面中,单击“用户和权限”部分中的“网站权限”。
在“权限”页面中,单击“权限”选项卡下功能区中的创建组图标。
- 在“创建组”页面上,提供名称以及组的其他设置(可选),例如描述和所有者。设置群组设置,例如谁可以查看/编辑群组成员身份、允许用户请求成员身份以及离开群组。所有请求都将发送到指定的电子邮件。
- 指定组所有者。默认情况下,它被设置为创建 SharePoint 组的用户。
在“授予组对此站点的权限”部分中,选中相应的复选框以将权限级别分配给您的新组。
- 单击“创建”。
创建组后,您可以对其进行重命名、在一处更改组的权限以及向其中添加/删除人员。让我们看看如何使用 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 添加批量用户和组
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[短剧] 2025年05月31日 精选+付费短剧推荐58部
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[韩剧] 宝物岛/宝藏岛/金银岛(2025)【全16集】【朴炯植/悬疑】
[电影] 愤怒的牦牛 (2025) 国语中字 4k
[短剧合集] 2025年05月30日 精选+付费短剧推荐56部
[软件合集] 25年5月30日 精选软件26个
[软件合集] 25年5月29日 精选软件18个
[短剧合集] 2025年05月28日 精选+付费短剧推荐38部
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag