As an Administrator, You should review the systems which has been created and deleted on daily basis in you Active directory and it’s difficult to generate the report and monitor the same. In this Blog I will show you to create and schedule the task for PowerShell script and this will generate all created and deleted computers report and send the email in html format.

#>

#Setting Date of One Week Back
$week = (Get-Date).AddDays(-1)
$today = (Get-Date).ToString()

# Html
$a = “<style>”
$a = $a + “BODY{background-color:Lavender ;}”
$a = $a + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}”
$a = $a + “TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:thistle}”
$a = $a + “TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:PaleGoldenrod}”
$a = $a + “</style>”

# Email Variables
$To = “amitceh@hotmail.com”
$from =”amitceh@gmail.com”
$subject = “Domain -AD Computers Created & Deleted last day ($today).”

# Import Module of Active Directory
Import-Module -Name ActiveDirectory

# CreatedComputers
$CreatedComputers = Get-ADcomputer -SearchBase “DC=infoalias,DC=local” -Filter * -Properties * | `
where { $_.whenCreated -ge $week } | sort |select Name,whenCreated,OperatingSystem,OperatingSystemversion, DistinguishedName `
| ConvertTo-html -Head $a -Body “<H2>Computers Created last day..</H2>”
# DeletedComputers
$DeletedComputers = Get-ADObject -IncludeDeletedObjects -filter {objectclass -eq “computer” -and objectclass -eq “computer” -and deleted -eq $true}-property whenChanged | `
Where-Object { $_.whenChanged -ge $week } |Select Name,DistinguishedName,whenChanged `
| ConvertTo-html -Head $a -Body “<H2>Computers deleted last day..</H2>”

$body = “Created & Deleted period from $week to $today .”
$body += “`n”
$body += $CreatedComputers
$body += “`n”
$body += $DeletedComputers
$body += “`n”

$smtpserver = ‘smtp.gmail.com’

$smtpUsername = ‘amitceh@gmail.com’
$smtpUsername = “amitceh@gmail.com”
$smtpPassword = ‘tdngaacahsdyeudmlm’
$credentials = new-object Management.Automation.PSCredential $smtpUsername, ($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)

Send-MailMessage -SmtpServer $smtpserver -Port 587 -UseSsl -Credential $credentials -To $to -From $from -Subject $subject -Body $body -BodyAsHtml

Post Executing the details you will get report on your email

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *