Forum Discussion

Audi86's avatar
Audi86
Brass Contributor
Dec 08, 2022

Looking to add a line to check for existing email if already there

Hi,

I have researched and came up this code to add email added in the access request field in SharePoint Subsites. I tested the script and its working fine. I am looking to modify this script to see if there is already an email present then dont overwrite it to my email. Can you advise how to create that check. Can you please advise on my code below or any link that will help me to modify:

 

 

 

$SiteURL = "https://MySite.sharepoint.com";


$Cred = Get-Credential

Try {
 
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

 
    $subwebs = Get-PnPSubWeb -Recurse
   
    foreach ($Subweb in $subwebs)
    {
      Write-Host $Subweb.Url
        $web = Connect-PnPOnline -Url $Subweb.Url -Credentials $Cred
      
     
      Set-PnPRequestAccessEmails -Emails @("email address removed for privacy reasons")
       
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

 

 

 

  • Hello Audi86,

    You can get list of emails that are present there and if they are not empty set your email(s).

    Something like this: ( Add this code instead your line #19)

    $RequestAccessEmails=Get-PnPRequestAccessEmails
    if(!$RequestAccessEmails){
        Set-PnPRequestAccessEmails -Emails @("email address removed for privacy reasons")
    }

     Hope that helps.

     

  • AndySvints's avatar
    AndySvints
    Steel Contributor

    Hello Audi86,

    You can get list of emails that are present there and if they are not empty set your email(s).

    Something like this: ( Add this code instead your line #19)

    $RequestAccessEmails=Get-PnPRequestAccessEmails
    if(!$RequestAccessEmails){
        Set-PnPRequestAccessEmails -Emails @("email address removed for privacy reasons")
    }

     Hope that helps.

     

Resources