Batch script (powershell?) to audit user/pc accts in domain, disable/move them

Greetings experts.  I need to run a script once a month that will accomplish the following tasks:
  • Query the last time users in our domain logged in
  • If the user hasn't logged in more than 7 weeks, disable the accounts
  • Move the disabled accounts to the Inactive OU
Eager to see what expert can accomplish this!

**Update**

Domain is structured:

OU=Depts
->OU=ABC
-->OU=Users

ou=depts,ou=nca,dc=ds,dc=com

Here is what I have been using (batch script) but for some reason, when I run it now, it throws an error [dsquery failed:directory object not found]
@echo off
setlocal

set users_log=c:\tools\logs\%date:~4,2%-%date:~7,2%-%date:~10%_dsquery.log
set user_cleanup_log=c:\tools\logs\%date:~4,2%-%date:~7,2%-%date:~10%_cleanup.log

dsquery user -o samid -limit 0 -inactive 8 ou=depts,ou=nca,dc=ds,dc=com >%users_log%

echo "Finished dsquery" >>%user_cleanup_log%

:://

FOR /f "delims= " %%i in (%users_log%) do ( 
	echo disabling %%i && echo. && dsquery user -samid %%i | dsmod user -disabled Yes && echo.
)  >>%user_cleanup_log%

echo "Finished dsmod" >>%user_cleanup_log%

:://

FOR /f "delims= " %%i in (%users_log%) do ( 
	echo Moving %%i && echo. && dsquery user -samid %%i | dsmove -newparent 

"ou=depts,ou=nca,dc=ds,dc=com" && echo.
)  >>%user_cleanup_log%


goto:eof

Open in new window

LVL 9
samiam41Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
You can test run this, update the $disabledOU value with the target OU for disabled accounts.

If the results are good (I haven't had a chance to test run it) then remove -WhatIf from the end of the lines that have them.

Import-Module ActiveDirectory

$disabledOU = "OU=InactiveUsers,DC=my,DC=domain,DC=com"

$users = Get-ADUser -Filter * -Properties SAMAccountName, lastLogonTimeStamp | ?{[DateTime]::FromFileTime($_.lastLogonTimeStamp) -lt (Get-Date).AddDays(-49)}
foreach ($user in $users)
{
    Disable-ADAccount -Identity $user.SAMAccountName -WhatIf
    Move-ADObject -Identity $user.SAMAccountName -TargetPath $disabledOU -WhatIf
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samiam41Author Commented:
D@mn!  That's slick.  It worked as advertised.  I left the -whatif in the script but it still moved the user accounts, which is fine, but I thought I would let you know.

One thing I noticed, there's no way to log show when this was ran or what users were moved.  I'll post a follow up question to add that variable to the script.
0
samiam41Author Commented:
Amazing, clean and to the point!  Thanks for the hard work.
0
samiam41Author Commented:
0
samiam41Author Commented:
New question about this script.  Just audited 10k+ user accounts.  :)

https://www.experts-exchange.com/questions/29072606/Narrow-down-OU's-being-audited-in-PS-script.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.