Skip to content

Instantly share code, notes, and snippets.

@OlafD
Last active November 28, 2018 15:25
Show Gist options
  • Save OlafD/41948778e8e9e0f541a85b3a3cea8153 to your computer and use it in GitHub Desktop.
Save OlafD/41948778e8e9e0f541a85b3a3cea8153 to your computer and use it in GitHub Desktop.
The starting point for a PowerShell script that loops all sites of a site collection to perform any function. The function(s) could be inserted starting in line 27. The script will write a transcript, the filename could be modified in line 51.
param (
[Parameter(Mandatory=$true)]
[string]$Url,
[Parameter(Mandatory=$true)]
[string]$TranscriptPath,
[Parameter(Mandatory=$true)]
$Credentials,
[switch]$IgnoreRoot
)
function DoWeb
{
param (
[Parameter(Mandatory=$true)]
[string]$Url,
[Parameter(Mandatory=$true)]
$Credentials,
[switch]$Ignore
)
Connect-PnPOnline -Url $Url -Credentials $Credentials
if ($Ignore.ToBool() -eq $false)
{
Write-Host -NoNewline "Doing $Url "
# place your code to run here
Write-Host "Done."
}
$web = Get-PnPWeb -Includes Webs
foreach ($subweb in $web.Webs)
{
DoWeb -Url $subweb.Url -Credentials $Credentials
}
}
#
# MAIN
#
if ($Credentials -eq $null)
{
$Credentials = Get-Credential
}
$transcriptExtension = Get-Date -Format yyyyMMdd-HHmmss
$transcriptFile = "$TranscriptPath\LoopAllSubwebs_EmptyScript_Transcript_$transcriptExtension.txt"
Start-Transcript -Path $transcriptFile
Connect-PnPOnline -Url $Url -Credentials $Credentials
if ($IgnoreRoot.ToBool() -eq $true)
{
DoWeb -Url $Url -Credentials $Credentials -Ignore
}
else
{
DoWeb -Url $Url -Credentials $Credentials
}
Write-Host -ForegroundColor Green "Done."
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment