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

[玩转系统] SharePoint Online:使用 PowerShell 将用户从 CSV 文件批量导入到组

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

SharePoint Online:使用 PowerShell 将用户从 CSV 文件批量导入到组


要求:将用户从 CSV 文件批量添加到 SharePoint Online 组。

如果您需要将用户批量添加到 SharePoint Online 中的组,则可以使用 PowerShell。在这篇文章中,我们将引导您完成使用 PowerShell 将用户添加到组所需的步骤。以下是包含用户和组数据的 CSV 文件,用于将用户批量添加到组中:

[玩转系统] SharePoint Online:使用 PowerShell 将用户从 CSV 文件批量导入到组

您可以在此处下载 CSV 文件:

SharePoint Online:使用 PowerShell 将用户批量添加到组

SharePoint Online 组通过组合用户帐户和安全组来帮助有效管理安全性。通常,Active Directory 用户和安全组将添加到 SharePoint 组,然后使用 SharePoint 组在网站内分配权限。以下是我的 PowerShell 脚本,用于将用户从 CSV 文件导入到 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"

Function Import-SPOUserFromCSV($CSVFile)
{  
    #Get data from CSV
    $UserData = Import-CSV $CSVFile

    #Get Credentials to connect
    $Cred = Get-Credential

    ForEach($Row in $UserData)
    {
        #Get Data from CSV
        $SiteURL = $Row.SiteURL
        $GroupName= $Row.GroupName
        $UserAccount = $Row.UserAccount
  
        Try {
            #Setup the context
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
            $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
     
            #Get the Web and Group
            $Web = $Ctx.Web
            $Group= $Web.SiteGroups.GetByName($GroupName)
 
            #Resolve the User
            $User=$web.EnsureUser($UserAccount)
 
            #Add user to the group
            $Result = $Group.Users.AddUser($User)
            $Ctx.Load($Result)
            $Ctx.ExecuteQuery()
 
            write-host  -f Green "User '$UserAccount' has been added to '$GroupName' in Site '$SiteURL'"
        }
        Catch {
            write-host -f Red "Error Adding user to Group!" $_.Exception.Message
        }
    }
}

#Call the function
Import-SPOUserFromCSV "C:\Temp\UserData.csv"

PnP PowerShell 从 CSV 文件批量添加用户

我们可以使用 PowerShell 从 Excel 文件或 CSV 将用户添加到 SharePoint Online 中的组。以下是如何使用 PnP PowerShell 批量添加用户:


#Variables
$CSVPath  ="C:\temp\UserData.csv"

#Get data from CSV
$CSVData = Import-Csv $CSVPath

#Iterate through each row in CSV
ForEach($Row in $CSVData)
{
    Try { 
        #Connect to SharePoint Online Site
        Write-host "Connecting to Site: "$Row.SiteURL
        Connect-PnPOnline -Url $Row.SiteURL -Interactive
 
        #Get the group
        $Group = Get-PnPGroup -Identity $Row.GroupName
 
        #Add Each user to the Group
        Add-PnPGroupMember -LoginName $Row.UserAccount -Identity $Group
        Write-host -f Green "`tAdded User $($Row.UserAccount) to $($Group.Title)"
    }
    Catch {
        write-host -f Red "Error Adding User to Group:" $_.Exception.Message
    }
}

当您设置新的团队网站并需要快速添加所有成员时,这会很有帮助。或者您可能只是想向现有群组添加一些新成员。这是 SharePoint 本地版本的另一篇文章:使用 PowerShell 将用户从 CSV 文件导入到 SharePoint

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

取消回复欢迎 发表评论:

关灯