Script Repository


Active users with exchange mailboxes

June 30, 2017
1123

The script creates and emails a CSV-formatted report on all active user accounts that have mailboxes.

To schedule a report, create a Scheduled Task configured for the Domain-DNS object type that runs the script and assign it over any of your AD domains. To add the script to a Scheduled Task, use the Run a program or PowerShell script action.

Parameters:

  • $csvFilePath - specifies a full path to the CSV file containing the report;
  • $removeCsvFile - specifies whether to remove the CSV file after sending;
  • $propertiesToExport - specifies LDAP names of mailbox properties to export to the report;
  • $to - specifies a comma separated list of recipients of the report;
  • $subject - specifies the email message subject;
  • $from - specifies the email message sender;
  • $mailServer - specifies the SMTP server to use when sending reports;
  • $body - specifies the body of the email message (in plain text format).
Edit Remove
PowerShell
$csvFilePath = "C:\Scripts\Report.csv" # TODO: modify me
$removeCsvFile = $True # TODO: modify me
$propertiesToExport = @("givenName", "middleName", "sn", "title", "co", "mail") # TODO: modify me

# Email message settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Active Users with Exchange Mailboxes" # TODO: modify me
$from = "noreply@domain.com" # TODO: Modify me
$mailServer = "mail.domain.com" # TODO: Modify me
$body = "Active Users with Exchange Mailboxes" # TODO: Modify me

function SearchObjects($filter, $propertiesToLoad)
{
    # Set search parameters
    $searcher = $Context.BindToObject("Adaxes://rootDSE")
    $searcher.SearchFilter = $filter
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 500
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SetPropertiesToLoad($propertiesToLoad)
    $searcher.VirtualRoot = $True
    
    try
    {
        # Perform the search
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        return ,$searchResults
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Find all active users with mailboxes
$dateFileTime = [System.DateTime]::UtcNow.ToFileTimeUTC()
$searchResults = SearchObjects "(&(sAMAccountType=805306368)(|(accountExpires=>$dateFileTime)(accountExpires=0)(accountExpires=9223372036854775807))(!(userAccountControl:1.2.840.113556.1.4.803:=2))(mailNickname=*)(msExchHomeServerName=*))" $propertiesToExport

if ($searchResults.Length -eq 0)
{
    $Context.LogMessage("No active users with Exchange mailboxes found", "Information")
    return
}

$records = @($NULL) * $searchResults.Length
for ($i = 0; $i -lt $searchResults.Length; $i++)
{
    $record = @{}
    foreach ($property in $propertiesToExport)
    {
        $record.Add($property, $searchResults[$i].Properties[$property].Value)
    }
    $records[$i] = New-Object PSObject -Property $record
}
$records | Export-Csv -Path $csvFilePath -NoTypeInformation

# Send mail
Send-MailMessage -to $to -From $from -Subject $subject -Body $body -SmtpServer $mailServer -Attachments $csvFilePath

if ($removeCsvFile)
{
    # Remove CSV File
    Remove-Item $csvFilePath -Force
}


Comments ( 0 )
No results found.
Leave a comment