-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGet-EntraDynamicGroupType.ps1
More file actions
58 lines (50 loc) · 2.84 KB
/
Copy pathGet-EntraDynamicGroupType.ps1
File metadata and controls
58 lines (50 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function Get-EntraDynamicGroupType{
<#
Copyright/License: Free to use / modify / distribute, but leave author details intact.
Author: Jos Lieben (JSolve B.V.)
Blog: https://www.lieben.nu
Purpose: For a given entra group GUID, which high efficiency / speed, return 'AllUsers', 'AllInternalUsers', 'AllGuests' or Null depending on who's in it
optionally, use a wider drift ratio if your tenant is small and has large user delta's while using this function
#>
Param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$groupId,
[Parameter(Mandatory = $false)]
[Double]$maxDriftRatio = 0.05
)
if([string]::IsNullOrEmpty($global:totalTenantUserCount)){
[Int]$global:totalTenantUserCount = New-GraphQuery -Uri 'https://graph.microsoft.com/v1.0/users?$top=1' -Method GET -ComplexFilter -justReturnCount
}
if([string]::IsNullOrEmpty($global:totalTenantGuestCount)){
[Int]$global:totalTenantGuestCount = New-GraphQuery -Uri 'https://graph.microsoft.com/v1.0/users?$filter=userType eq ''Guest''&$top=1' -Method GET -ComplexFilter -justReturnCount
}
[Int]$totalTenantMemberCount = $global:totalTenantUserCount - $global:totalTenantGuestCount
try{
$groupMemberCount = 0; $groupMemberCount = New-GraphQuery -Method GET -Uri "https://graph.microsoft.com/v1.0/groups/$groupId/transitiveMembers/microsoft.graph.user/`$count" -ComplexFilter
}catch{
$groupMemberCount = 0
}
if($groupMemberCount -le 0){
Write-LogMessage -message "No members found in group: $groupId" -level 6
return $Null
}
# calculate drift and decide match for all internal users type
$diffRatio = if ($totalTenantMemberCount -gt 0) { [math]::Abs($groupMemberCount - $totalTenantMemberCount) / $totalTenantMemberCount } else { 1 }
if ($diffRatio -le $maxDriftRatio){
Write-LogMessage -message "Detected group with all internal users: $groupId" -level 6
return "AllInternalUsers"
}
# calculate drift and decide match for all users type
$diffRatio = if ($global:totalTenantUserCount -gt 0) { [math]::Abs($groupMemberCount - $global:totalTenantUserCount) / $global:totalTenantUserCount } else { 1 }
if ($diffRatio -le $maxDriftRatio){
Write-LogMessage -message "Detected group with all users: $groupId" -level 6
return "AllUsers"
}
# calculate drift and decide match for all users type
$diffRatio = if ($global:totalTenantGuestCount -gt 0) { [math]::Abs($groupMemberCount - $global:totalTenantGuestCount) / $global:totalTenantGuestCount } else { 1 }
if ($diffRatio -le $maxDriftRatio){
Write-LogMessage -message "Detected group with all users: $groupId" -level 6
return "AllGuests"
}
return $Null
}