SharePoint Online:使用 PowerShell 进行网站用户和组报告
要求:生成包含 SharePoint Online 网站集中所有组以及每个组的成员的报告。
先决条件:确保您已安装 SharePoint Online Management Shell!
获取所有网站组和每个组的成员:
管理用户和组是管理 SharePoint Online 的一个重要方面。在某些情况下,您可能需要从 SharePoint Online 导出所有用户和组的列表,以便执行备份、迁移或出于审核目的。使用 PowerShell,这个过程可以以高效且强大的方式实现自动化。在本文中,我们将讨论如何使用 PowerShell 从 SharePoint Online 导出用户和组。
如何在 SharePoint Online 中获取用户和组?嗯,答案并不像听起来那么容易。管理员可以按照以下步骤查看 SharePoint Online 中的用户和组,并快速导出用户和组列表以进行进一步分析。
可是等等!有一种更好的方法可以使用 PowerShell 查看 SharePoint Online 中的用户和组。
使用 PowerShell 将 SharePoint Online 用户和组导出到 Excel 是定期获取用户和组数据的快速而简单的方法。您需要做的第一件事是使用 PowerShell 连接到您的 SharePoint Online 租户。连接后,您可以使用 Get-SPOSiteGroup cmdlet 获取有关用户和组的信息。要将此信息导出到 Excel,您可以使用 Export-CSV cmdlet。这将创建一个 CSV 文件,然后您可以在 Excel 中打开该文件。您还可以使用 Out-File cmdlet 将数据输出到文本文件。
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
#Variables for processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$AdminName = "spadmin@crescent.com"
$SiteURL="https://crescent.sharepoint.com/sites/sales"
#User Names Password to connect
$Password = Read-host -assecurestring "Enter Password for $AdminName"
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password
#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credentialexp
#get all sharepoint online groups
$SiteGroups = Get-SPOSiteGroup -Site $SiteURL
#Get Members of each group
foreach($Group in $SiteGroups)
{
Write-host "Group:"$Group.Title
Get-SPOSiteGroup -Site $SiteURL -Group $Group.Title | Select-Object -ExpandProperty Users
}
要获取网站集的所有用户,请使用以下脚本:
Get-SPOUser -Site $siteURL | select DisplayName, LoginName | Where { $_.LoginName -like "*@crescent.com"}
如何从 SharePoint Online 导出用户列表?
我们可以使用 PowerShell 将 SharePoint Online 网站的所有用户及其组导出为 CSV,如下所示:
#Get All users of the site collections and their Groups
Get-SPOUser -Site "https://Crescent.sharepoint.com/sites/marketing" | ForEach-Object {
$_.LoginName + "`t" + $_.DisplayName+ "`t" + ($_.Groups -join ",") | Out-File "C:\Temp\UsersRpt.csv" -Force -Append
}
获取所有 SharePoint Online 网站集的用户和组:
您想要从 SharePoint Online 获取用户和组吗? PowerShell 可以提供帮助!
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
#Variables for processing
$AdminURL = "https://Crescent-admin.sharepoint.com/"
$AdminName = "SPAdmin@Crescent.com"
#User Name & Password to connect
$Password = Read-host -assecurestring "Enter Password for $AdminName"
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password
Try {
#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential
#Get all Site collections
Get-SPOSite -Limit ALL | ForEach-Object {
Write-Host "Site Collection:"$_.URL
#Get all Site Groups
$SiteGroups = Get-SPOSiteGroup -Site $_.URL
#get sharepoint online group members powershell
foreach($Group in $SiteGroups)
{
Write-host "Group:"$Group.Title
Get-SPOSiteGroup -Site $_.URL -Group $Group.Title | Select-Object -ExpandProperty Users
}
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online 组和权限报告
让我们获取有权访问该站点的所有组和权限以及每个组的成员。
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
#Variables for SharePoint Online Admin & Target site collection
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteCollURL="https://crescent.sharepoint.com/sites/sales"
#Get the Credentials
$Credential = Get-credential
#Connect To SharePoint Online
Connect-SPOService -url $AdminSiteURL -Credential $Credential
#Get the Site collection
$Site = Get-SPOSite $SiteCollURL
#Get all Groups of the site collection
$GroupColl = Get-SPOSiteGroup -Site $Site | Where { $_.Roles -ne $NULL -and $_.Users -ne $NULL}
Foreach($Group in $GroupColl)
{
#Get Permissions assigned to the Group
$GroupPermissions=""
ForEach($Role in $Group.Roles)
{
$GroupPermissions+= $Role+";"
}
Write-host -f Yellow "Group Name: $($Group.Title) - Permissions: $($GroupPermissions)"
#sharepoint online powershell to get group members
foreach($User in $Group.Users)
{
write-host -f Green $user
}
}
接下来,让我们使用 PowerShell 将 SharePoint Online 用户和组导出到 Excel。
将所有 SharePoint Online 网站集的用户和组导出到 CSV:
如果您需要将 SharePoint Online 用户和组导出到 Excel,PowerShell cmdlet 可以提供帮助。以下是将所有组和用户导出到 CSV 文件的 PowerShell:
#Admin Center & Site collection URL
$AdminCenterURL = "https://Crescent-admin.sharepoint.com/"
$CSVPath = "C:\Temp\GroupsReport.csv"
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)
$GroupsData = @()
#Get all Site collections
Get-SPOSite -Limit ALL | ForEach-Object {
Write-Host -f Yellow "Processing Site Collection:"$_.URL
#get sharepoint online groups powershell
$SiteGroups = Get-SPOSiteGroup -Site $_.URL
Write-host "Total Number of Groups Found:"$SiteGroups.Count
ForEach($Group in $SiteGroups)
{
$GroupsData += New-Object PSObject -Property @{
'Site URL' = $_.URL
'Group Name' = $Group.Title
'Permissions' = $Group.Roles -join ","
'Users' = $Group.Users -join ","
}
}
}
#Export the data to CSV
$GroupsData | Export-Csv $CSVPath -NoTypeInformation
Write-host -f Green "Groups Report Generated Successfully!"
以及样本报告:
如果需要使用 CSOM 或 PnP PowerShell 方法获取 SharePoint Online 组和用户报告,请使用:使用 PowerShell 获取 SharePoint Online 网站中的所有用户和组
包起来
在本文中,我们讨论了如何使用 PowerShell 从 SharePoint Online 导出用户和组。按照本指南中概述的步骤,您可以快速轻松地将所有用户和组的列表从 SharePoint Online 导出到 CSV 文件。使用 PowerShell,您可以自动执行此过程,并使管理和维护 SharePoint Online 环境变得更加轻松。


