Я пытаюсь получить все семейства сайтов и соответствующие дочерние сайты на всех уровнях в семействе сайтов с помощью PowerShell. Есть так много сообщений и ссылок, которые описывают путь, но я не смог найти сообщение, которое могло бы предоставить количество дочерних сайтов (снова всех уровней) в семействе сайтов. Моя среда - 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\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
}
0
Suhas 22 Окт 2019 в 19:44

1 ответ

Лучший ответ

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

    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
0
Michael Han_MSFT 23 Окт 2019 в 09:37