Find unactive AD computer objects that are still enabled

Wants to know which computer in your AD is no longer used, but are still enabled in the AD. Schedule this script to run on a monthly basis and get an email with all computers not used in the last 365 days.

Import-Module ActiveDirectory

$date = get-date

$systems = Get-ADComputer –filter * -prop Lastlogondate,passwordLastSet,whencreated,enabled,OperatingSystem,description |
Where { $_.passwordLastSet –eq $null –or $_.passwordLastSet –lt (Get-Date).AddDays(-365) -and $_.lastlogondate –lt (Get-Date).AddDays(-365) -and $_.enabled -eq “TRUE” }|
select Name,
LastLogonDate,
PasswordLastSet,
whencreated,
OperatingSystem,
description,
DistinguishedName |
export-Csv C:\temp\InactiveComputers.csv -notypeinformation -Delimiter “;”

function sendMail{

Write-Host “Sending Email”

#SMTP server name
$smtpServer = “mail.local”

#Creating a Mail object
$msg = new-object Net.Mail.MailMessage

#Attach output file
$file = “C:\temp\InactiveComputers.csv”
$att = new-object Net.Mail.Attachment($file)

#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#Email structure
$msg.From = “srv-0001@mail.local”
$msg.ReplyTo = “user@mail.local”
$msg.To.Add(“recepient@mail.local”)
$msg.subject = ” Inactive computer accounts to be removed”
$msg.body = “This is the email Body.”
$msg.Attachments.Add($att)

#Sending email
$smtp.Send($msg)

}
#Send mail
sendMail

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s