Skip to content

Commit

Permalink
Merge pull request #675 from ganesh-sanap/spo-check-unaccessable-home…
Browse files Browse the repository at this point in the history
…pages-cli

Sample Update - Scan for potential inaccessible site collection homepages
  • Loading branch information
pkbullock authored Mar 14, 2024
2 parents 1bca638 + ceb66b4 commit a8b0821
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 21 deletions.
79 changes: 62 additions & 17 deletions scripts/spo-check-unaccessable-homepages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ plugin: add-to-gallery

## Summary

Sample looks for site collections where the default page has custom permissions and outputs to a CSV file
This script sample looks for the site collections where site home page has custom permissions and outputs to a CSV file.

![Example Screenshot](assets/example.png)


# [PnP PowerShell](#tab/pnpps)

```powershell
$SPOAdminUrl = "The URL of your tenant SharePoint admin center"
if(-not $adminConn)
{
$adminConn = Connect-PnPOnline -Url $SPOAdminUrl -Interactive -ReturnConnection
Expand All @@ -28,19 +25,19 @@ $allRelevantSites.Count
#output to an arraylist
$output = New-Object System.Collections.ArrayList
foreach($site in $allRelevantSites)
{
try
try
{
$localconn = Connect-PnPOnline -Url $site.Url -Interactive -WarningAction SilentlyContinue -ErrorAction Stop -ReturnConnection
Write-Host "Processing $($site.Url)" -ForegroundColor Green
$sitepagesList = Get-PnPList -Identity "SitePages" -Connection $localconn
$defaultpageUrl = Get-PnPHomePage -Connection $localconn
$defaultpageUrl = $defaultpageUrl.Substring($defaultpageUrl.IndexOf("/")+1)
$defaultpageItem = Get-PnPListItem -List $sitepagesList -Connection $localconn | Where-Object { $_.FieldValues.FileLeafRef -eq $defaultpageUrl }
$defaultpageItem = Get-PnPListItem -List $sitepagesList -Connection $localconn | Where-Object { $_.FieldValues.FileLeafRef -eq $defaultpageUrl }
#check if the page has custom permissions as this can cause issues
#check if the page has custom permissions as this can cause issues
$permissions = Get-PnPListItemPermission -List $sitepagesList -Identity $defaultpageItem -Connection $localconn
$output.Add([PSCustomObject]@{
Expand All @@ -49,37 +46,85 @@ foreach($site in $allRelevantSites)
PagePermissions = $defaultpageItem.HasUniqueRoleAssignments
ErrorMsg = ""
}) | Out-Null
}
catch
catch
{
$output.Add([PSCustomObject]@{
SiteUrl = $site.Url
DefaultPage = $defaultpage
PagePermissions = $defaultpageItem.HasUniqueRoleAssignments
ErrorMsg = $_.Exception.Message
}) | Out-Null
}
}
$output | Export-Csv -Path "C:\temp\DefaultHomePageAccessablityRisk.csv" -NoTypeInformation -Force -Encoding utf8BOM -Delimiter "|"
# Disconnect SharePoint online connection
Disconnect-PnPOnline
```

[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
***

# [CLI for Microsoft 365](#tab/cli-m365-ps)

```powershell
#Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
$allRelevantSites = m365 spo site list | ConvertFrom-Json | Where-Object { $_.Template -like "STS#3" } #or similar filter as per your requirement
$allRelevantSites.Count
#output to an arraylist
$output = New-Object System.Collections.ArrayList
foreach($site in $allRelevantSites)
{
try
{
Write-Host "Processing $($site.Url)" -ForegroundColor Green
$webDetails = m365 spo web get --url $site.Url | ConvertFrom-Json
$defaultpageUrl = $webDetails.WelcomePage
$defaultpageUrl = $defaultpageUrl.Substring($defaultpageUrl.IndexOf("/")+1)
$defaultpageItem = m365 spo listitem list --listTitle "Site Pages" --webUrl $site.Url --fields "ID,HasUniqueRoleAssignments,FileLeafRef" --filter "FileLeafRef eq '$($defaultpageUrl)'" | ConvertFrom-Json
$output.Add([PSCustomObject]@{
SiteUrl = $site.Url
DefaultPage = $defaultpageUrl
PagePermissions = $defaultpageItem.HasUniqueRoleAssignments
ErrorMsg = ""
}) | Out-Null
}
catch
{
$output.Add([PSCustomObject]@{
SiteUrl = $site.Url
DefaultPage = $defaultpage
PagePermissions = $defaultpageItem.HasUniqueRoleAssignments
ErrorMsg = $_.Exception.Message
}) | Out-Null
}
}
$output | Export-Csv -Path "D:\dtemp\DefaultHomePageAccessablityRisk-CLI.csv" -NoTypeInformation -Force -Encoding utf8BOM -Delimiter "|"
#Disconnect SharePoint online connection
m365 logout
```

[!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)]

***

## Contributors

| Author(s) |
|-----------|
| Kasper Larsen |
| [Ganesh Sanap](https://ganeshsanapblogs.wordpress.com/) |

[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-check-unaccessable-homepages" aria-hidden="true" />
34 changes: 30 additions & 4 deletions scripts/spo-check-unaccessable-homepages/assets/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,44 @@
"name": "spo-check-unaccessable-homepages",
"source": "pnp",
"title": "Scan for potential inaccessible site collection homepages",
"shortDescription": "looks for site collections where the default page has custom permissions and outputs to a CSV file",
"shortDescription": "Looks for site collections where the home page has custom permissions and outputs to a CSV file.",
"url": "https://pnp.github.io/script-samples/spo-check-unaccessable-homepages/README.html",
"longDescription": [
""
],
"creationDateTime": "2024-03-04",
"updateDateTime": "2024-03-04",
"updateDateTime": "2024-03-10",
"products": [
"SharePoint"

],
"metadata": [
{
"key": "PNP-POWERSHELL",
"value": "2.4.0"
},
{
"key": "CLI-FOR-MICROSOFT365",
"value": "7.5.0"
}
],
"categories": [
"Report",
"Security"
],
"tags": [
"Get-PnPHomePage,Get-PnPListItem,Get-PnPListItemPermission"
"Connect-PnPOnline",
"Get-PnPTenantSite",
"Get-PnPList",
"Get-PnPHomePage",
"Get-PnPListItem",
"Get-PnPListItemPermission",
"Disconnect-PnPOnline",
"m365 login",
"m365 status",
"m365 spo site list",
"m365 spo web get",
"m365 spo listitem list",
"m365 logout"
],
"thumbnails": [
{
Expand All @@ -36,6 +51,12 @@
}
],
"authors": [
{
"gitHubAccount": "ganesh-sanap",
"company": "",
"pictureUrl": "https://avatars.githubusercontent.com/u/25476310?v=4",
"name": "Ganesh Sanap"
},
{
"gitHubAccount": "kasperbolarsen",
"company": "",
Expand All @@ -48,6 +69,11 @@
"name": "Want to learn more about PnP PowerShell and the cmdlets",
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
"url": "https://aka.ms/pnp/powershell"
},
{
"name": "Want to learn more about CLI for Microsoft 365 and the commands",
"description": "Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.",
"url": "https://aka.ms/cli-m365"
}
]
}
Expand Down

0 comments on commit a8b0821

Please sign in to comment.