Script Repository


Users with malformed street addresses

January 11, 2017
1242

This script generates and emails an HTML-formatted report containing user accounts whose street addresses specified in the Street Address attribute do not match the office names specified in the Office attribute.

To generate the reports upon request, you can create a Custom Command that runs the script. To schedule the reports, you need to create a Scheduled Task configured for the Domain-DNS object type and run it against any of your AD domains.

To add the script to a Custom Command or Scheduled Task, use the Run a program or PowerShell script action.

Parameters:

  • $to - specifies email addresses of the recipient(s) of the report;
  • $subject - specifies the email message subject;
  • $reportHeader - specifies the email message header;
  • $reportFooter - specifies the email message footer;
  • $ouDNs - specifies the Distinguished Names (DNs) of the Organizational Units in which the script will verify the street addresses.
Edit Remove
PowerShell
$to = "recipient@domain.com" # TODO: modify me
$subject = "List of users with malformed offices" # TODO: modify me
$reportHeader = "<h2><b>List of users with malformed offices:</b></h2><br/>" # 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
$ouDNs = @("OU=Users,OU=LA,DC=example,DC=com","OU=Users,OU=DC,DC=example,DC=com","OU=Users,OU=NY,DC=example,DC=com") # TODO: modify me
$officesInfo = @{
    "Office A" = "123 Ave A";
    "Office B" = "456 Ave B";
} # TODO: modify me

function BuildUserLists($ouPath, $officesInfo)
{
    # 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.BaseObjectPath = "$ouPath"
    $searcher.SearchParameters.Filter = "(sAMAccountType=805306368)"
    $searcher.SearchParameters.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SetPropertiesToLoad(@("streetAddress", "physicalDeliveryOfficeName"))
    try
    {
        $searchResult = $searcher.ExecuteSearch()
        
        # Build user lits
        $usersWithEmptyOfficeAddress = ""
        $usersWithEmptyOffice = ""
        $usersWithEmptyAddress = ""
        $usersOfficeAddressMismatch = ""
        foreach ($userId in $searchResult.FetchAll())
        {
            $userOffice = $userId.Properties["physicalDeliveryOfficeName"].Value
            $userAddress = $userId.Properties["streetAddress"].Value
            
            $user = $Context.BindToObject($userID.AdsPath)
            $userName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($user, 'IncludeParentPath')
            if ([System.String]::IsNullOrEmpty($userOffice) -and [System.String]::IsNullOrEmpty($userAddress))
            {
                $usersWithEmptyOfficeAddress += "<li>$userName</li>"
            }
            elseif ([System.String]::IsNullOrEmpty($userOffice))
            {
                $usersWithEmptyOffice += "<li>$userName</li>"
            }
            elseif ([System.String]::IsNullOrEmpty($userAddress))
            {
                $usersWithEmptyAddress += "<li>$userName</li>"
            }
            elseif ($officesInfo["$userOffice"] -ine $userAddress)
            {
                $usersOfficeAddressMismatch += "<li>$userName</li>"
            }
        }
    }
    finally
    {
        $searchResult.Dispose()
    }
    
    # Add user lists to the report
    $reportPart = New-Object "System.Text.StringBuilder"
    $reportPart.Append("<ul><li><b>Users who have neither an Office, nor an Address:</b><br/>") | Out-Null
    $reportPart.Append("<ol>$usersWithEmptyOfficeAddress</ol></li>")| Out-Null

    $reportPart.Append("<b><li>Users who don't have an Office:</b><br/>")| Out-Null
    $reportPart.Append("<ol>$usersWithEmptyOffice</ol></li>")| Out-Null
    
    $reportPart.Append("<b><li>Users who don't have an Address:</b><br/>")| Out-Null
    $reportPart.Append("<ol>$usersWithEmptyAddress</ol></li>")| Out-Null
    
    $reportPart.Append("<b><li>Users whose Office doesn't match the Address:</b><br/>")| Out-Null
    $reportPart.Append("<ol>$usersOfficeAddressMismatch</ol></li></ul>")| Out-Null
    return $reportPart.ToString()
}

foreach ($ouDN in $ouDNs)
{
    # Bind to OU
    $ou = $Context.BindToObjectByDN($ouDN)
    
    # Build report part for the current OU
    $ouName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($ou, 'IncludeParentPath')
    $report = "<h3><b>$ouName</b></h3><br/>"
    
    # Add users
    $report += BuildUserLists $ou.ADsPath $officesInfo
    $reportHeader += "$report <br/>"
}

# Build HTML report
$htmlBody = $reportHeader + $reportFooter

# Send mail
$Context.SendMail($to, $subject, $NULL, $htmlBody)


Comments ( 0 )
No results found.
Leave a comment