Skip to content

Commit 526ca5f

Browse files
committed
addresses code review feedback
1 parent 2c47c3d commit 526ca5f

File tree

1 file changed

+91
-40
lines changed
  • docs/docs/sample-scripts/spo/list-tenant-alert-usage

1 file changed

+91
-40
lines changed

docs/docs/sample-scripts/spo/list-tenant-alert-usage/index.mdx

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,96 @@ Author: [Saurabh Tripathi](https://github.com/saurabh7019)
1515

1616
SharePoint Online list alerts are being gradually retired. This script scans all sites across the tenant and generates a comprehensive CSV report of existing alerts. This information helps administrators identify and plan the migration of critical alerts to modern alternatives.
1717

18+
- Prerequisites: This script requires app-only permissions with full control on all site collections to run.
19+
1820
<Tabs>
1921
<TabItem value="PowerShell">
2022

2123
```powershell
2224
$fileExportPath = "Alerts.csv"
2325
24-
try {
25-
$m365Status = m365 status --output text
26+
function Add-AlertsToResults {
27+
param(
28+
[array]$Alerts,
29+
[string]$SiteTitle,
30+
[string]$SiteUrl
31+
)
32+
33+
$alertResults = @()
34+
35+
foreach ($alert in $Alerts) {
36+
$targetPath = $alert.List.RootFolder.ServerRelativeUrl
37+
38+
$filterPath = ($alert.Properties | Where-Object { $_.Key -eq "filterpath" }).Value
39+
if ($filterPath) {
40+
$targetPath = $filterPath
41+
}
42+
elseif ($alert.Item) {
43+
$targetPath = $alert.Item.FileRef
44+
}
45+
46+
$frequency = switch ($alert.AlertFrequency) {
47+
0 { "Immediate" }
48+
1 { "Daily" }
49+
2 { "Weekly" }
50+
default { "Unknown" }
51+
}
52+
53+
$alertResults += [PSCustomObject][ordered]@{
54+
SiteTitle = $SiteTitle
55+
SiteUrl = $SiteUrl
56+
AlertTitle = $alert.Title
57+
AlertId = $alert.ID
58+
TargetPath = $targetPath
59+
Frequency = $frequency
60+
AlertType = $alert.AlertTemplateName
61+
UserName = $alert.User.Title
62+
UserEmail = $alert.User.Email
63+
}
64+
}
65+
66+
return $alertResults
67+
}
68+
69+
function Get-SubSiteAlerts {
70+
param(
71+
[string]$SiteUrl,
72+
[string]$SiteTitle
73+
)
74+
75+
$allResults = @()
2676
27-
if ($m365Status -eq "Logged Out") {
28-
# Connection to Microsoft 365
29-
m365 login
77+
try {
78+
$subSites = m365 spo web list --url $SiteUrl --output json | ConvertFrom-Json
79+
80+
if ($subSites -and $subSites.Count -gt 0) {
81+
foreach ($subSite in $subSites) {
82+
Write-Host "`t`tScanning subsite '$($subSite.Url)' for alerts..." -ForegroundColor Gray
83+
84+
$subSiteAlerts = m365 spo site alert list --webUrl $subSite.Url --output json | ConvertFrom-Json
85+
86+
if ($subSiteAlerts.Count -gt 0) {
87+
Write-Host "`t`t`tFound $($subSiteAlerts.Count) alert(s)" -ForegroundColor Yellow
88+
$allResults += Add-AlertsToResults -Alerts $subSiteAlerts -SiteTitle $SiteTitle -SiteUrl $subSite.Url
89+
}
90+
else {
91+
Write-Host "`t`t`tNo alerts found" -ForegroundColor Green
92+
}
93+
94+
$nestedResults = Get-SubSiteAlerts -SiteUrl $subSite.Url -SiteTitle $SiteTitle
95+
$allResults += $nestedResults
96+
}
97+
}
98+
}
99+
catch {
100+
Write-Host "`tError: $($_.Exception.Message)" -ForegroundColor Red
30101
}
102+
103+
return $allResults
104+
}
105+
106+
try {
107+
m365 login --ensure
31108
32109
$results = @()
33110
Write-Host "Retrieving all sites..."
@@ -40,50 +117,24 @@ SharePoint Online list alerts are being gradually retired. This script scans all
40117
foreach ($site in $sites) {
41118
$iCnt++
42119
Write-Host "($iCnt/$count) Scanning '$($site.Url)' for alerts..."
43-
120+
44121
try {
45122
$alerts = m365 spo site alert list --webUrl $site.Url --output json | ConvertFrom-Json
46123
47124
if ($alerts.Count -gt 0) {
48-
Write-Host " Found $($alerts.Count) alert(s)" -ForegroundColor Yellow
49-
50-
foreach ($alert in $alerts) {
51-
$targetPath = $alert.List.RootFolder.ServerRelativeUrl
52-
53-
$filterPath = ($alert.Properties | Where-Object { $_.Key -eq "filterpath" }).Value
54-
if ($filterPath) {
55-
$targetPath = $filterPath
56-
}
57-
elseif ($alert.Item) {
58-
$targetPath = $alert.Item.FileRef
59-
}
60-
61-
$frequency = switch ($alert.AlertFrequency) {
62-
0 { "Immediate" }
63-
1 { "Daily" }
64-
2 { "Weekly" }
65-
default { "Unknown" }
66-
}
67-
68-
$results += [PSCustomObject][ordered]@{
69-
SiteTitle = $site.Title
70-
SiteUrl = $site.Url
71-
AlertTitle = $alert.Title
72-
AlertId = $alert.ID
73-
TargetPath = $targetPath
74-
Frequency = $frequency
75-
AlertType = $alert.AlertTemplateName
76-
UserName = $alert.User.Title
77-
UserEmail = $alert.User.Email
78-
}
79-
}
125+
Write-Host "`tFound $($alerts.Count) alert(s)" -ForegroundColor Yellow
126+
$results += Add-AlertsToResults -Alerts $alerts -SiteTitle $site.Title -SiteUrl $site.Url
80127
}
81128
else {
82-
Write-Host " No alerts found" -ForegroundColor Green
129+
Write-Host "`tNo alerts found" -ForegroundColor Green
83130
}
131+
132+
# scan subsites
133+
$subSiteResults = Get-SubSiteAlerts -SiteUrl $site.Url -SiteTitle $site.Title
134+
$results += $subSiteResults
84135
}
85136
catch {
86-
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red
137+
Write-Host "`tError: $($_.Exception.Message)" -ForegroundColor Red
87138
}
88139
}
89140

0 commit comments

Comments
 (0)