Skip to content

Instantly share code, notes, and snippets.

@danawesome
Created September 13, 2024 21:30
Show Gist options
  • Select an option

  • Save danawesome/0b0fb767740187e09f4f32150a0fcfed to your computer and use it in GitHub Desktop.

Select an option

Save danawesome/0b0fb767740187e09f4f32150a0fcfed to your computer and use it in GitHub Desktop.
SharePoint 2019 On-prem Update Access request email addresses in Site Collection(s)
<#
.SYNOPSIS
This script updates the Request Access email address for all the webs for
a single Site Collection or Web Application's Site Collections.
.DESCRIPTION
This script searches through a site collection or web application's site collections
looking for the current email set for Access Requests, and replaces the current email
with the email provided in the script.
A report of the activity is created. In addition a count of the current entries and
updates entries are part of the report.
This assumes use on an On-prem SharePoint Server. Tested with 2019.
Author: https://github.com/danawesome
License: MIT
.PARAMETER ReportOnly
This switch can be used when caling the script to generate a report of the current state
of assigned emails.
#>
param([switch]$ReportOnly)
# Add SharePoint PowerShell snapin if not already loaded
if ($null -eq (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue)) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$script:countChanged = 0;
$script:countCurrent = 0;
# Function to update Access Request Settings
function Update-AccessRequestSettings {
param (
[Parameter(Mandatory=$true)]
[Microsoft.SharePoint.SPWeb]$Web,
[Parameter(Mandatory=$true)]
[string]$NewEmail
)
$currentEmail = $Web.RequestAccessEmail
if(!$ReportOnly){Write-Host "Real Deal"}
if ($currentEmail -ne $NewEmail) {
Write-Host "Site $($Web.Url) - Current email: $currentEmail, Updating to: $NewEmail"
if(!$ReportOnly){
$Web.RequestAccessEmail = $NewEmail
$Web.Update()
$script:countChanged += 1
}
} else {
Write-Host "Site $($Web.Url) - Current email: $currentEmail (No change needed)"
$script:countCurrent += 1
}
}
# Function to process a single site collection
function Update-SiteCollection {
param (
[Parameter(Mandatory=$true)]
[Microsoft.SharePoint.SPSite]$Site,
[Parameter(Mandatory=$true)]
[string]$NewEmail
)
Write-Host "Processing Site Collection: $($Site.Url)"
try {
# Get all webs (sub-sites) in the site collection
$webs = $Site.AllWebs
foreach ($web in $webs) {
try {
Update-AccessRequestSettings -Web $web -NewEmail $NewEmail
}
finally {
if ($null -ne $web) {
$web.Dispose()
}
}
}
}
finally {
if ($null -ne $Site) {
$Site.Dispose()
}
}
}
Start-Transcript -Path "C:\temp\AccessRequestEmailLog.txt" -Append # Replace with your desired log path.
# Main script
$NewAccessRequestEmail = "[email protected]" # Replace with your desired email address
$SingleSiteCollectionUrl = $null # Set this to a URL to process a single site collection, or leave as $null to process all
if($ReportOnly){
Write-Host "Report Only"
}
if ($SingleSiteCollectionUrl) {
# Process single site collection
$site = Get-SPSite $SingleSiteCollectionUrl
Update-SiteCollection -Site $site -NewEmail $NewAccessRequestEmail
}
else {
# Process all site collections
$webApps = Get-SPWebApplication
foreach ($webApp in $webApps) {
Write-Host "Processing Web Application: $($webApp.Url)"
# Get all site collections in the web application
$sites = $webApp.Sites
foreach ($site in $sites) {
Update-SiteCollection -Site $site -NewEmail $NewAccessRequestEmail
}
}
}
Write-Host "Emails Current: $script:countCurrent"
Write-Host "Emails Updated: $script:countChanged"
Write-Host "Script completed."
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment