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

[玩转系统] SharePoint Online:用于获取所有网站集和子网站的 PowerShell

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

SharePoint Online:用于获取所有网站集和子网站的 PowerShell


要求: SharePoint Online PowerShell 获取所有网站集和子网站。

用于列出所有网站集和子网站的 SharePoint Online PowerShell

您是否需要获取 SharePoint Online 环境中所有网站和子网站的列表?手动检索 SharePoint Online 中所有网站和子网站的列表可能是一个耗时且容易出错的过程,尤其是当您拥有包含许多网站和子网站的大型 SharePoint 环境时。 PowerShell 让一切变得简单!使用 PowerShell,您可以快速轻松地检索 SharePoint Online 中所有网站和子网站的完整列表。

在本文中,我们将了解如何使用 PowerShell 从 SharePoint Online 租户获取 SharePoint Online 中的所有网站集和子网站。以下是用于列出 SharePoint Online 中所有网站和子网站的 CSOM PowerShell:


Import-Module Microsoft.Online.SharePoint.Powershell

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

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

#Function to get all subsites of a site
Function Get-SPOWeb($WebURL)
{ 
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
    $Context.Credentials = $Credentials
    $Web = $context.Web
    $Context.Load($web)
    $Context.Load($web.Webs)
    $Context.executeQuery()
 
    #Iterate through each subsite in the current web
    foreach ($Subweb in $web.Webs)
    {
        #Get the web object
        $Subweb

        #Call the function recursively to process all subsites underneath the current web
        Get-SPOWeb($Subweb.url)
    }
}

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

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All

#Traverse through each site collection and get their subsits
Foreach ($Site in $SiteCollections)
{
    Write-Host $Site.Url     
    $AllWebs = Get-SPOWeb $Site.Url
    $AllWebs | ForEach-Object { Write-Host $_.URL }
}   

这将为您提供网站集及其子网站的列表。这是另一个脚本:

SharePoint Online:PowerShell 列出所有网站集中的所有子网站

让我们从租户的所有网站集中获取子网站。以下是用于获取 SharePoint Online 中所有网站集和子网站的 PowerShell 脚本:


#Load SharePoint CSOM 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"

#Function to get each subsite in site collection
Function Get-SPOWebs($SiteURL)
{
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.Webs)
    $Ctx.ExecuteQuery()

    #Do something
    Write-host $Web.Url

    #Loop through each each subsite in site
    Foreach($Web in $web.Webs)
    {
        Get-SPOWebs $Web.Url
    }
}

#Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"

#Setup Credentials to connect
$Cred= Get-Credential
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential ($Cred)

#Get all Site collections
$Sites = Get-SPOSite -Limit All

#Loop through site collections
ForEach($Site in $Sites)
{
    #Call the function to get all sub-sites in site collection
    Get-SPOWebs($Site.URL)
}

SharePoint 管理员可以使用此 PowerShell 快速获取租户中所有网站和子网站的列表。

PnP PowerShell 获取 SharePoint Online 中的所有网站和子网站

让我们看看如何使用 PnP PowerShell 模块获取 SharePoint Online 中的所有网站和子网站。此脚本使用 PnP PowerShell 递归地检索所有网站集及其子网站。


