$starttime = Get-Date

echo "Let's get started, shall we? It's $starttime"

#Dsquery does the work here- just checking to count how many disabled accounts exist. We'll use this later to calculate how many were moved.

$initialdisabled = dsquery computer - disabled

foreach ( $item in $initialdisabled ) { $initialcount ++ }

echo "$initialcount initial count"

#Dsquery will produce all computer accounts not active for 8 weeks.

$inactive = dsquery computer - inactive 8

foreach ( $item in $inactive ) { $inactivecount ++ }

#Send the DSQuery output to DSMove to park these in the correct OU)

foreach ( $item in $inactive ) { dsmod computer $item - desc "Disabled by script $starttime" - disabled yes }

foreach ( $item in $inactive ) { dsmove $item - newparent "OU=Disabled Computer Accounts,OU=Computers,OU=Bar,DC=Foo,DC=com" }

$endtime = Get-Date

$duration = $endtime - $starttime

$changed = $inactivecount - $initialcount

echo "Today's job took: $duration"

echo "Today's job disabled and moved $changed computers."

echo "End Time: $endtime"

#now we'll drop this into an email by calling the Net.mail.smtpclient object.

$SMTPserver = "192.168.ur.mailserver"

$from = "AD@foo.com"

$to = "you@foo.com"

$subject = "Computer Accounts Changed"

$body = "Hello, this a script generated email. I've just disabled computer accounts for inactivity exceeding 8 weeks. Today's job took: $duration . Today's job disabled and moved $changed computers. End Time: $endtime"

$mailer = new-object Net.Mail.SMTPclient ( $SMTPserver )

$msg = new-object Net.Mail.MailMessage ( $from , $to , $subject , $body )

$mailer .send ( $msg )

Clear-Variable inactive

Clear-Variable initialcount

Clear-Variable inactivecount

Clear-Variable duration

Clear-Variable endtime

Clear-Variable starttime