Hello All,
I searched the forum for answers but couldn’t find anything that quite explains the problem I’m facing.
I am trying to
- grab users that haven’t logged in after 55 days,
- disabled them, and
- move them to the disabled OU.
Getting the list of users part works fine, but the if-else statement doesn’t work; the output only shows the else output as if it doesn’t find any users. I am trying to turn this into a Scheduled Tasks; that’s the reason why I don’t just want to use get-ADuser by itself.
I included the output at the bottom of the script. Please help.
Import-Module ActiveDirectory
$time = (Get-Date).Adddays(-55)
$OUUser = "user OU"
$OUSupport = "support user OU"
$OUUserMove = "disable user OU"
$OUSupportMove = "disabled support user OU"
$OU1 = "service account OU"
$DirPath = "C:\bin\ADAM"
$LogFile = $DirPath + "\" + "Disable_and_Move_User_Accounts.log"
$userlist=Get-ADUser -Filter {(LastLogonDate -lt $time) -and (Enabled -eq "True")} -Properties LastLogonDate | Where-Object {$_.distinguishedname -notlike $OU1}
#Get-ADUser -Filter {(LastLogonDate -lt $time) -and (Enabled -eq "True")} -Properties LastLogonDate | Where-Object {$_.distinguishedname -notlike $OU1}
Start-Transcript -path $LogFile
ForEach ($users in $userlist){
If ($users.distinguishedName -like "$OUUser"){
$desc="Disabled on $(Get-Date) for being inactive - $($users.Description)"
Set-ADUser $users -Description $desc -Enabled $false
Move-ADObject $users -TargetPath $OUUserMove
}
If ($users.distinguishedName -like "$OUSupport"){
$desc="Disabled on $(Get-Date) for being inactive - $($users.Description)"
Set-ADUser $users -Description $desc -Enabled $false
Move-ADObject $users -TargetPath $OUSupportMove
}
else {
echo " "
echo " "
echo "All users are active"
echo " "
echo " "
}
}
Stop-Transcript
below are the results I am getting:
Transcript started, output file is C:\bin\ADAM\Disable_and_Move_User_Accounts.log
All users are active
All users are active
All users are active
All users are active
Transcript stopped, output file is C:\bin\ADAM\Disable_and_Move_User_Accounts.log