forked from jameswh3/MW-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-HubSites.ps1
More file actions
55 lines (44 loc) · 2.01 KB
/
Copy pathNew-HubSites.ps1
File metadata and controls
55 lines (44 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<#
.SYNOPSIS
Creates SharePoint Online Hub Sites using PnP.PowerShell, with optional parent hub site association.
.DESCRIPTION
This script creates one or more SharePoint Online Hub Sites using the PnP.PowerShell module.
Optionally, it can associate the new hub site with a parent hub site if specified.
.PARAMETER SiteUrls
Array of site URLs to register as hub sites.
.PARAMETER ParentHubSiteId
(Optional) The Hub Site ID (GUID) of the parent hub site to associate with.
.EXAMPLE
.\New-HubSites.ps1 -SiteUrls "https://contoso.sharepoint.com/sites/Hub1","https://contoso.sharepoint.com/sites/Hub2"
.\New-HubSites.ps1 -SiteUrls "https://contoso.sharepoint.com/sites/Hub3" -ParentHubSiteId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
#>
param(
[Parameter(Mandatory = $true)]
[string[]]$SiteUrls,
[Parameter(Mandatory = $false)]
[string]$ParentHubSiteId
)
# Ensure PnP.PowerShell is installed and imported
if (-not (Get-Module -ListAvailable -Name "PnP.PowerShell")) {
Install-Module -Name "PnP.PowerShell" -Scope CurrentUser -Force
}
Import-Module PnP.PowerShell -ErrorAction Stop
# Connect to SharePoint Online (interactive)
Write-Host "Connecting to SharePoint Online..." -ForegroundColor Cyan
Connect-PnPOnline -Interactive
foreach ($siteUrl in $SiteUrls) {
Write-Host "Registering $siteUrl as a hub site..." -ForegroundColor Yellow
try {
$hubSite = Register-PnPHubSite -Site $siteUrl -ErrorAction Stop
Write-Host "Hub site registered: $($hubSite.Title) ($siteUrl)" -ForegroundColor Green
if ($ParentHubSiteId) {
Write-Host "Associating $siteUrl with parent hub site ID $ParentHubSiteId..." -ForegroundColor Cyan
Add-PnPHubSiteAssociation -Site $siteUrl -HubSiteId $ParentHubSiteId -ErrorAction Stop
Write-Host "Associated $siteUrl with parent hub site." -ForegroundColor Green
}
}
catch {
Write-Host "Error processing $siteUrl $_" -ForegroundColor Red
}
}
Write-Host "Script completed." -ForegroundColor Magenta