#Set Variables
$SiteURL = "https://crescent.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get All Site collections  
    $SitesCollection = Get-PnPTenantSite

    #Loop through each site collection
    ForEach($Site in $SitesCollection)  
    {  
        Write-host -F Green $Site.Url  
        Try {
            #Connect to site collection
            Connect-PnPOnline -Url $Site.Url -Credentials $Cred

            #Get Sub Sites Of the site collections
            $SubSites = Get-PnPSubWeb -Recurse
            ForEach ($web in $SubSites)
            {
                Write-host `t $Web.URL
            }
        }
        Catch {
            write-host -f Red "`tError:" $_.Exception.Message
        }
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

此脚本使用 PnP PowerShell 获取 SharePoint Online 中的所有网站和子网站。

在执行此脚本之前,请确保您对租户中的所有网站集具有网站集管理员权限。否则,您可能会收到“远程服务器返回错误:(401) 未经授权。”错误! PowerShell 将网站集管理员添加到 SharePoint Online 中的所有网站

获取 SharePoint Online 中每个网站集中的子网站计数:

在另一种情况下,我们希望快速获取租户中每个网站集的子网站数量(递归总计)。


#Parameters
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"

#Connect to Admin Center
$Cred = Get-Credential
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
 
#Get All Site collections - Exclude: Seach Center, Redirect site, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0","REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
 
#Loop through each SharePoint site collection
$SubSitesData = @()
ForEach($Site in $SitesCollections)
{
    #Connect to site collection
    Write-host -f Yellow "Processing site:"$Site.Url    
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred
 
    #Get the SubSites of the site collection
    $Webs = Get-PnPSubWeb -Recurse

    #Collect Subsites count
    $SubSitesData += New-Object PSObject -Property ([ordered]@{
        SiteName = $Site.Title
        URL = $Site.URL
        SubSitesCount = $Webs.Length
    })    
}
$SubSitesData

将所有网站和子网站的列表导出到 CSV

如何在 PowerShell 中获取所有 SharePoint 网站和子网站?以下是 PnP PowerShell cmdlet Get-PnPTenantSite 和 Get-PnPSubWeb,用于获取租户中的所有网站集和子网站并将其导出到 CSV 文件。


#Set Variables
$TenantURL = "https://crescent.sharepoint.com"
$ExportFile = "C:\Temp\SitesandSubsites.csv"

$AllSitesList = @()
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $TenantURL -Interactive

    #Get All Site collections - Exclude certain site templates such as personal sites
    $SiteCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0","REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
    Write-host "Total Number of site collections:"$SiteCollections.count

    #Loop through each site collection
    ForEach($Site in $SitesCollections)  
    {  
        #Connect to site collection
        Connect-PnPOnline -Url $Site.Url -Interactive

        #Get all Top-level site and Sub Sitess Of the SharePoint Online site collection
        Get-PnPSubWeb -Recurse -IncludeRootWeb -Includes ParentWeb | ForEach-Object {
            $AllSitesList += New-Object PSObject -property $([ordered]@{
                SiteName  = $_.Title            
                SiteURL = $_.Url
                IsSubSite = If($_.ParentWeb.Title -eq $null) {$False} Else {$True}
            })
        }
    }
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}
#get all sites and subsites in sharepoint online
$AllSitesList

#Export All sites and Subsites list to a CSV list
$AllSitesList | Export-CSV -Path $ExportFile -NoTypeInformation -Encoding UTF8

[玩转系统] SharePoint Online:用于获取所有网站集和子网站的 PowerShell

总之,我们讨论了如何使用 PowerShell 获取 SharePoint 中的所有网站和子网站。使用本指南中的脚本,您可以快速轻松地检索 SharePoint Online 环境中的所有网站和子网站的列表。

经常问的问题:

如何在 SharePoint Online 中启用子网站?

要在 SharePoint Online 中启用子网站,请登录 SharePoint Online 管理中心 >> 单击“设置”>> 单击“经典设置页面”链接。在设置页面的“子网站创建”部分下,将选项设置为“为所有网站启用子网站创建”。
详细信息:在 SharePoint Online 中启用子网站

在 SharePoint Online 中可以创建多少个子网站?

每个网站集有 2,000 个子网站!但是,拥有大量子网站可能会对 SharePoint 环境的性能和可管理性产生影响!

如何授予 SharePoint Online 中子网站的权限?

假设您有一个具有唯一权限的子网站,请按照以下步骤授予对子网站的访问权限:
1.导航到您想要授予子网站权限的 SharePoint Online 网站。
2.单击页面右上角的齿轮图标,然后选择“站点设置”。
3.在“用户和权限”部分下,单击“站点权限”。
4.单击页面顶部功能区中的“授予权限”。
5.输入您想要授予权限的子网站用户或组的电子邮件地址或组名称。
6.选择您想要授予子网站用户或组的权限级别。可用的权限级别包括完全控制、设计、编辑、贡献、读取和有限访问。
7.单击“共享”向子网站用户或组授予权限。
子网站用户或组将收到一封电子邮件通知,其中包含子网站的链接。他们需要单击链接并使用其 Microsoft 帐户登录才能访问子网站。

相关文章:

  • 如何使用 PowerShell 获取 SharePoint Online 中的所有网站集?
  • SharePoint Online:使用 PowerShell 获取所有子网站
  • SharePoint Online:如何使用 PowerShell 创建子网站?

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

取消回复欢迎 发表评论:

关灯