使用 PowerShell 获取 SharePoint Online 中所有网站集中所有子网站(所有级别)的计数

Get count of all the subsites (all levels) in all site collections in SharePoint Online using PowerShell

我正在尝试使用 PowerShell 获取网站集中所有级别的所有网站集和相应的子网站。有很多 posts 和参考概述了方法,但我找不到 post 可以提供网站集中子网站(所有级别)的计数。我的环境是 SharePoint Online。我需要两件事:

  1. 获取 SharePoint Online 中所有网站集中所有子网站的计数并导出为 CSV。
  2. 以网站集 URL 格式导出网站集和子网站列表 |子站点 URL.

请注意,无论级别如何,CSV 中的第一列都应包含网站集 URL。

我试过以下代码但无法正常工作。请帮帮我。

#Add the PowerShell module
Import-Module Microsoft.Online.SharePoint.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

Try {
    Function Get-SPOWebs($SiteURL)
    {
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        $SiteCollURL = $SiteURL

    #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.Title "-" $Web.Url

    $SubsitesResultSet = @()
    $SubsiteResult = new-object PSObject
    $SubsiteResult | add-member -membertype NoteProperty -name "SiteCollURL" -Value $SiteCollURL
    $SubsiteResult | add-member -membertype NoteProperty -name "SubSiteURL" -Value $Web.Url
    $SubsitesResultSet += $SubsiteResult

    $WebsCount++
    Write-Host "Total Number of Subsites: "$WebsCount

    #Loop through each each subsite in site
    Foreach($Web in $web.Webs)
    {
        #Call the function again to get all sub-sites in the site (web)
        Get-SPOWebs($Web.Url)
        $WebsCount++
    }
    return $WebsCount
}
#Admin Center and CSV File Location Variables
$AdminCenterURL = "https://tenant-admin.sharepoint.com"
$SiteCollCSVFile = "..\SiteCollectionsData.csv"
$SiteCollAndSubSitesCSV = "..\SiteCollectionsAndSubsitesData.csv"
#$WebsCount = 0

#Setup Credentials to connect 
$Cred= Get-Credential

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential ($Cred)

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

#Loop through each site collection and retrieve details
$ResultSet = @()
Foreach ($Site in $SiteCollections)
{
    #Write-Host "Processing Site Collection :"$Site.URL -f Yellow

    #Get site collection details   
    $Result = new-object PSObject
    $Result | add-member -membertype NoteProperty -name "Title" -Value $Site.Title
    $Result | add-member -membertype NoteProperty -name "Url" -Value $Site.Url
    $Result | add-member -membertype NoteProperty -name "LastContentModifiedDate" -Value $Site.LastContentModifiedDate
    $Result | add-member -membertype NoteProperty -name "Status" -Value $Site.Status
    $Result | add-member -membertype NoteProperty -name "LocaleId" -Value $Site.LocaleId
    $Result | add-member -membertype NoteProperty -name "LockState" -Value $Site.LockState
    $Result | add-member -membertype NoteProperty -name "StorageQuota" -Value $Site.StorageQuota
    $Result | add-member -membertype NoteProperty -name "StorageQuotaWarningLevel" -Value $Site.StorageQuotaWarningLevel
    $Result | add-member -membertype NoteProperty -name "Used" -Value $Site.StorageUsageCurrent
    $Result | add-member -membertype NoteProperty -name "CompatibilityLevel" -Value $Site.CompatibilityLevel
    $Result | add-member -membertype NoteProperty -name "Template" -Value $Site.Template
    $Result | add-member -membertype NoteProperty -name "SharingCapability" -Value $Site.SharingCapability

    $ResultSet += $Result

} 

#Export Result to csv file
$ResultSet |  Export-Csv $SiteCollCSVFile -notypeinformation

#Loop through site collections
ForEach($Site in $SiteCollections)
{
    #Call the function to get all sub-sites in site collection
    Write-Host "Getting subsites for site collection: "$Site.Title -foregroundcolor Yellow
    Get-SPOWebs($Site.URL)
    Write-Host "Total Number of Subsites: "$WebsCount
    Write-Host "=================================================================================="
}

#Export Subsites Resultset to CSV
$SubsitesResultSet |  Export-Csv $SiteCollAndSubSitesCSV -notypeinformation

}

