From 862fd7da610f3aec2bb1c2e8048295564d204aad Mon Sep 17 00:00:00 2001 From: poslop Date: Thu, 13 Feb 2025 12:15:53 -0600 Subject: [PATCH] Comments and rearrange --- scripts/DeleteOldProfiles.ps1 | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/scripts/DeleteOldProfiles.ps1 b/scripts/DeleteOldProfiles.ps1 index d77bc10..32d519d 100644 --- a/scripts/DeleteOldProfiles.ps1 +++ b/scripts/DeleteOldProfiles.ps1 @@ -31,8 +31,27 @@ ForEach ($computer in $ComputerName) $special = $profile.Special + # Check if the account is special/system account If ($special) {continue} + # Check if the account is Excluded or not + If($accountName.ToLower() -Eq $UserName.ToLower() -Or + ($UserName.Contains("*") -And $accountName.ToLower() -Like $UserName.ToLower())) { + ForEach ($eun in $ExcludedUserNames) { + If($eun -ne [string]::Empty -And -Not $eun.Contains("*") -And ($accountName.ToLower() -eq $eun.ToLower())){ + $isExcluded = $True + break + } + If($eun -ne [string]::Empty -And $eun.Contains("*") -And ($accountName.ToLower() -Like $eun.ToLower())){ + $isExcluded = $True + break + } + } + + # Continue if excluded + If($isExcluded) {Write-Host "Profile $accountName was excluded!" continue} + + #Calculation of the login date $lastLoginDate = $null If ($accountDomain.ToUpper() -eq $computer.ToUpper()) {$lastLoginDate = [datetime]([ADSI]"WinNT://$computer/$accountName").LastLogin[0]} @@ -44,21 +63,6 @@ ForEach ($computer in $ComputerName) Else{$profileUnusedDays = (New-TimeSpan -Start $lastLoginDate -End (Get-Date)).Days} } - If($accountName.ToLower() -Eq $UserName.ToLower() -Or - ($UserName.Contains("*") -And $accountName.ToLower() -Like $UserName.ToLower())) { - - ForEach ($eun in $ExcludedUserNames) { - If($eun -ne [string]::Empty -And -Not $eun.Contains("*") -And ($accountName.ToLower() -eq $eun.ToLower())){ - $isExcluded = $True - break - } - If($eun -ne [string]::Empty -And $eun.Contains("*") -And ($accountName.ToLower() -Like $eun.ToLower())){ - $isExcluded = $True - break - } - } - - If($isExcluded) {Write-Host "Profile $accountName was excluded!" continue} If($InactiveDays -ne [uint32]::MaxValue -And $profileUnusedDays -le $InactiveDays){