Skip to content

Commit f12d99d

Browse files
authored
Merge pull request #969 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 059caf2 + a6fdfe2 commit f12d99d

2 files changed

Lines changed: 62 additions & 19 deletions

File tree

Modules/CIPPCore/Public/Get-CIPPSharePointSiteUsageReport.ps1

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,56 @@ function Get-CIPPSharePointSiteUsageReport {
1818

1919
try {
2020
if ($TenantFilter -eq 'AllTenants') {
21-
$AllSiteItems = Get-CIPPDbItem -TenantFilter 'allTenants' -Type 'SharePointSiteListing'
22-
$Tenants = @($AllSiteItems | Where-Object { $_.RowKey -ne 'SharePointSiteListing-Count' } | Select-Object -ExpandProperty PartitionKey -Unique)
21+
# Bulk-fetch all site listings and usage data in 2 queries instead of per-tenant
22+
$AllSiteItems = @(Get-CIPPDbItem -TenantFilter 'allTenants' -Type 'SharePointSiteListing' | Where-Object { $_.RowKey -ne 'SharePointSiteListing-Count' })
23+
$AllUsageItems = @(Get-CIPPDbItem -TenantFilter 'allTenants' -Type 'SharePointSiteUsage' | Where-Object { $_.RowKey -ne 'SharePointSiteUsage-Count' })
2324

2425
$TenantList = Get-Tenants -IncludeErrors
25-
$Tenants = $Tenants | Where-Object { $TenantList.defaultDomainName -contains $_ }
26+
$ValidTenants = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
27+
foreach ($T in $TenantList) { [void]$ValidTenants.Add($T.defaultDomainName) }
28+
29+
# Build usage lookup keyed by siteId across all tenants
30+
$UsageBySiteId = [System.Collections.Generic.Dictionary[string, object]]::new([System.StringComparer]::OrdinalIgnoreCase)
31+
foreach ($UsageItem in $AllUsageItems) {
32+
$UsageRow = $UsageItem.Data | ConvertFrom-Json -Depth 10
33+
if (-not [string]::IsNullOrWhiteSpace($UsageRow.siteId)) {
34+
$UsageBySiteId[[string]$UsageRow.siteId] = $UsageRow
35+
}
36+
}
2637

2738
$AllResults = [System.Collections.Generic.List[PSCustomObject]]::new()
28-
foreach ($Tenant in $Tenants) {
29-
try {
30-
$TenantResults = Get-CIPPSharePointSiteUsageReport -TenantFilter $Tenant
31-
foreach ($Result in $TenantResults) {
32-
$Result | Add-Member -NotePropertyName 'Tenant' -NotePropertyValue $Tenant -Force
33-
$AllResults.Add($Result)
34-
}
35-
} catch {
36-
Write-LogMessage -API 'SharePointSiteUsageReport' -tenant $Tenant -message "Failed to get report for tenant: $($_.Exception.Message)" -sev Warning
37-
}
39+
foreach ($SiteItem in $AllSiteItems) {
40+
$Tenant = $SiteItem.PartitionKey
41+
if (-not $ValidTenants.Contains($Tenant)) { continue }
42+
43+
$Site = $SiteItem.Data | ConvertFrom-Json -Depth 10
44+
if ($Site.isPersonalSite -eq $true) { continue }
45+
46+
$SiteUsage = $null
47+
[void]$UsageBySiteId.TryGetValue([string]$Site.sharepointIds.siteId, [ref]$SiteUsage)
48+
49+
$StorageUsedInBytes = [double]($SiteUsage.storageUsedInBytes ?? 0)
50+
$StorageAllocatedInBytes = [double]($SiteUsage.storageAllocatedInBytes ?? 0)
51+
52+
$AllResults.Add([PSCustomObject]@{
53+
Tenant = $Tenant
54+
siteId = $Site.sharepointIds.siteId
55+
webId = $Site.sharepointIds.webId
56+
createdDateTime = $Site.createdDateTime
57+
displayName = $Site.displayName
58+
webUrl = $Site.webUrl
59+
ownerDisplayName = $SiteUsage.ownerDisplayName
60+
ownerPrincipalName = $SiteUsage.ownerPrincipalName
61+
lastActivityDate = $SiteUsage.lastActivityDate
62+
fileCount = $SiteUsage.fileCount
63+
storageUsedInGigabytes = [math]::round($StorageUsedInBytes / 1GB, 2)
64+
storageAllocatedInGigabytes = [math]::round($StorageAllocatedInBytes / 1GB, 2)
65+
storageUsedInBytes = $SiteUsage.storageUsedInBytes
66+
storageAllocatedInBytes = $SiteUsage.storageAllocatedInBytes
67+
rootWebTemplate = $SiteUsage.rootWebTemplate
68+
reportRefreshDate = $SiteUsage.reportRefreshDate
69+
AutoMapUrl = $Site.AutoMapUrl
70+
})
3871
}
3972
return $AllResults
4073
}

Modules/CIPPTests/Public/Tests/CopilotReadiness/Identity/Invoke-CippTestCopilotReady003.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,30 @@ function Invoke-CippTestCopilotReady003 {
4646

4747
# For each licensed user, check if they have a desktop activation in the activation report.
4848
# Users absent from the report entirely are counted as unactivated.
49+
# The Graph API returns activation counts nested inside userActivationCounts[] per product type,
50+
# so we sum windows/mac across all product types to get the total desktop activation count.
4951
$NoDesktopUsers = [System.Collections.Generic.List[object]]::new()
5052
$DesktopCount = 0
5153
foreach ($User in $LicensedUsers) {
5254
$Activation = $ActivationLookup[$User.userPrincipalName.ToLower()]
53-
if ($Activation -and (([int]($Activation.windows ?? 0) + [int]($Activation.mac ?? 0)) -gt 0)) {
55+
$TotalWindows = 0
56+
$TotalMac = 0
57+
$TotalAndroid = 0
58+
$TotalIos = 0
59+
if ($Activation.userActivationCounts) {
60+
$TotalWindows = ($Activation.userActivationCounts | Measure-Object -Property windows -Sum).Sum
61+
$TotalMac = ($Activation.userActivationCounts | Measure-Object -Property mac -Sum).Sum
62+
$TotalAndroid = ($Activation.userActivationCounts | Measure-Object -Property android -Sum).Sum
63+
$TotalIos = ($Activation.userActivationCounts | Measure-Object -Property ios -Sum).Sum
64+
}
65+
if ($Activation -and (($TotalWindows + $TotalMac) -gt 0)) {
5466
$DesktopCount++
5567
} else {
5668
$NoDesktopUsers.Add([pscustomobject]@{
5769
displayName = $User.displayName
5870
userPrincipalName = $User.userPrincipalName
59-
web = if ($Activation) { $Activation.web } else { 0 }
60-
android = if ($Activation) { $Activation.android } else { 0 }
61-
ios = if ($Activation) { $Activation.ios } else { 0 }
71+
android = if ($Activation) { $TotalAndroid } else { 0 }
72+
ios = if ($Activation) { $TotalIos } else { 0 }
6273
neverActivated = ($null -eq $Activation)
6374
})
6475
}
@@ -83,7 +94,6 @@ function Invoke-CippTestCopilotReady003 {
8394
$PlatformStr = ' (never activated)'
8495
} else {
8596
$Platforms = @()
86-
if ([int]($User.web ?? 0) -gt 0) { $Platforms += 'Web' }
8797
if ([int]($User.android ?? 0) -gt 0 -or [int]($User.ios ?? 0) -gt 0) { $Platforms += 'Mobile' }
8898
$PlatformStr = if ($Platforms) { " ($(($Platforms -join ', ')) only)" } else { ' (no activations)' }
8999
}
@@ -93,7 +103,7 @@ function Invoke-CippTestCopilotReady003 {
93103
$NeverActivated = @($NoDesktopUsers | Where-Object { $_.neverActivated }).Count
94104
$Result += "**$($NoDesktopUsers.Count) users** have no desktop M365 Apps activation"
95105
if ($NeverActivated -gt 0) { $Result += " ($NeverActivated have never activated on any platform)" }
96-
$Result += '.`n'
106+
$Result += ".`n"
97107
}
98108
}
99109

0 commit comments

Comments
 (0)