Forum Discussion

Zenul's avatar
Zenul
Copper Contributor
Jul 23, 2024

Retrieving SharePoint Site Visitors Using PowerShell Script

I’m working on a PowerShell script to generate a report of Microsoft Teams details, including the owners, members, and SharePoint site visitors. While the script successfully retrieves most of the information, it fails to retrieve the site visitors, which I can clearly see in the SharePoint Admin Center.

Issues:

  1. Site Visitors Not Retrieved: Despite having site visitors visible in the SharePoint Admin Center, the script returns "Error retrieving Visitors" or an empty string for site visitors.

  2. Potential Errors: I'm using a try-catch block to handle exceptions, but it seems like there are no errors being caught that could indicate why the visitors aren't being retrieved.

    What I’ve Tried:

    • Ensured the SharePoint site URL is correct.
    • Checked permissions and roles for the account running the script.

      Request:

      Could someone help identify what might be going wrong or suggest a different approach to reliably retrieve the list of SharePoint site visitors or any module I am missing on? I am using below modules with Powershell Runtime version 5.1.

       

      • ExchangeOnlineManagement - Version 3.5.1
      • Get-MsolUserLicences - Version 0.0.8
      • Microsoft.Graph.Authentication - Version 2.20.0
      • Microsoft.Graph.Teams - Version 2.20.0
      • Microsoft.Graph.Users - Version 2.20.0
      • Microsoft.Online.SharePoint.PowerShell - Version 16.0.25012.12000
      • MicrosoftTeams - Version 6.4.0
      • MSOnline - Version 1.1.183.81
      • PackageManagement - Version 1.4.8.1
      • PnP.PowerShell - Version 2.5.0
      • PowerShellGet - Version 2.2.5
      • SharePointPnPPowerShellOnline - Version 3.29.2101.0

         

        Any insights or recommendations would be greatly appreciated!

        Thanks in advance for your help.

         

        Connect-MicrosoftTeams -Credential $UserCredential -AccountId $myCredential.UserName
        Connect-ExchangeOnline -Credential $UserCredential -ShowProgress $true

 

 

# Initialize an empty array to store the results
$Result = @()
# Get all Teams enabled groups
$AllTeamsInOrg = Get-Team
$Teamcount = $AllTeamsInOrg.Count
foreach ($Team in $AllTeamsInOrg) {
    Write-Host "Processing Team $($Team.DisplayName) ..." -NoNewline -ForegroundColor Gray
    $TeamName = $Team.DisplayName
    $TeamGroupId = $Team.GroupId
    # Get team details
    $TeamOwner = (Get-TeamUser -GroupId $TeamGroupId -Role Owner).User
    $TeamUserCount = (Get-TeamUser -GroupId $TeamGroupId).Count
    $TeamGuest = (Get-TeamUser -GroupId $TeamGroupId -Role Guest).Name
    if ($TeamGuest -eq $null) { $TeamGuest = "No Guests in Team" }
    $TeamMembers = (Get-TeamUser -GroupId $TeamGroupId -Role Member).User
    $TeamChannels = (Get-TeamChannel -GroupId $TeamGroupId).DisplayName
    $ChannelCount = (Get-TeamChannel -GroupId $TeamGroupId).Count
     # Retrieve SharePoint site URL from the team
    $TeamGroup = Get-UnifiedGroup -Identity $TeamGroupId
    $SharePointSiteURL = $TeamGroup.SharePointSiteURL
    if ($SharePointSiteURL -ne $null) {
        try {
            # Get all SharePoint Online groups for the site
            $SiteGroups = Get-SPOSiteGroup -Site $SharePointSiteURL
            # Find the "Visitors" group and get its members
            $VisitorsGroup = $SiteGroups | Where-Object { $_.Title -eq "Visitors" }
            if ($VisitorsGroup) {
                $Visitors = Get-SPOSiteGroup -Site $SharePointSiteURL -Group $VisitorsGroup.Title | Select-Object -ExpandProperty Users
                $VisitorsEmails = $Visitors | Select-Object -ExpandProperty Email -join ", "
                $GroupsData += [pscustomobject]@{
                    'Site URL'   = $SharePointSiteURL
                    'Group Name' = $VisitorsGroup.Title
                    'Users'      = $VisitorsEmails
                }
            } else {
                $GroupsData += [pscustomobject]@{
                    'Site URL'   = $SharePointSiteURL
                    'Group Name' = "Visitors"
                    'Users'      = "No Visitors Group Found"
                }
            }
        } catch {
            Write-Host "Error processing site ${SharePointSiteURL}: $($_.Exception.Message)" -ForegroundColor Red
            $GroupsData += [pscustomobject]@{
                'Site URL'   = $SharePointSiteURL
                'Group Name' = "Visitors"
                'Users'      = "Access Denied or Error"
            }
        }
    } else {
        $GroupsData += [pscustomobject]@{
            'Site URL'   = $SharePointSiteURL
            'Group Name' = "Visitors"
            'Users'      = "No SharePoint Site URL"
        }
    }
    # Add team details to the $Result array
    $Result += [PSCustomObject]@{
        TeamName = $TeamName
        TeamOwners = $TeamOwner -join ', '
        TeamMemberCount = $TeamUserCount
        TeamMembers = $TeamMembers -join ', '
        NoOfChannels = $ChannelCount
        ChannelNames = $TeamChannels -join ', '
        SharePointSite = $SharePointSiteURL
        AccessType = $TeamGroup.AccessType
        TeamGuests = $TeamGuest -join ','
        SharePointSiteVisitors = $SharePointSiteVisitors
    }
    Write-Host "done." -ForegroundColor Green
}

 

 

  •  

No RepliesBe the first to reply

Resources