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

[玩转系统] SharePoint Online:使用 PowerShell 获取所有网站集

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

SharePoint Online:使用 PowerShell 获取所有网站集


要求:在 SharePoint Online 中使用 PowerShell 获取所有网站集。

如何获取 SharePoint Online 中的网站集列表?

网站集是一组网站,通常按部门、项目、区域、职能或业务单位组织。如果您负责管理 SharePoint Online 环境,您迟早需要获取租户中所有网站集的列表。值得庆幸的是,PowerShell 使收集有关 SharePoint Online 环境的信息变得容易。本博文将向您展示如何使用 PowerShell 获取租户中所有网站集的列表。您还将了解如何将结果导出到 CSV 或 HTML 文件以用于报告目的。

使用 SharePoint 管理中心查看 SharePoint Online 租户中的所有网站

如何查看SharePoint Online中的所有网站集?那么,如果您想获取 SharePoint Online 网站集的列表,您可以转到 SharePoint 管理中心 (https://YourDomain-admin.sharepoint.com) >> 展开“网站”> > 活跃站点。

[玩转系统] SharePoint Online:使用 PowerShell 获取所有网站集

这将为您提供 SharePoint Online 中所有网站集的列表。您可以通过单击工具栏中的“导出”按钮将它们导出为 CSV。让我们看看如何使用 SharePoint Online PowerShell 获取所有网站集。

如何使用 PowerShell 在 SharePoint Online 中获取网站集?

作为 SharePoint Online 管理员,您需要检索有关 SharePoint 环境中特定网站集的信息。要使用 PowerShell 在 SharePoint Online 中获取网站集,您可以按照以下步骤操作:

  1. 在本地计算机上打开 PowerShell。
  2. 使用 Connect-SPOService cmdlet 连接到您的 SharePoint Online 租户。您将需要提供您的 SharePoint Online 管理 URL 和凭据。
  3. 运行 Get-SPOSite cmdlet 以检索所需的网站集。例如,要检索 URL 为“https://crescent.sharepoint.com/sites/marketing”的网站集,您可以运行以下命令:Get-SPOSite -Identity https://crescent.sharepoint。 com/sites/marketing 这将返回有关网站集的信息,包括其 URL、标题和所有者。
  4. 如果要检索有关网站集的其他信息,可以使用其他 PowerShell cmdlet,例如 Get-SPOSiteGroup 或 Get-SPOSiteGroupPermissions。

#Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"

#Connect to SharePoint Online from PowerShell
Connect-SPOService -url $AdminSiteURL

#Get the Site Collection
$Site =  Get-SPOSite -Identity $SiteURL

#sharepoint online list all site collections powershell
$Site | Select -Property Template, StorageUsageCurrent, StorageQuota, LastContentModifiedDate, sharingcapability

请注意,要运行 SharePoint Online 的 PowerShell cmdlet,您需要在租户上具有 SharePoint 管理员角色,并在本地计算机上安装 SharePoint Online Management Shell(或 SharePoint Online PowerShell 模块)。

如何使用 PowerShell 获取 SharePoint Online 中的所有网站集?

网站集是 SharePoint Online 中的主要对象。使用 Get-SPOSite PowerShell cmdlet 列出租户中的所有网站集。与本地SharePoint Management Shell类似,您还可以使用“SharePoint Online Management Shell”连接到Office 365和SharePoint Online网站!以下是用于获取 SharePoint Online 中所有网站集的 PowerShell 脚本:


Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

$AdminSiteURL="https://crescent-admin.sharepoint.com"

$Credential = Get-credential 
Connect-SPOService -url $AdminSiteURL -Credential $Credential 

#sharepoint online list all site collections powershell
Get-SPOSite -Limit All | Format-Table Url, Template, StorageUsageCurrent, StorageQuota, LastContentModifiedDate, sharingcapability -AutoSize 

默认情况下,Get-SPOSite cmdlet 检索 200 个网站集。我们必须将“Limit”开关指定为“All”,才能从租户获取所有站点。以下是 PowerShell 脚本,用于从 SharePoint Online 获取包含详细信息的所有网站集并将所有网站集库存导出到 CSV:


Connect-SPOService -url "https://crescent-admin.sharepoint.com" -Credential (Get-credential)
Get-SPOSite -Detailed | Export-CSV -LiteralPath C:\Temp\SitesInventory.csv -NoTypeInformation

此 PowerShell 列出了 SharePoint Online 租户中的所有网站集以及所有可用的网站集详细信息。

[玩转系统] SharePoint Online:使用 PowerShell 获取所有网站集

PowerShell 获取 SharePoint Online 中的所有网站集

使用保存的凭据连接到 SharePoint Online 怎么样?下面是使用保存的凭据列出所有 SharePoint Online 网站的 PowerShell(我假设您在脚本中指定的帐户未启用 MFA!):


#Load SharePoint Online 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"
 
#Variables for Processing
$AdminUrl = "https://crescent-admin.sharepoint.com/"
$UserName= "[email protected]"
$Password = "Password goes here"
 
#Setup Credentials to connect
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword

#connect to the service
Connect-SPOService -Url $AdminUrl -Credential $Credentials

#sharepoint online get all site collections PowerShell
$SiteColl = Get-SPOSite 

#sharepoint online PowerShell iterate through all site collections
ForEach($Site in $SiteColl)
{
    Write-host $Site.Url
}

用于获取所有网站集的 PowerShell,包括现代团队网站和组网站:

此 PowerShell 枚举 SharePoint Online 中的网站集并获取每个网站的 URL。

让我们使用 PowerShell 获取所有网站的列表,包括 SharePoint Online 中的现代团队网站和组连接网站。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
  
#Get All Site collections from the Tenant- Including Modern Team sites and communication sites
Function Get-SPOSites($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
  
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()
 
    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        Write-host $Site.URL
    }
}
  