Catch [Exception] 
{
    #Write-Output $_.Exception.GetType().FullName, $_.Exception.Message
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    continue

    #To get the detailed exception, use the following command
    #Write-Output $_.Exception|format-list -force
}

将你的代码改成这样,检查它是否有效:

    Function Get-SPOWebs($SiteURL)
    {

     $SiteCollURL = $SiteURL

    #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.Title "-" $Web.Url  
    $SubsiteResult = new-object PSObject
    $SubsiteResult | add-member -membertype NoteProperty -name "SiteCollURL" -Value $SiteCollURL
    $SubsiteResult | add-member -membertype NoteProperty -name "SubSiteURL" -Value $Web.Url
    $SubsitesResultSet += $SubsiteResult

    $WebsCount++

    #Loop through each each subsite in site
    Foreach($Web in $web.Webs)
    {
        #Call the function again to get all sub-sites in the site (web)
        $WebsCount=Get-SPOWebs($Web.Url)+1
    }
    return $WebsCount
}
#Admin Center and CSV File Location Variables
$AdminCenterURL = "https://tenant-admin.sharepoint.com"
$SiteCollCSVFile = "..\SiteCollectionsData.csv"
$SiteCollAndSubSitesCSV = "..\SiteCollectionsAndSubsitesData.csv"
#$WebsCount = 0

#Setup Credentials to connect 
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential ($Cred)

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

#Loop through each site collection and retrieve details
$ResultSet = @()
$SubsitesResultSet = @()
Foreach ($Site in $SiteCollections)
{
    #Write-Host "Processing Site Collection :"$Site.URL -f Yellow

    #Get site collection details   
    $Result = new-object PSObject
    $Result | add-member -membertype NoteProperty -name "Title" -Value $Site.Title
    $Result | add-member -membertype NoteProperty -name "Url" -Value $Site.Url
    $Result | add-member -membertype NoteProperty -name "LastContentModifiedDate" -Value $Site.LastContentModifiedDate
    $Result | add-member -membertype NoteProperty -name "Status" -Value $Site.Status
    $Result | add-member -membertype NoteProperty -name "LocaleId" -Value $Site.LocaleId
    $Result | add-member -membertype NoteProperty -name "LockState" -Value $Site.LockState
    $Result | add-member -membertype NoteProperty -name "StorageQuota" -Value $Site.StorageQuota
    $Result | add-member -membertype NoteProperty -name "StorageQuotaWarningLevel" -Value $Site.StorageQuotaWarningLevel
    $Result | add-member -membertype NoteProperty -name "Used" -Value $Site.StorageUsageCurrent
    $Result | add-member -membertype NoteProperty -name "CompatibilityLevel" -Value $Site.CompatibilityLevel
    $Result | add-member -membertype NoteProperty -name "Template" -Value $Site.Template
    $Result | add-member -membertype NoteProperty -name "SharingCapability" -Value $Site.SharingCapability

    $ResultSet += $Result

} 

#Export Result to csv file
$ResultSet |  Export-Csv $SiteCollCSVFile -notypeinformation

#Loop through site collections
ForEach($Site in $SiteCollections)
{
    #Call the function to get all sub-sites in site collection
    Write-Host "Getting subsites for site collection: "$Site.Title -foregroundcolor Yellow
    $WebsCount=Get-SPOWebs($Site.URL)
    Write-Host "Total Number of Subsites: "$WebsCount
    Write-Host "=================================================================================="
}

#Export Subsites Resultset to CSV
$SubsitesResultSet |  Export-Csv $SiteCollAndSubSitesCSV -notypeinformation