使用 PowerShell 获取 SharePoint Online 中所有网站集中的所有子网站(所有级别)的计数 [英] Get count of all the subsites (all levels) in all site collections in SharePoint Online using PowerShell

查看:65
本文介绍了使用 PowerShell 获取 SharePoint Online 中所有网站集中的所有子网站(所有级别)的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 获取 SharePoint Online 中所有网站集中所有子网站的数量并导出为 CSV.
  2. 以网站集 URL | 格式导出网站集和子网站列表子网站网址.

请注意,无论级别如何,CSV 中的第一列都应包含网站集 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
}

推荐答案

把你的代码改成这个,看看它是否有效:

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

这篇关于使用 PowerShell 获取 SharePoint Online 中所有网站集中的所有子网站(所有级别)的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