SharePoint Online:使用 PowerShell 添加批量用户和组
要求:将多个用户和组批量添加到 SharePoint Online 网站。
如何在 SharePoint Online 中创建多个组和用户?
我必须创建多个 SharePoint 用户组并将用户添加到不同站点上的这些组。在这篇博文中,我们来看看如何使用 PowerShell 将多个用户和组添加到 SharePoint Online 网站。如果您想要自动执行将用户和组添加到站点的过程,这会派上用场。这是我的 CSV 文件,其中包含用户和组数据:
PowerShell 将用户和组添加到 SharePoint 网站
当您想要快速将大量用户和组添加到多个 SharePoint Online 网站时,此 PowerShell 脚本会派上用场。它检查站点上是否已存在组。如果没有,它将创建一个新的 SharePoint 组,然后向该组分配权限。最后,它将给定的用户添加到相应的组中。
#Config Variables
$CSVPath = "C:\Temp\UsersAndGroups.csv"
#Get Data from CSV
$CSVData = Import-CSV -Path $CSVPath
Try {
ForEach($Row in $CSVData)
{
#Connect to the Site with PnP PowerShell
Connect-PnPOnline -Url $Row.SiteURL -Interactive
#Check if the group exists already
If((Get-PnPGroup | Where { $_.Title -eq $Row.GroupName}) -eq $Null)
{
#Create SharePoint Online Group
$Group = New-PnPGroup -Title $Row.GroupName -ErrorAction Stop
Write-Host -f Green "Group '$($Row.GroupName)' Created Successfully!"
}
Else
{
#Get the Group
$Group = Get-PnPGroup -Identity $Row.GroupName
}
#Set Group Permissions
Set-PnPGroupPermissions -Identity $Group -AddRole $Row.Permission
#Add Users to the Group
$Users = $Row.Users -split ";"
ForEach($User in $Users)
{
$NewUser = Add-PnPGroupMember -LoginName $User.Trim() -Identity $Group
Write-host -f Green "`tAdded $User to $($Group.Title)"
}
}
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
我有关使用 PowerShell 将多个 SharePoint 组和用户添加到 SharePoint Online 网站的相关帖子:
- SharePoint Online:使用 PowerShell 将用户从 CSV 文件批量导入到组
- SharePoint Online:使用 PowerShell 从 CSV 文件批量创建 SharePoint 组
