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

筅森魡賤 提交于 2020-03-05 05:08:24

问题


I'm trying to get all site collections and respective subsites at all levels within the site collection using PowerShell. There are so many posts and references which outline the way but I could not find a post which can provide the count of subsites (all levels again) in a site collection. My environment is SharePoint Online. I need two things:

  1. Get the count of all subsites in all site collections in SharePoint Online and export to CSV.
  2. Export the list of site collections and subsites in the format Site Collection URL | Subsite URL.

Please note that irrespective of the level, the first column in the CSV should have Site Collection URL.

I've tried the following code but not able to get it to work. Please help me out.

#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\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"

#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
}

回答1:


Change your code to this, check if it works:

    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


来源:https://stackoverflow.com/questions/58508878/get-count-of-all-the-subsites-all-levels-in-all-site-collections-in-sharepoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!