#Set Parameters
$AdminSiteUrl = "https://crescent-admin.sharepoint.com/"
$Cred= Get-Credential

#sharepoint online powershell list all sites
Get-SPOSites -AdminSiteURL $AdminSiteUrl -Cred $Cred

SharePoint Online PowerShell 列出所有网站集

让我们获取特定路径下的所有网站集:


Import-Module Microsoft.Online.SharePoint.Powershell

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials to connect to the SharePoint Admin Center
$Cred = Get-Credential

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get All site collections which has /sites in its path
Get-SPOSite -Filter { Url -like '*/sites*' } -Limit All

#Get All site collections of Group site template
#Get-SPOSite -Filter { Template -eq "GROUP#0" } -Limit All

您可以根据某些条件过滤并获取所有网站集。例如,让我们获取所有使用 > 1 GB 存储的网站。


Get-SPOSite -Limit All | where { $_.StorageUsageCurrent -gt 1024 }

过滤和获取集合:例如,获取 URL 中包含“Project*”的所有网站集。


Get-SPOSite -Filter {$_.url -like "https://tenant.sharepoint.com/sites/Project*"}

SharePoint Online PnP PowerShell 获取所有网站

需要获取 SharePoint Online 环境中所有网站集的列表? PnP PowerShell 来救援!此脚本向您展示如何使用 PnP PowerShell cmdlet Get-PnPTenantSite 获取租户中所有网站集的列表。


#Set Parameter
$TenantSiteURL="https://crescent.sharepoint.com"

#Connect to the Tenant site
Connect-PnPOnline $TenantSiteURL -Credentials (Get-Credential)

#sharepoint online pnp powershell get all sites
Get-PnPTenantSite

我们可以使用以下命令提取所有网站集详细信息并将其导出为 CSV:


#Config Variables
$TenantSiteURL =  "https://crescent-admin.sharepoint.com/"
$CSVFilePath = "C:\Temp\AllSitesData.csv"

#Connect to Tenant Admin Site
Connect-PnPOnline -Url $TenantSiteURL -Interactive

#Get All Site collections data and export to CSV
Get-PnPTenantSite -Detailed | Select Title, URL, Owner, LastContentModifiedDate, WebsCount, Template, StorageUsage | Export-Csv -path $CSVFilePath -NoTypeInformation

