รับผู้ใช้และกลุ่มทั้งหมดในไซต์ SharePoint Online โดยใช้ PowerShell
ความต้องการ: รับรายงานผู้ใช้และกลุ่มทั้งหมดใน SharePoint Online โดยใช้ PowerShell
สคริปต์ PowerShell เพื่อรับรายงานผู้ใช้และกลุ่มทั้งหมดใน SharePoint Online:
คุณจำเป็นต้องได้รับรายชื่อผู้ใช้และกลุ่มทั้งหมดใน SharePoint Online หรือไม่ ถ้าเป็นเช่นนั้น PowerShell สามารถช่วยได้! การส่งออกผู้ใช้และกลุ่มจากไซต์ SharePoint Online สามารถทำได้ด้วย PowerShell มาดูวิธีสร้างรายงานภาพรวมสำหรับผู้ใช้ไซต์และกลุ่มในรูปแบบ Excel กันดีกว่า
สคริปต์ PowerShell CSOM นี้รับกลุ่มและผู้ใช้ทั้งหมดของแต่ละกลุ่มในไซต์ SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set Variables for Site URL
$SiteURL= "https://crescent.sharepoint.com/sites/sales"
#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 Groups
$Groups=$Ctx.Web.SiteGroups
$Ctx.Load($Groups)
$Ctx.ExecuteQuery()
#Get Each member from the Group
Foreach($Group in $Groups)
{
Write-Host "--- $($Group.Title) --- "
#Getting the members
$SiteUsers=$Group.Users
$Ctx.Load($SiteUsers)
$Ctx.ExecuteQuery()
Foreach($User in $SiteUsers)
{
Write-Host "$($User.Title), $($User.Email), $($User.LoginName)"
}
}
}
Catch {
write-host -f Red "Error getting groups and users!" $_.Exception.Message
}
วิธีนี้จะมีประโยชน์ถ้าคุณต้องการรับรายชื่อผู้ใช้หรือกลุ่มทั้งหมดในไซต์เพื่อวัตถุประสงค์ในการรายงาน แก้ไขปัญหาการเข้าถึง หรือถ้าคุณต้องการลบผู้ใช้หรือกลุ่มออกจากไซต์
PnP PowerShell เพื่อรับรายงานกลุ่มและผู้ใช้ใน SharePoint Online:
หากคุณต้องการส่งออกผู้ใช้และกลุ่ม SharePoint ออนไลน์ไปยัง Excel PnP PowerShell cmdlets สามารถช่วยได้ Get-PnPGroup cmdlet มีวิธีการส่งออกกลุ่ม SharePoint Online และสมาชิกผู้ใช้ในแต่ละกลุ่ม หากต้องการใช้ cmdlet นี้ คุณจะต้องเชื่อมต่อกับ SharePoint ออนไลน์โดยใช้ Connect-PnPOnline cmdlet เมื่อคุณเชื่อมต่อแล้ว คุณสามารถใช้ Get-PnPGroup cmdlet เพื่อส่งออกข้อมูลผู้ใช้ที่ต้องการไปยังไฟล์ CSV ไฟล์นี้สามารถเปิดได้ใน Excel เพื่อการวิเคราะห์เพิ่มเติม
มาส่งออกผู้ใช้และผู้ใช้ทั้งหมดจากไซต์เป็นไฟล์ CSV:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFile = "C:\Temp\GroupUsers.csv"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential) #-Interactive
#Get All Groups from Site - Exclude system Groups
$Groups = Get-PnPGroup | Where {$_.OwnerTitle -ne "System Account"}
$GroupData=@()
#Get Group Details
ForEach($Group in $Groups)
{
#Get Group data
$GroupData += New-Object PSObject -Property ([ordered]@{
"Group Name" = $Group.Title
"Users" = $Group.Users.Title -join "; "
})
}
$GroupData
#Export Users data to CSV file
$GroupData | Export-Csv -NoTypeInformation $CSVFile
นี่คือบทความอื่นเพื่อรับกลุ่มและสมาชิกกลุ่มทั้งหมดจาก SharePoint Online โดยใช้เชลล์ SharePoint Online: SharePoint Online: ผู้ใช้ไซต์และกลุ่มรายงานโดยใช้ PowerShell
