Great, thanks! Excited for the expanded reporting functionality.
In the simplest form, I should be able to update the attributeName variable here to what attribute I want the report to run on, and place an email address in the to field. I've attempted to set this up as a custom command, configuring it to look at OU objects, and then run it off an OU and can't get it to email a report.
I also created a scheduled task and pointing it to an OU and came up blank. Thanks!
$attributeName = "employeeNumber" # TODO: modify me
# Email message settings
$to = 'Emailaddress@domain.com' # TODO: modify me
$subject = "Users whose '$attributeName' attribute is empty" # TODO: modify me
$reportHeader = "<h1><b>Users whose '$attributeName' attribute is empty</b></h1><br/>" # TODO: modify me
$table = @"
<table border="1">
<tr>
<th>Full name</th>
<th>Logon name</th>
</tr>
"@ # TODO: modify me
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me
# Search all users in the target object whose attribute is empty
$searcher = $Context.TargetObject
$searcher.PageSize = 500
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchFilter = "(&(sAMAccountType=805306368)(!($attributeName=*)))"
$searcher.SetPropertiesToLoad(@("userPrincipalName","cn"))
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
# Build report
if ($searchResults.Length -eq 0)
{
$html = $reportHeader + "<b>No users found</b>" + $reportFooter
}
else
{
foreach ($searchResult in $searchResults)
{
# Add users to report
$table += "<tr><td>" + $searchResult.Properties["cn"].Value + "</td>"
$table += "<td>" + $searchResult.Properties["userPrincipalName"].Value + "</td>"
}
$html = $reportHeader + $table + "</table>" + $reportFooter
}
}
finally
{
# Release resources
if ($searchResultIterator) { $searchResultIterator.Dispose() }
}