这将获取所有网站集,包括现代团队网站和通信网站。我们跳过搜索中心、我的网站主机、应用程序目录、内容类型中心、电子数据展示和机器人等,获取租户中所有网站的配额:


#Parameter
$TenantAdminURL = "https://Crescent-admin.sharepoint.com"

#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Interactive
   
#Get All Site collections - Exclude: Seach Center, Redirect site, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SiteCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
   
#Loop through each site collection
ForEach($Site in $SiteCollections)
{
    #Get the storage Quota of the site
    Write-host $Site.URL - $Site.StorageQuota
}

您可以将 OneDrive 站点包含为:


#Get all site collections including OneDrive Sites
Get-PnPTenantSite -IncludeOneDriveSites

获取 HTML 报告格式的网站集列表

让我们使用 PowerShell 以友好的 HTML 格式获取所有网站集的列表:


Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$ReportOutput="C:\Temp\SitesRpt.html"

#CSS Styles
$HeadTag = @"
<style type="text/css">

table {
 border-collapse: collapse; font-family: verdana,arial,sans-serif;
 font-size:11px; color:#333333; border-width: 1px; border-color: #a9c6c9;
 border: b1a0c7 0.5pt solid; border-spacing: 1px; border-collapse: separate; /*Sal Table format */  
}

th {
border-width: 1px; padding: 5px; background-color:#8064a2;
border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; 
color: white;  font-size: 11pt;  font-weight: 700;  text-decoration: none;
}

td {
 border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: black; 
 font-size: 11pt; font-weight: 400; text-decoration: none; 
}

tr:nth-child(even) { background-color: #e4dfec; }
tr:hover { background-color: #694D8C; color:#ffffff }

</style>
"@

$PreContentTag = "<h3> SharePoint Online: Site Collections Inventory Report </h3>"

#Get Credentials to connect to the SharePoint Admin Center
$Cred = Get-Credential

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
Write-Host "Total Number of Site collections Found:"$SiteCollections.count -f Yellow

#Get the site collection details, convert to html and output to the report
$SiteCollections | ConvertTo-HTML -Title "Site Collection Inventory Report" -Property URL, Title,@{Expression={$_.LastContentModifiedDate }; Label="Last Modified"} -Head $HeadTag -PreContent $PreContentTag | Out-File $ReportOutput

报告如下所示:

[玩转系统] SharePoint Online:使用 PowerShell 获取所有网站集

包起来

总之,PowerShell 提供了一种强大而有效的方法来检索 SharePoint Online 中所有网站集的列表。通过使用适当的 PowerShell cmdlet,SharePoint 管理员可以快速轻松地检索其环境中所有网站集的完整列表。此信息可用于各种管理和审核任务,例如审核、故障排除、跟踪使用情况和优化 SharePoint 环境。

这是另一篇有关将 PowerShell 与 SharePoint Online 结合使用的文章:如何将 PowerShell 与 SharePoint Online 结合使用?

经常问的问题:

如何使用 PowerShell 创建 SharePoint Online 网站集?

您可以使用 SharePoint Online Management Shell - New-SPOSite、PnP PowerShell cmdlet New-PnPTenantSite 或 CSOM PowerShell 脚本在 SharePoint Online 中创建新网站。
详细信息:PowerShell 在 SharePoint Online 中创建新网站集

如何使用 PowerShell 获取 SharePoint Online 中的所有子网站?

若要从 SharePoint Online 网站获取所有子网站,请使用:PnP PowerShell 中的 Get-PnPSubWeb cmdlet。您还可以使用 CSOM PowerShell 脚本加载网站集的获取网站(子网站)。
详细信息:PowerShell 在 SharePoint Online 中获取网站集中的所有子网站

如何在 PowerShell 中获取所有 SharePoint 网站和子网站?

要获取所有网站集和子网站,您必须在 CSOM 中递归获取 Web 集合。使用 PnP PowerShell:Get-PnPSubWeb -Recurse
详细信息:PowerShell 获取 SharePoint Online 中的所有网站集和子网站

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

取消回复欢迎 发表评论:

关灯