{"id":538,"date":"2014-03-18T17:47:07","date_gmt":"2014-03-18T15:47:07","guid":{"rendered":"http:\/\/dety.net.ua\/?p=538"},"modified":"2014-03-18T17:47:07","modified_gmt":"2014-03-18T15:47:07","slug":"passwords-expired-alert","status":"publish","type":"post","link":"https:\/\/dety.net.ua\/?p=538","title":{"rendered":"&#8220;Password&#8217;s expired&#8221; alert"},"content":{"rendered":"<blockquote><p>Import-Module ActiveDirectory<\/p>\n<p>#System globalization<br \/>\n#$ci = New-Object System.Globalization.CultureInfo(&#8220;ru-RU&#8221;)<\/p>\n<p>#SMTP server name<br \/>\n$smtpServer = &#8220;mail.domain.com&#8221;<\/p>\n<p>#Creating a Mail object<br \/>\n$msg = new-object Net.Mail.MailMessage<br \/>\n#Creating a Mail object for report<br \/>\n$msgr = new-object Net.Mail.MailMessage<\/p>\n<p>#Creating SMTP server object<br \/>\n$smtp = new-object Net.Mail.SmtpClient($smtpServer)<\/p>\n<p>#E-mail structure<br \/>\nFunction EmailStructure($to,$expiryDate,$upn)<br \/>\n{<br \/>\n$msg.IsBodyHtml = $true<br \/>\n$msg.From = &#8220;admin@domain.com&#8221;<br \/>\n$msg.To.Clear()<br \/>\n$msg.To.Add($to)<br \/>\n$msg.Subject = &#8220;Password expiration notice&#8221;<br \/>\n$msg.Body = &#8220;&lt;html&gt;&lt;body&gt;&lt;font face=&#8217;Arial&#8217;&gt;This is an automatically generated message from Exchange service.&lt;br&gt;&lt;br&gt;&lt;b&gt;Please note that the password for your account &lt;i&gt;&lt;u&gt;Domain\\$upn&lt;\/u&gt;&lt;\/i&gt; will expire on $expiryDate.&lt;\/b&gt;&lt;br&gt;&lt;br&gt;Please change your password immediately or at least before this date as you will be unable to access the service without contacting your administrator.&lt;br&gt;&lt;\/font&gt;&lt;\/body&gt;&lt;\/html&gt;&#8221;<\/p>\n<p>}<\/p>\n<p>Function EmailStructureReport($to)<br \/>\n{<br \/>\n$msgr.IsBodyHtml = $true<br \/>\n$msgr.From = &#8220;admin@domain.com&#8221;<br \/>\n$msgr.To.Add($to)<br \/>\n$msgr.Subject = &#8220;Script running report&#8221;<br \/>\n$msgr.Body = &#8220;&lt;html&gt;&lt;body&gt;&lt;font face=&#8217;Arial&#8217;&gt;&lt;b&gt;This is a daily report.&lt;br&gt;&lt;br&gt;Script has successfully completed its work.&lt;br&gt;$NotificationCounter users have recieved notifications:&lt;br&gt;&lt;br&gt;$ListOfAccounts&lt;br&gt;&lt;br&gt;&lt;br&gt;Users with expired password:&lt;br&gt;$ListOfExpAccounts&lt;\/br&gt;&lt;\/b&gt;&lt;\/font&gt;&lt;\/body&gt;&lt;\/html&gt;&#8221;<\/p>\n<p>}<\/p>\n<p>#Set the target OU that will be searched for user accounts<br \/>\n$OU = &#8220;DC=ou,DC=domain,DC=com&#8221;<br \/>\nwrite-host $OU<br \/>\n$ADAccounts = Get-ADUser -LDAPFilter &#8220;(objectClass=user)&#8221; -searchbase $OU -properties PasswordExpired, extensionAttribute15, PasswordNeverExpires, PasswordLastSet, Mail, Enabled | Where-object {$_.Enabled -eq $true -and $_.PasswordNeverExpires -eq $false}<\/p>\n<p>$NotificationCounter = 0<br \/>\n$ListOfAccounts = &#8220;&#8221;<br \/>\n$ListOfExpAccounts = &#8220;&#8221;<\/p>\n<p>Foreach ($ADAccount in $ADAccounts)<br \/>\n{<br \/>\n$accountFGPP = Get-ADUserResultantPasswordPolicy $ADAccount<br \/>\nif ($accountFGPP -ne $null)<br \/>\n{<br \/>\n$maxPasswordAgeTimeSpan = $accountFGPP.MaxPasswordAge<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge<br \/>\n}<\/p>\n<p>#Fill in the user variables<br \/>\n$samAccountName = $ADAccount.samAccountName<br \/>\n$userEmailAddress = $ADAccount.mail<br \/>\n$userPrincipalName = $ADAccount.UserPrincipalName<\/p>\n<p>if ($ADAccount.PasswordExpired)<br \/>\n{<br \/>\nWrite-host &#8220;The password for account $samAccountName has expired!&#8221;<br \/>\n$ListOfExpAccounts = $ListOfExpAccounts + $samAccountName + &#8220;&lt;br&gt;&#8221;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n$ExpiryDate = $ADAccount.PasswordLastSet + $maxPasswordAgeTimeSpan<br \/>\n$TodaysDate = Get-Date<br \/>\n$DaysToExpire = $ExpiryDate &#8211; $TodaysDate<br \/>\n#Calculating DaysToExpireDD to DD format (w\/o fractional part and dot)<br \/>\n$DaysToExpireDD = $DaysToExpire.ToString() -Split (&#8220;\\S{17}$&#8221;)<br \/>\nWrite-host &#8220;The password for account $samAccountName expires on: $ExpiryDate.Days left: $DaysToExpireDD&#8221;<br \/>\nif (($DaysToExpire.Days -eq 15) -or ($DaysToExpire.Days -eq 7) -or ($DaysToExpire.Days -le 3))<br \/>\n{<br \/>\n$expiryDate = $expiryDate.ToString(&#8220;d&#8221;,$ci)<br \/>\n#Generate e-mail structure and send message<br \/>\nif ($userEmailAddress)<br \/>\n{<br \/>\nEmailStructure $userEmailAddress $expiryDate $samAccountName<br \/>\n$smtp.Send($msg)<br \/>\nWrite-Host &#8220;NOTIFICATION &#8211; $samAccountName :: e-mail was sent to $userEmailAddress&#8221;<br \/>\n$NotificationCounter = $NotificationCounter + 1<br \/>\n$ListOfAccounts = $ListOfAccounts + $samAccountName + &#8221; &#8211; $DaysToExpireDD days left. Sent to $userEmailAddress&lt;br&gt;&#8221;<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nWrite-Host &#8220;SENDING REPORT TO IT DEPARTMENT&#8221;<br \/>\nEmailStructureReport(&#8220;admin@domain.com&#8221;)<br \/>\n$smtp.Send($msgr)<\/p><\/blockquote>\n<p>The original article:\u00a0<a href=\"http:\/\/habrahabr.ru\/post\/204796\/\" target=\"_blank\">http:\/\/habrahabr.ru\/post\/204796\/ <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Import-Module ActiveDirectory #System globalization #$ci = New-Object System.Globalization.CultureInfo(&#8220;ru-RU&#8221;) #SMTP server name $smtpServer = &#8220;mail.domain.com&#8221; #Creating a Mail object $msg = new-object Net.Mail.MailMessage #Creating a Mail object for report $msgr = new-object Net.Mail.MailMessage #Creating SMTP server object $smtp = new-object Net.Mail.SmtpClient($smtpServer) #E-mail structure Function EmailStructure($to,$expiryDate,$upn) { $msg.IsBodyHtml = $true $msg.From = &#8220;admin@domain.com&#8221; $msg.To.Clear() $msg.To.Add($to) $msg.Subject = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,12],"tags":[],"class_list":["post-538","post","type-post","status-publish","format-standard","hentry","category-novosti","category-windows"],"_links":{"self":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=538"}],"version-history":[{"count":1,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/538\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/538\/revisions\/539"}],"wp:attachment":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}