Получить подсчет всех дочерних сайтов (всех уровней) во всех семействах сайтов в SharePoint Online с помощью PowerShell - PullRequest
0 голосов
/ 22 октября 2019

Я пытаюсь получить все семейства сайтов и соответствующие дочерние сайты на всех уровнях семейства сайтов с помощью PowerShell. Есть так много постов и ссылок, которые описывают путь, но я не смог найти пост, который может обеспечить количество дочерних сайтов (снова всех уровней) в семействе сайтов. Моя среда - SharePoint Online. Мне нужны две вещи:

  1. Получить подсчет всех дочерних сайтов во всех семействах сайтов в SharePoint Online и экспортировать в CSV.
  2. Экспортировать список семейств сайтов и дочерних сайтов в формате SiteКоллекция 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\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 Ответ

0 голосов
/ 23 октября 2019

Измените свой код на это, проверьте, работает ли он:

    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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...