Passwords set to never expire can be a security issues for your network. As per Active directory password policy it should be changed on regulars basis. However, if you didn’t intentionally set passwords to never expire, you may want check your Active Directory for User property which has been set with password never expire. This Report can be generated manually or you may configure a schedule task for the PowerShell script and it will execute it accordingly and send it on configured email address in PowerShell.

Powershell Script

#>

$then= (get-date).AddDays(-100)
$month = (Get-Date).AddDays(-100)
$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”
$smtpserver = ‘smtp.gmail.com’
$smtpUsername = ‘amitceh@gmail.com’
$smtpUsername = “amitceh@gmail.com”
$smtpPassword = ‘tdngaacahsdyeudm’
$credentials = new-object Management.Automation.PSCredential $smtpUsername, ($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)
$subject = “Password Never expired Objects in infoalias.local ($today).”

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

# User reports
$Users = Get-ADUser -Filter {(enabled -eq $true -and PasswordNeverExpires -eq $True )} -Properties LastLogonTimeStamp, whencreated, whenchanged,passwordlastset, PasswordNeverExpires, fax, mail | ? { (($_.distinguishedname -notlike’*Disabled*’)  -and ($_.distinguishedname -notlike’*Domain Admins*’))} | `
Select-Object -Property Name, SamAccountName, Enabled, DistinguishedName, whenCreated,whenchanged,passwordlastset,PasswordNeverExpires,fax,mail,@{Name=”LastLogonTimeStamp”;Expression={[datetime]::FromFileTime($_.LastLogonTimeStamp)}} `
| ConvertTo-html -Head $a -Body “<H2>Password Never expired Objects in infoalias.local ..</H2>”

$body = “Password Never expired Objects in infoalias.local from $month to $today .”
$body += “`n”
$body += $Users
$body += “`n”

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

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 *