SharePoint Online: Export Access Requests Settings using PowerShell

Requirement: Export access requests settings for all sites in SharePoint Online using PowerShell

SharePoint Online: PowerShell to Export Access Requests Settings (Email Address, Group Configuration)
To export access requests settings from all sites in the SharePoint Online tenant, use this PowerShell:
#Parameter
$TenantAdminURL = "https://crescent-Admin.SharePoint.com"
$CSVPath = "C:\Temp\AcccessRequestData.csv"
$AccessRequestData= @()
 
#Function to Get access request Configuration for a SharePoint Online site
Function Get-PnPAccessRequestConfig
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web
    )
 
    Try {
        Write-host -f Yellow "Getting Access Request Settings on:"$web.Url
        If($Web.HasUniqueRoleAssignments)
        {
            #Get Access Requests Configuration
            If($Web.RequestAccessEmail -ne [string]::Empty)
            {
                #Get Access Request Email
                $AccessRequest = "Enabled"
                $EmailOrGroup = "Email"
                $AccessRequestConfig = $Web.RequestAccessEmail
                Write-host -f Green "`tAccess Requests is '$AccessRequest', Set to '$EmailOrGroup' as '$AccessRequestConfig'!"
            }
            Elseif($Web.UseAccessRequestDefault -eq $true)
            {
                #Get Associated Owners Group
                $AccessRequest = "Enabled"
                $EmailOrGroup = "Default Owner Group"
                #Get the Default Owners Group of the site
                $OwnersGroup = Get-PnPGroup -AssociatedOwnerGroup
                $AccessRequestConfig = $OwnersGroup.Title
                Write-host -f Green "`tAccess Requests is '$AccessRequest', Set to '$EmailOrGroup' as '$AccessRequestConfig'!"
            }
            Else
            {
                #Access Request is Disabled!
                $AccessRequest = "Disabled"
                $EmailOrGroup = ""
                $AccessRequestConfig = ""
                Write-host -f Green "`tAccess Requests is $AccessRequest!"
            }            
        }
        Else
        {
            $AccessRequest = "Inherits from Parent"
            $EmailOrGroup = ""
            $AccessRequestConfig = ""
            Write-host -f Yellow "`tWeb inherits permissions from the parent!"$web.Url
        }

        #Collect Data
        $AccessRequestData+= New-Object PSObject -Property ([ordered]@{
                WebURL  = $Web.URL
                AccessRequest = $AccessRequest
                EmailOrGroup = $EmailOrGroup
                AccessRequestConfig =  $AccessRequestConfig
           })
           #Export Data to CSV
            $AccessRequestData | Export-Csv -Path $CSVPath -NoTypeInformation -append

    }
    Catch {
        write-host "`tError Getting Access Requests: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Connect to Admin Center
$Cred = Get-Credential
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred

If(Test-Path $CSVPath) { Remove-Item $CSVPath }

#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
  
#Loop through each site collection
ForEach($Site in $SitesCollections)
{
    #Connect to site collection
    $SiteConn = Connect-PnPOnline -Url $Site.Url -Credentials $Cred
 
    #Call the Function for Root Web and all Subwebs
    Get-PnPWeb  -Connection $SiteConn -Includes HasUniqueRoleAssignments, RequestAccessEmail, UseAccessRequestDefault| Get-PnPAccessRequestConfig
    Get-PnPSubWebs -Connection $SiteConn -Recurse -Includes HasUniqueRoleAssignments, RequestAccessEmail, UseAccessRequestDefault | ForEach-Object { Get-PnPAccessRequestConfig $_ } 
    Disconnect-PnPOnline -Connection $SiteConn
}
This PowerShell script gets the inventory of all access request settings and exports to a CSV file.
sharepoint online access requests

Related posts:

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.