Skip to content

Commit a155351

Browse files
Merge pull request KelvinTegelaar#1491 from ngms-psh/feat-OnedriveUsage-alert
Feat: Alert on % OneDrive quota used
2 parents 2414906 + 4271168 commit a155351

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function Get-CIPPAlertOneDriveQuota {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
#>
6+
[CmdletBinding()]
7+
Param (
8+
[Parameter(Mandatory)]
9+
$TenantFilter,
10+
[Alias('input')]
11+
[ValidateRange(0,100)]
12+
[int]$InputValue = 90
13+
)
14+
15+
try {
16+
$Usage = New-GraphGetRequest -tenantid $TenantFilter -uri "https://graph.microsoft.com/beta/reports/getOneDriveUsageAccountDetail(period='D7')?`$format=application/json&`$top=999" -AsApp $true
17+
if (!$Usage) {
18+
Write-AlertMessage -tenant $($TenantFilter) -message "OneDrive quota Alert: Unable to get OneDrive usage: Error occurred: No data returned from API."
19+
return
20+
}
21+
}
22+
catch {
23+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
24+
Write-AlertMessage -tenant $($TenantFilter) -message "OneDrive quota Alert: Unable to get OneDrive usage: Error occurred: $ErrorMessage"
25+
return
26+
}
27+
28+
#Check if the OneDrive quota is over the threshold
29+
$OverQuota = $Usage | ForEach-Object {
30+
if ($_.StorageUsedInBytes -eq 0 -or $_.storageAllocatedInBytes -eq 0) { return }
31+
try {
32+
$UsagePercent = [math]::Round(($_.storageUsedInBytes / $_.storageAllocatedInBytes) * 100)
33+
} catch { $UsagePercent = 100 }
34+
35+
if ($UsagePercent -gt $InputValue) {
36+
$GBLeft = [math]::Round(($_.storageAllocatedInBytes - $_.storageUsedInBytes) / 1GB)
37+
"$($_.ownerPrincipalName): OneDrive is $UsagePercent% full. OneDrive has $($GBLeft)GB storage left"
38+
}
39+
40+
}
41+
42+
#If the quota is over the threshold, send an alert
43+
if ($OverQuota) {
44+
Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $OverQuota
45+
}
46+
}

0 commit comments

Comments
 (0)