Hello again,
Here you are:
$email = "somebody@mii.com" # TODO: modify me
$propertyNames = @("DisplayName","MIIUSDenverProductionSQL","MIIUSDenverTestSQL","MIITopTeam","MIIMSDN","MIILabManager","MIIADN","MIITFS2","MIIUSTestTrack","MIIUSAegisTestTrack","MIINAmericaTestTrack","MIIUSRobbinsTestTrack","MIIUSEngineOneTestTrack","MIIUSTrencoTestTrack","MIISapphirePortalTestTrack","MIISapphireOEMTestTrack","MIIGlobalWebTestTrack","MIIUSTestTrackMiTekBAEApplications","MIIUSTestTrackMitekPortalsandAccessoryApps","MIIUSTestTrackWebandSWE") # TODO: modify me
$bodyBuilder = New-Object "System.Text.StringBuilder"
$bodyBuilder.append("<html><head>") | Out-Null
$bodyBuilder.append("<meta http-equiv=""Content-Type"" content=""text/html charset=UTF-8""></head>") | Out-Null
$bodyBuilder.append("<body>") | Out-Null
$bodyBuilder.append("Rob & Dave: This is an annual security review required by Berkshire Hathaway auditors. Please review the various security areas and those who have been authorized security to these areas. If you wish to modify any of the security options for an individual, please complete a security request appropriately. If you have any questions, please contact Michael Paul; otherwise you do not need to respond. Unless I hear from you, it is expected by default that you have completed the review and made any necessary security changes accordingly within 30 days of receiving this email report. Thank you!") | Out-Null
$bodyBuilder.append("<table width=""100%%"" border=""1"">") | Out-Null
$bodyBuilder.append("<tr>") | Out-Null
foreach ($propertyName in $propertyNames)
{
$bodyBuilder.append("<th>$propertyName</th>") | Out-Null
}
$bodyBuilder.append("</tr>") | Out-Null
# Find all users under the target object
$searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
$searcher.SearchParameters.PageSize = 500
$searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchParameters.BaseObjectPath = $Context.TargetObject.AdsPath
$searcher.SearchParameters.Filter = "(&(objectCategory=person)(objectClass=user))"
$searcher.SearchParameters.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad($propertyNames)
$searcherResult = $searcher.ExecuteSearch()
$users = $searcherResult.FetchAll()
$searcherResult.Dispose()
# Iterate through search results and include them into the report
foreach ($user in $users)
{
$bodyBuilder.append("<tr>") | Out-Null
$resultPropertyCollection = $user.Properties
# Skip if none of the $propertyNames are set
if (($resultPropertyCollection.Count -eq 0) -or (($resultPropertyCollection.Count -eq 1) -and ($resultPropertyCollection.Contains("DisplayName"))))
{
continue
}
foreach ($propertyName in $propertyNames)
{
# If the property is not set for the user
if (!($resultPropertyCollection.Contains($propertyName)))
{
$bodyBuilder.append("<td> </td>") | Out-Null
continue
}
# include values for the property in the report
$values = ($resultPropertyCollection[$propertyName]).Values
$bodyBuilder.append("<td>")
foreach ($value in $values)
{
$bodyBuilder.append("$value")
}
$bodyBuilder.append("</td>")
}
$bodyBuilder.append("</tr>") | Out-Null
}
$bodyBuilder.append("</table>") | Out-Null
$bodyBuilder.append("</body></html>") | Out-Null
# Send mail
$Context.SendMail($email, "Annual Required Security Audit: SWE", $null, $bodyBuilder.ToString())
Here's the portion of the script that skips a user if none of the properties are set:
# Skip if none of the $propertyNames are set
if (($resultPropertyCollection.Count -eq 0) -or (($resultPropertyCollection.Count -eq 1) -and ($resultPropertyCollection.Contains("DisplayName"))))
{
continue
}