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){