@NicolasKheirallahHere's my version of applying a template to a new site.
Hope it's not too buggy. Works for me.
You will need to supply your own JSON and XML file.
Thanks for all your input on this project.
# Managing creation of department SharePoint sites using a standard template
# This script has 4 functions
#============================================================
# Menu Function
#============================================================
Function ShowMenu
{
param([string]$title = 'MenuName')
Clear-Host
Write-Host `n"================ $title =================="`n -ForegroundColor White -BackgroundColor DarkGreen
Write-Host " Enter 1 to: Create New SharePoint Site" -ForegroundColor White -BackgroundColor DarkGreen
Write-Host " Enter 2 to: Create New XML Template File" -ForegroundColor White -BackgroundColor DarkGreen
Write-Host " Enter 3 to: Apply MIU Template to Site" -ForegroundColor White -BackgroundColor DarkGreen
Write-Host " Enter E to: Exit Script"`n -ForegroundColor White -BackgroundColor DarkGreen
}
#=========================================================================================
# Create SharePoint Team Site
#
# This function will create a basic Team site in SPO. The site will have General folders in the Document
# repository, an Attention Issues List, and some Quick links to Knowledge Base.
# You will need to manually update the Site Theme and Header Info.
#=========================================================================================
Function Create_SPO
{
clear-host
$Name = "" # Site name WITH spaces
$Sn = "" # Site name WITHOUT spaces to append to SPO URL
$Own = "" # Owner's Username - ****@miu.edu
$T = "STS#3" # Template Type - use Get-SPOWebTemplate to get a list of valid templates
$SQ = 1024 # Storage Quota - in Megabytes. Can't exceed 25tb (our tenent max quota)
$TZ = 11 # SP Time Zone Collection -
https://learn.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.spregionalsettings.timezones?view=...Write-Host `n" Create a SharePoint Site "`n -ForegroundColor White -BackgroundColor DarkGreen
do
{
$v = $false
$Name = (Read-Host -Prompt "Please enter a name for your New Site or type E to Exit")
$Sn = $Name -replace '\s', ''
If($Sn -eq '')
{
write-host `n'Name Not Entered.' -ForegroundColor White -BackgroundColor DarkGreen
}
else
{
If($Sn.ToUpper() -eq 'E')
{
$v = $true
}
else
{
# we have a name we need to test to see if name is currently being used
$Url = $Url + $Sn
try{
Get-SPOSite -identity $Url -ErrorAction Stop
#if we DO NOT error out that means there is a SP site with this name already
write-host `n"This name is being used. Please enter a different name."
}
catch{
# Get-SPOSite Errored out - No Site with that name found
$v = $true
}
}
}
}until($v = $true)
if($Sn.ToUpper() -ne 'E')
{
do
{
$v = $false
$Own = (Read-Host `n"Please enter the MIU Username of the Owner or E to Exit")
If($Own.ToUpper() -eq 'E')
{
$v = $true
}
else
{
try
{
Get-MsolUser -UserPrincipalName $Own -ErrorAction Stop
$v = $true
}
catch
{
write-host `n"Username not Found. Re-enter Username"`n -ForegroundColor White -BackgroundColor DarkGreen
}
}
}until($v -eq $true)
# at this point we should either have a (valid site name and valid username) or an E
# only a valid site name and username will trigger the New-SPOSite cmdlet
If($Own.ToUpper() -ne 'E')
{
New-SPOSite -Url $Url -Template $T -Owner $Own -StorageQuota $sq -Title $Name -TimeZoneId $tz -NoWait
}
}
}
#=========================================================================================
# Create New XML File
#
# If you make changes to your SPO Template, you must run this function to update your existing XML file.
# If you do not, the changes you made to the Template will not be applied.
#=========================================================================================
Function Create_XML
{
$c = ""
Write-Host `n "**** Create/Update XML file from SPO Template ****"`n -ForegroundColor white -BackgroundColor darkgreen
$c = (Read-Host -Prompt "Create Xml File? (Y/N)").ToUpper()
if ($c -eq "Y")
{
Connect-PnpOnline -Credential $Cr -Url $Tsite
Get-PnPSiteTemplate -Configuration $jsonPath -Out $xmlPath
Write-Host `n"*** XML created and saved in " + $xmlPath +" ***"`n -ForegroundColor white -BackgroundColor darkgreen
}
}
#=========================================================================================
# Apply Template to New Site
#
# This Function requests the name of any SPO site where you want to apply the Template attributes.
#=========================================================================================
Function Apply_Template
{
$NewSite = ""
$c = ""
Write-Host `n "**** Apply Template Attributes to SP Site ****"`n -ForegroundColor white -BackgroundColor darkgreen
$c = (Read-Host -Prompt "Apply XML Template to a site?(Y/N)")
If($c.ToUpper() -eq "Y")
{
do
{
$Newsite = (Read-Host -Prompt "Please enter a name of the Site to apply the template or E to Exit")
$Newsite = $Newsite -replace '\s', ''
If($NewSite -eq '')
{
write-host `n'Please enter a Site Name or E to exit' -ForegroundColor White -BackgroundColor DarkGreen
}
}until($NewSite -ne '')
if($NewSite -ne 'E')
{
$newSite = $Url + $NewSite
Connect-PnpOnline -Credential $Cr -Url $Newsite
Invoke-PnPSiteTemplate -Path $xmlPath
Write-Host `n"*** Tempate Applied - Please update Site Thumbnail, Logo, and Theme ***"`n -ForegroundColor white -BackgroundColor darkgreen
}
}
}
#=========================================================================================
# Script to call above functions
#=========================================================================================
Clear-Host
# replace ***** with your credential file name
$Cr = Import-Clixml -Path "${env:\userprofile}\*****.xml"
[string]$Url = "https://<Your SP site>.sharepoint.com/sites/"
[string]$Tsite = "https://<Your SP site>.sharepoint.com/sites/Your SiteTemplate Name"
[string]$jsonPath = "C:\Users\SharePoint\Site Templates\SPTemplate.json"
[string]$xmlPath = "C:\Users\SharePoint\Site Templates\PnPOutFile.xml"
write-host "To create a new SPO site, You MUST sign into MSOL and SPO services with your Credentials"`n -ForegroundColor White -BackgroundColor DarkGreen
$ask = (Read-host "Connect to MS Modules with Credentials? (Y/N)")
if($ask.ToUpper() -eq 'Y')
{
Connect-MsolService -Credential $Cr
Connect-SPOService -Credential $Cr -Url "https://<Your Admin SP site>.sharepoint.com/"
}
do
{
$inp = ""
ShowMenu -title "SharePoint Online Template App"
$inp = (Read-host "Please make a selection")
switch ($inp)
{
'1' {Create_SPO}
'2' {Create_XML}
'3' {Apply_Template}
}
}until($inp.ToUpper() -eq 'E')
write-host `n"***** Exiting Script *****"`n -ForegroundColor White -BackgroundColor DarkGreen