SharePoint Online:使用 PowerShell 获取网站集中的所有用户
要求: SharePoint Online PowerShell 获取网站集中的所有用户。
SharePoint Online:PowerShell 从网站集中获取所有用户
作为 SharePoint Online 网站的管理员,您可能需要获取有权访问该网站的所有用户的列表。在这篇博文中,我们将了解如何使用 PowerShell 获取 SharePoint Online 网站集中的所有用户。如果您需要获取所有用户的列表以实现自动化,这会很方便。假设您需要生成报告来审核用户权限或获取当前正在使用网站集的人员的概述。让我们开始吧!
如何从 SharePoint Online 网站获取所有用户?
获取 SharePoint Online 网站集中所有用户的列表的最简单方法是使用以下技巧通过网站设置:
- 导航到顶级 SharePoint 网站集。 (例如,https://YourDomain.SharePoint.com/sites/Retail)
- 将“/_layouts/15/people.aspx?MembershipGroupId=0”附加到 URL 以获取“所有人”页面。例如,https://crescent.sharepoint.com/sites/Retail/_layouts/15/people.aspx?MembershipGroupId=0。
SharePoint 将返回已明确提供网站访问权限的所有用户以及从组继承访问权限的所有用户。
虽然我的其他帖子讨论了如何获取 SharePoint Online 中的所有用户和组?,但本文旨在获取 SharePoint 网站集的所有用户。以下是获取所有用户的 SharePoint Online CSOM:
#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"
#Site collection URL
$SiteURL="https://crescent.sharepoint.com"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Initialize the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get all users of the site collection
$Users = $ctx.Web.SiteUsers
$ctx.Load($Users)
$ctx.ExecuteQuery()
#Get User name and Email
$Users | ForEach-Object { Write-Host "$($_.Title) - $($_.Email)"}
PowerShell 从 SharePoint Online 中的所有网站集获取所有用户
要使用 PowerShell 从网站集中获取所有用户,您需要首先从 PowerShell 连接到您的 SharePoint Online 网站。连接后,您可以运行 Get-SPOUser cmdlet 以返回有权访问该站点的所有用户。以下是使用 Get-SPOUser cmdlet 从租户中的所有网站集中获取所有用户的 SharePoint Online Management Shell 方法:
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
$AdminSiteURL="https://crescent-admin.sharepoint.com"
#Connect to SharePoint Online Admin
Write-host "Connecting to Admin Center..." -f Yellow
Connect-SPOService -url $AdminSiteURL -Credential (Get-Credential)
Write-host "Getting All Site collections..." -f Yellow
#Get each site collection and users
$Sites = Get-SPOSite -Limit ALL
Foreach($Site in $Sites)
{
Write-host "Getting Users from Site collection:"$Site.Url -f Yellow
Get-SPOUser -Limit ALL -Site $Site.Url | Select DisplayName, LoginName
}
此 PowerShell 脚本获取 SharePoint Online 中的所有用户。
PnP PowerShell 获取 SharePoint Online 网站集中的所有用户
该脚本将收集指定站点上的所有用户并以易于阅读的格式返回它们。
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All users of the site collection
Get-PnPUser
此脚本会获取当前网站集中的所有用户,无论他们是否具有当前网站的访问权限。您可以添加 -WithRightsAssigned 开关来获取有权访问该站点的所有用户。
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/2017"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Web = Get-PnPWeb
#Get All users who have permission to the subsite
Get-PnPUser -WithRightsAssigned -Web $Web
$Users=获取 PnPUser |其中 { $_.IsHiddenInUI -eq $false -and $_.PrincipalType -eq “User”-and (-not[string]::IsNullOrEmpty($_.Email))}
SharePoint Online:使用 PnP PowerShell 将网站集中的所有用户导出为 CSV
让我们使用 Get-PnPUser cmdlet 编译网站集中的所有用户,然后将其导出到 CSV 以供进一步分析。
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/"
$CSVFile = "C:\Temp\UserData.csv"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential) #-Interactive
#Get All users of the site collection
$Users = Get-PnPUser
$UserData=@()
#Loop through Users and get properties
ForEach ($User in $Users)
{
$UserData += New-Object PSObject -Property @{
"User Name" = $User.Title
"Login ID" = $User.LoginName
"E-mail" = $User.Email
"User Type" = $User.PrincipalType
}
}
$UserData
#Export Users data to CSV file
$UserData | Export-Csv -NoTypeInformation $CSVFile
此脚本使用 PowerShell 连接到您的 SharePoint Online 网站集,从网站集中获取用户列表,然后将用户列表导出为 CSV!以及脚本输出:
