Great - that gets me most of it - however, I'd also like to include the date that the account expires on.
According to the web, I just need to update it like the below:
$numDays = 30
$to = "<email>"
# Email message setings
$subject = "INFORM: PCI Compliance Account Expiration Notice"
$htmlReportFirstPart = @"
<h3><b>Users whose accounts expire in the next $numDays days</b></h3><br/>
<table border="1">
<tr>
<th>Full name</th>
<th>Logon name</th>
<th>Account Expiration Date</th>
</tr>
"@
$htmlReportLastPart = @"
</table><br/>
<p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>
"@
$accountExpiresDate = ((Get-Date).AddDays($numDays)).ToFileTime()
$currentDate = (Get-Date).ToFileTime()
# Search all users
$searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
$searcher.SearchParameters.PageSize = 500
$searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchParameters.Filter = "(&(objectCategory=user)(accountExpires>=$currentDate)(accountExpires<=$accountExpiresDate))"
$searcher.VirtualRoot = $True
$result = $searcher.ExecuteSearch()
$users = $result.FetchAll()
$result.Dispose()
if ($users.Count -eq 0)
{
return
}
foreach ($user in $users)
{
$user = $Context.BindToObject($user.AdsPath)
# Add user information to the report
$htmlReportFirstPart += "<tr><td>" + $user.Get("cn") + "</td>"
$htmlReportFirstPart += "<td>" + $user.Get("userPrincipalName") + "</td>"
$htmlReportFirstPart += "<td>" + [datetime]::FromFileTime($user.Get("accountExpires")) + "</td></tr>"
}
# Build message body in HTML
$htmlBody = $htmlReportFirstPart + $htmlReportLastPart
# Send mail to the initiator
$Context.SendMail($to, $subject, $NULL, $htmlBody)
Specifically:
$htmlReportFirstPart += "<td>" + [datetime]::FromFileTime($user.Get("accountExpires")) + "</td></tr>"
However, when I run this code, it does not put in anything the the third column.
If I change it to
$htmlReportFirstPart += "<td>" + $user.Get("accountExpires") + "</td></tr>"
I get the "File Time" display - which isn't very helpful.
Is there a different set of code for changing the date back into an easily "readable" date format.