Tìm kiếm trang web

PowerShell để nhận tất cả các trang web được liên kết với các trang Hub trong SharePoint Online


Yêu cầu: Nhận liên kết site trung tâm trong SharePoint Online bằng PowerShell.

SharePoint Online: Nhận liên kết site Hub bằng PowerShell

Các trang SharePoint Online Hub giúp sắp xếp các trang liên quan vào các cổng tập trung dựa trên các thuộc tính tổ chức như dự án, nhóm, khái niệm, phòng ban, khu vực, v.v. Nếu bạn chưa quen với Hub Sites trong Office 365, tôi khuyên bạn nên xem liên kết này trước khi tiếp tục. Trang Hub trong SharePoint Online được giải thích trong bài viết khác của tôi: Định cấu hình Trang Hub trong SharePoint Online

Làm cách nào để có được tất cả các trang Hub và bộ sưu tập trang web liên kết của nó?

Để có được tất cả các liên kết trang trung tâm, hãy truy cập:

  1. Đăng nhập vào Trung tâm quản trị SharePoint Online >> Nhấp vào “Trang web hoạt động”
  2. Lọc dựa trên trang Hub cụ thể

Điều này cung cấp cho bạn danh sách các site được liên kết với site Hub cụ thể.


PowerShell để có được các liên kết Hub-Site trong SharePoint Online

Đây là PowerShell để lấy tất cả các site được liên kết với các site Hub trong SharePoint Online:


#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
 
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Get all Hubsites
$HubSites = Get-SPOHubSite

#Get all site collections
$Sites = Get-SPOSite -Limit All  

#Iterate through site collections to get associated sites with the hub
ForEach($Hub in $HubSites)
{
    Write-host "Hub-Site:"$Hub.SiteUrl
    #Get the ID of the Hubsite
    $HubSiteId = $Hub.ID.GUID

    #Iterate through each site collection
    ForEach($Site in $Sites)
    {
        #get the Associated Hubsite ID
        $AssociatedHubSiteID = (Get-SPOSite $site.Url).HubSiteID.GUID

        If($AssociatedHubSiteID -eq $HubSiteId)
        {
            Write-Host "`t $($Site.Url)"
        }
    }
}

Tập lệnh PowerShell này cung cấp cho bạn danh sách tất cả các site Hub và các tuyển tập site được liên kết của chúng. Để có được các trang Hub được liên kết của một tuyển tập trang cụ thể, hãy sử dụng như sau:


#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/intranet"
 
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Get the Hubsite ID
$HubSiteId = (Get-SPOHubSite | Where {$_.SiteUrl -eq $HubSiteURL}).Id.Guid

#Get all site collections
$Sites = Get-SPOSite -Limit All  

#Iterate through site collections to get associated sites with the hub
ForEach($Site in $Sites)
{
    Try {
        #Get the Associated Hubsite ID of the site collection
        $AssociatedHubSiteID = (Get-SPOSite $site.Url).HubSiteID.GUID

        If($AssociatedHubSiteID -eq $HubSiteId)
        {
            Write-Host "`t $($Site.Url)"
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

Nhận tất cả các trang web được liên kết với một trang Hub bằng PnP PowerShell

Đây là một ví dụ về cách sử dụng PnP PowerShell để lấy tất cả các site được liên kết với một site Hub:


#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/intranet"

#Connect to Pnp Online
Connect-PnPOnline -Url $AdminCenterURL  -Credentials (Get-Credential) #-Interactive

#Get the hub site id
$HubSiteID = (Get-PnPTenantSite $HubSiteURL).HubSiteId

#Get all site collections associated with the hub site
Get-PnPTenantSite -Detailed | select url | ForEach-Object { 
  $Site = Get-PnPTenantSite $_.url 
  If($Site.HubSiteId -eq $HubSiteId){
    write-host $Site.url
  }
}

Chúng tôi có PowerShell để tải tất cả các tuyển tập trang được liên kết với trang Hub. Bây giờ, hãy áp dụng nó vào thực tế bằng cách đặt tất cả các trang web được liên kết sang chế độ chỉ đọc!


#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/intranet"

#Connect to Pnp Online
Connect-PnPOnline -Url $AdminCenterURL  -Credentials (Get-Credential) #-Interactive

#Get the hub site id
$HubSiteID = (Get-PnPTenantSite $HubSiteURL).HubSiteId

#Loop through site collections
$Sites  = Get-PnPTenantSite
ForEach($Site in $Sites)
{
    #Get the Site collection
    $Site = Get-PnPTenantSite $Site.url -ErrorAction SilentlyContinue
    If($Site)
    { 
        If($Site.HubSiteId -eq $HubSiteId)
        {
            write-host "Setting Site Collection to Read-only:"$Site.url
            Set-PnPTenantSite -Url $Site.URL -LockState ReadOnly
        }
    }
}

Bài viết liên quan