ค้นหาเว็บไซต์

นำเข้าผู้ใช้จาก Excel (CSV) ไปยัง SharePoint โดยใช้ PowerShell


ความต้องการ: มีผู้ใช้จำนวนมากและต้องการเพิ่มพวกเขาลงใน SharePoint หรือไม่ ใช้สคริปต์ด้านล่างเพื่อเพิ่มผู้ใช้จำนวนมากไปยังกลุ่ม SharePoint โดยทางโปรแกรมโดยใช้ PowerShell สคริปต์นี้จะนำเข้ารายชื่อผู้ใช้จาก Excel CSV ไปยัง SharePoint และเพิ่มลงในกลุ่มที่เหมาะสม

วิธีแก้ปัญหา: มานำเข้าผู้ใช้จาก Excel ไปยัง SharePoint โดยใช้ PowerShell กันดีกว่า นี่คือไฟล์ CSV ของฉันพร้อมรายชื่อผู้ใช้และกลุ่มเป้าหมายใน SharePoint:

สคริปต์ PowerShell เพื่อนำเข้าผู้ใช้จาก CSV ไปยัง SharePoint:

นี่คือ PowerShell สำหรับเพิ่มผู้ใช้จำนวนมากไปยังกลุ่ม SharePoint จากไฟล์ CSV:


Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Variables
$UserListCSV = "C:\UsersToImport.csv"
$SiteURL ="https://Sales.Crescent.com"

# Import the CSV file
$UserList = Import-CSV $UserListCSV #-header("GroupName","UserAccount") - If CSV doesn't has headers

#Get the Web
$Web = Get-SPWeb $SiteURL

#Iterate through each user from CSV file
foreach ($user in $UserList)
{
    #Get the Group and User
    $Group = $web.SiteGroups[$User.GroupName]
    $User = $web.Site.RootWeb.EnsureUser($User.UserAccount)
    #Add user to Group
    $Group.AddUser($User) 

    Write-Host "$($User) Added Successfully!" -ForegroundColor Green
}

#Dispose web object
$Web.Dispose()

นำเข้าผู้ใช้จำนวนมากไปยังไซต์ SharePoint จากไฟล์ CSV โดยใช้ PowerShell:

ไฟล์ CSV อื่นของฉันมีชื่อบัญชี (ในรูปแบบ "โดเมน\ชื่อผู้ใช้" และ "ชื่อกลุ่ม")

สคริปต์ PowerShell เพื่อเพิ่มผู้ใช้ในกลุ่ม SharePoint:

มาเพิ่มข้อผิดพลาดเล็กน้อยในการจัดการในครั้งนี้


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Import the List of Users from a .csv file,
$UserList= Import-CSV "C:\UserList.csv"
 
#Get the Web
$web = Get-SPWeb "https://sharepoint.crescent.com/sites/marketing"
 
foreach ($Row in $UserList)
{
  #Get the Group
   $Group = $web.Groups[$Row.GroupName]
 
 #Validate the user name
 try
   {
        $user = $web.Site.RootWeb.EnsureUser($Row.AccountName)
   }
   catch [system.exception]
   {
     write-host $_.Exception.Message
   }
 
   #Add user if valid user name is provided
    if($user -ne $null)
    {
       $Group.AddUser($user)
       write-host $user.AccountName
    }
}

#Dispose the web object
$Web.Dispose()

บทความที่เกี่ยวข้อง