Skip to content

Commit 9502e20

Browse files
authored
Merge pull request KelvinTegelaar#1357 from Ren-Roros-Digital/DisableGuests
fix: please stop disabling my reactivated guests
2 parents 1b33a14 + 0aeb167 commit 9502e20

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Diff for: Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardDisableGuests.ps1

+11-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ function Invoke-CIPPStandardDisableGuests {
3333
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'DisableGuests'
3434

3535
$Lookup = (Get-Date).AddDays(-90).ToUniversalTime().ToString('o')
36-
$GraphRequest = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$filter=(signInActivity/lastSuccessfulSignInDateTime le $Lookup)&`$select=id,UserPrincipalName,signInActivity,mail,userType,accountEnabled" -scope 'https://graph.microsoft.com/.default' -tenantid $Tenant | Where-Object { $_.userType -EQ 'Guest' -and $_.AccountEnabled -EQ $true }
36+
$AuditLookup = (Get-Date).AddDays(-7).ToUniversalTime().ToString('o')
37+
38+
$GraphRequest = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$filter=(signInActivity/lastSuccessfulSignInDateTime le $Lookup)&`$select=id,UserPrincipalName,signInActivity,mail,userType,accountEnabled" -scope 'https://graph.microsoft.com/.default' -tenantid $Tenant |
39+
Where-Object { $_.userType -eq 'Guest' -and $_.AccountEnabled -eq $true }
40+
41+
$RecentlyReactivatedUsers = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/auditLogs/directoryAudits?`$filter=activityDisplayName eq 'Enable account' and activityDateTime ge $AuditLookup" -scope 'https://graph.microsoft.com/.default' -tenantid $Tenant |
42+
ForEach-Object { $_.targetResources[0].id } | Select-Object -Unique)
43+
44+
$GraphRequest = $GraphRequest | Where-Object { -not ($RecentlyReactivatedUsers -contains $_.id) }
3745

3846
If ($Settings.remediate -eq $true) {
39-
if ($GraphRequest) {
47+
if ($GraphRequest.Count -gt 0) {
4048
foreach ($guest in $GraphRequest) {
4149
try {
4250
New-GraphPostRequest -type Patch -tenantid $tenant -uri "https://graph.microsoft.com/beta/users/$($guest.id)" -body '{"accountEnabled":"false"}'
@@ -53,7 +61,7 @@ function Invoke-CIPPStandardDisableGuests {
5361
}
5462
if ($Settings.alert -eq $true) {
5563

56-
if ($GraphRequest) {
64+
if ($GraphRequest.Count -gt 0) {
5765
Write-StandardsAlert -message "Guests accounts with a login longer than 90 days ago: $($GraphRequest.count)" -object $GraphRequest -tenant $tenant -standardName 'DisableGuests' -standardId $Settings.standardId
5866
Write-LogMessage -API 'Standards' -tenant $tenant -message "Guests accounts with a login longer than 90 days ago: $($GraphRequest.count)" -sev Info
5967
} else {

0 commit comments

Comments
 (0)