Skip to content

Commit a9ad195

Browse files
committed
make guest check less expensive
1 parent 9ced3b8 commit a9ad195

1 file changed

Lines changed: 4 additions & 14 deletions

File tree

Modules/CIPPStandards/Public/Standards/Invoke-CIPPStandardDisableGuests.ps1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,15 @@ function Invoke-CIPPStandardDisableGuests {
5555
try {
5656
$GraphRequest = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$filter=createdDateTime le $Lookup and userType eq 'Guest' and accountEnabled eq true &`$select=id,UserPrincipalName,signInActivity,mail,userType,accountEnabled,createdDateTime,externalUserState" -scope 'https://graph.microsoft.com/.default' -tenantid $Tenant
5757

58-
$EnrichedGuests = [System.Collections.Generic.List[object]]::new()
59-
foreach ($guest in $GraphRequest) {
60-
$lastSignIn = $null
58+
$EnrichedGuests = foreach ($guest in $GraphRequest) {
6159
if ($guest.signInActivity -and $guest.signInActivity.lastSuccessfulSignInDateTime) {
6260
$lastSignIn = [datetime]$guest.signInActivity.lastSuccessfulSignInDateTime
63-
} else {
64-
# signInActivity is null, try auditLogs/signIns
65-
$SignInLogs = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/auditLogs/signIns?`$filter=userId eq '$($guest.id)' and status/errorCode eq 0&`$orderby=createdDateTime desc&`$top=1" -scope 'https://graph.microsoft.com/.default' -tenantid $Tenant -noPagination $true
66-
if ($SignInLogs -and $SignInLogs.Count -gt 0) {
67-
$lastSignIn = [datetime]$SignInLogs[0].authenticationDetails.authenticationStepDateTime
61+
if ($lastSignIn.ToUniversalTime() -le $Days) {
62+
$guest
6863
}
6964
}
70-
# Only add guests whose last sign-in is older than cutoff
71-
if ($lastSignIn -and $lastSignIn.ToUniversalTime() -le $Days) {
72-
$guest | Add-Member -MemberType NoteProperty -Name 'EnrichedLastSignInDateTime' -Value $lastSignIn -Force
73-
$EnrichedGuests.Add($guest)
74-
}
7565
}
76-
$GraphRequest = $EnrichedGuests
66+
$GraphRequest = @($EnrichedGuests)
7767
} catch {
7868
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
7969
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the DisableGuests state for $Tenant. Error: $ErrorMessage" -Sev Error

0 commit comments

Comments
 (0)