Skip to content

Commit 8aa41da

Browse files
committed
moved Managed Identities exclusion to SQL query to improve performance for large tenants
1 parent e31e32a commit 8aa41da

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/powershell/private/tests-shared/Get-ApplicationsWithPermissions.ps1

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ function Get-ApplicationsWithPermissions {
1919
[string[]]$ExcludeServicePrincipalType
2020
)
2121

22+
# Build optional SQL exclusion clause for service principal types
23+
$excludeClause = ''
24+
if ($ExcludeServicePrincipalType) {
25+
$quoted = "'" + ($ExcludeServicePrincipalType -join "', '") + "'"
26+
$excludeClause = "`n and sp.servicePrincipalType not in ($quoted)"
27+
}
28+
2229
# Query ServicePrincipal objects with permissions
2330
# Used by tests 21770, 24518, and 21867
2431
$sql = @"
@@ -27,7 +34,7 @@ spsi.lastSignInActivity.lastSignInDateTime,
2734
sp.owners, sp.signInAudience, sp.servicePrincipalType
2835
from main.ServicePrincipal sp
2936
left join main.ServicePrincipalSignIn spsi on spsi.appId = sp.appId
30-
where sp.id in
37+
where (sp.id in
3138
(
3239
select sp.id
3340
from main.ServicePrincipal sp
@@ -43,7 +50,7 @@ where sp.id in
4350
from main.ServicePrincipal) spAppRole
4451
on sp.appRoleId = spAppRole.id
4552
where permissionName is not null
46-
)
53+
))$excludeClause
4754
order by spsi.lastSignInActivity.lastSignInDateTime
4855
"@
4956

@@ -55,12 +62,6 @@ order by spsi.lastSignInActivity.lastSignInDateTime
5562
return @()
5663
}
5764

58-
# Exclude specified service principal types before enrichment to avoid unnecessary per-item DB calls
59-
if ($ExcludeServicePrincipalType) {
60-
$results = @($results | Where-Object { $ExcludeServicePrincipalType -notcontains $_.servicePrincipalType })
61-
Write-PSFMessage "Excluded $($ExcludeServicePrincipalType -join ', ') type(s), $($results.Count) service principals remaining" -Level Verbose
62-
}
63-
6465
# Enrich each app with permissions and risk classification (using Test-21770 pattern)
6566
$enrichedApps = @()
6667

0 commit comments

Comments
 (0)