PI Services

Le blog des collaborateurs de PI Services

Powershell : Lister les membres externes des sites Sharepoint

Dans certain cas de figure nous sommes amenés à devoir faire une liste de tout accès externe à différents types de ressources, dans le cas présent nous allons lister l'ensemble des sites Sharepoint avec un partage externe, pour ce faire vous aurez besoin de :

  • Powershell 5.1,
  • Le module Sharepoint Online,
  • un accès Sharepoint Admin.

Vous pouvez maintenant lancer le script en prenant soin de modifier les url et identifiants (L2, L21, L74 et L88).

# Connection to Sharepoint Online
Connect-SPOService -Url https://contoso-admin.sharepoint.com # (Please change the connection URL)

# Define variables
$ArraySP = @()
$i = 0
$x = 0

# Get the list of All 
$AllSPSite = Get-SPOSite -Limit All
$AllSPSite = $AllSPSite | sort Url

$AllSPSite | foreach {
    $I++
    $Url = $_.Url
    $Owner = $_.Owner
    $Title = $_.title

    # If you are not in the Admin Group you will need to Grant your account as Admin
    Try {
        Set-SPOUser -Site $Url -LoginName "mathieu@contoso.com" -IsSiteCollectionAdmin:$true -ErrorAction Stop
        $Result = $true
        }
    Catch {
        Write-Warning $($_)
        Write-Output $Url | Add-Content C:\temp\SPError.txt
        $Result = $false
        }

    # If you're Admin you can check
    If ($Result -eq $true) {
        # Check Guest Access
        Try {
            # Get Members
            $Members = Get-SPOUser -Site $Url -Limit All -ErrorAction stop
            $External = $Members.where({$_.Usertype -eq "Guest"})

            If ($External.count -ne 0) {
                Write-Host "External found on $Title" -ForegroundColor Cyan
                $External.count
                $External | ForEach {
                    $DisplayName = $_.DisplayName
                    $LoginName = $_.LoginName
                    $Groups = $_.Groups

                    # Store Data
                    $ArraySP += New-Object psobject -Property @{
                        Url = $Url
                        Owner = $Owner
                        Title = $title
                        DisplayName = $DisplayName
                        LoginName = $LoginName
                        Groups = $Groups
                        }
                    # Release
                    $DisplayName = $null
                    $LoginName = $null
                    $Groups = $null
                    }
                }
            # Release
            $Members = $null
            $External = $null
            }
        Catch {
            Write-Warning $($_)
            Write-Host "$Title not Accessible" -ForegroundColor Yellow
            Write-Host "$Url not Accessible" -ForegroundColor Magenta
            Write-Output $Url | Add-Content C:\temp\SPError.txt
            }
        
        # Remove Admin Rights
        Try {
            Set-SPOUser -Site $Url -LoginName "mathieu@contoso.com" -IsSiteCollectionAdmin:$false
            }
        Catch {
            Write-Warning $($_)
            Write-Output $Url | Add-Content C:\temp\SPError.txt
            }
        }
    
    # Release
    $Url = $null
    $Owner = $null
   
    # After 200 connection, reconnect to Sharepoint
    If ($x -eq 200) {
        Connect-SPOService -Url https://contoso-admin.sharepoint.com # (Please change the connection URL)
        $x = 0
        }
    $x++
    }

$ArraySP | Export-Csv c:\temp\ExternalSharepointMembers.csv -Encoding UTF8 -Delimiter ";" -NoTypeInformation

 

Ajouter un commentaire

Loading