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