SharePoint Online - Change Access Request Email for All Subsites using PowerShell

This is a PowerShell script that changes the email address for access requests for a SharePoint Online site collection and all subsites within that site collection.
Simply change the variables in the “Set variables here” section inputting your tenant admin username, URL of the site collection, and email you want to use for access requests > Save the file as a .ps1 > Run the script. Prerequisites are noted in the disclaimer section of the file.



## DISCLAIMER:
## Copyright (c) Microsoft Corporation. All rights reserved. This
## script is made available to you without any express, implied or
## statutory warranty, not even the implied warranty of
## merchantability or fitness for a particular purpose, or the
## warranty of title or non-infringement. The entire risk of the
## use or the results from the use of this script remains with you.
##
## Filename:    RequestAccessEmail_SiteCollection.ps1
## Description: This script will change email address for access requests for all sites within a site collection
## Required: SPO Client Components SDK from http://www.microsoft.com/en-us/download/details.aspx?id=42038
## Reference Credit: https://gallery.technet.microsoft.com/office/Set-RequestAccessEmail-for-cc7246a1
#############################################################################

# Set variables here
#########################################################################
#Tenant Admin Account
$AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
$username="admin@juobrie.onmicrosoft.com"
$Url="https://juobrie.sharepoint.com/sites/modern"
$email = "user5@juobrie.onmicrosoft.com"
#########################################################################


#Load Assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

function Set-SPOWebRequestAccessEmail
{
param (
        [Parameter(Mandatory=$true,Position=1)]
[string]$Username,
[Parameter(Mandatory=$true,Position=2)]
[string]$Url,
        [Parameter(Mandatory=$true,Position=3)]
$password,
[Parameter(Mandatory=$true,Position=4)]
[string]$RequestAccessEmail
)


  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  $ctx.Load($ctx.Web)
  $ctx.ExecuteQuery()
     Write-Host "Changing for " $ctx.Web.Url
  $ctx.Web.RequestAccessEmail=$RequestAccessEmail
  $ctx.Web.Update()
   $ctx.ExecuteQuery()

  $ctx.Load($ctx.Web.Webs)
  $ctx.ExecuteQuery()

  if($ctx.Web.Webs.Count -ne 0)
  {
     foreach($webbie in $ctx.Web.Webs)
     {
        

        Set-SPOWebRequestAccessEmail -Username $Username -Url $webbie.Url -password $password -RequestAccessEmail $RequestAccessEmail


     }

  }

  }

Set-SPOWebRequestAccessEmail -Username $username -Url $Url -password $AdminPassword -RequestAccessEmail $email

Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.