Category Archives: Scripting

How to Delete Emails from Mailboxes in Exchange 2010 Using Search-Mailbox

Exchange 2010, Exchange 2013, Scripting, Security

Quick post today.  If you have an criteria of emails that you want to delete from all mailbox’s you can use Search-Mailbox to delete the message from users mailbox.  For example, you get hit with a virus email that has the subject “I am a virus” from the user virussender@virus.com.

The below search goes through all mailbox’s and deletes any email with the subject “I am a virus”:

 

get-mailbox | Search-Mailbox -SearchQuery ‘subject:”I am a virus.” ‘ -TargetMailbox “adminmailbox” -TargetFolder “DeleteVirus” -LogLevel Full –DeleteContent

 

That above command will delete the emails from the users mailbox’s and copy them to the adminmailbox in the folder DeleteVirus

The next search does it based on the sender of the email:

get-mailbox | Search-Mailbox -SearchQuery ‘from:virussender@virus.com ‘ -TargetMailbox “adminmailbox” -TargetFolder “DeleteVirus” -LogLevel Full –DeleteContent

 

The above search does the same thing as the first, except this query does it based on the from address of the sender versus the subject.

If you log into the admin mailbox, you can see the results of the search and log files for it:

 

image

You can also run the same commands without the –DeleteContent switch to copy the messages to the admin mailbox for review before running the commands to delete.

Remember, do this at your own risk and test test test before you run it!

(UPDATED) How to Get a Report of Active Sync Devices in Exchange 2010/Exchange 2013

ActiveSync, Exchange 2010, Exchange 2013, Scripting

***UPDATE***

 

There was a lot of feedback from people who wanted extra items and fields added in so I edited the original script.  It will now also include the users Primary SMTP Address, LastSyncAttemptTime, and LastSyncSuccessTime.  Also edited it for environments with over a thousand mailboxes. Below is the updated script code:

The new output will look like this:

image

image

 

Ever had the need to get a nice report of all active sync devices in your Exchange organization?  Well then I have the script for you!

This script will get through all active sync devices, and match them up with their respective owners.  It will also output the Device Type, Device Model, and most importantly the Device OS.

Why is this important?  As some of you remember shortly back, there was an issue with Apple iOS devices causing excessive logging on Exchange Mailbox servers.  As part of the way to fix that you could block or quarantine those devices.  You most likely want to be able to see who you are blocking though, so you you know who your going to annoy and warn them preemptively.

The script requires 4 parameters to run, and should be run from an Exchange Management Shell:

  1. SMTPServer = SMTP server as the report will send you a copy of the report
  2. SMTPFrom = The FROM address of the email
  3. SMTPTo = The recipient of the email
  4. ExportPath = The folder location where you want the CSV export of the report. 

For example, to send the report to admin@port25guy.com using the SMTP server relay.port25guy.com, have the from address be reports@port25guy.com and export the CSV to C:\Reports:

Get-ActiveSyncReport.ps1 –exportpath C:\Reports –smtpserver relay.port25guy.com –smtpfrom reports@port25guy.com –smtpto admin@port25guy.com

After running that, if we check the C:\reports folder we should have a nice CSV export:

image

And a nice email report in your inbox:

 

image

You can download the script from the link below, just rename the file to extension .ps1

Here is the ***UPDATED** script block as well:

 

### BEGINNING OF SCRIPT

#####
#
# Get-ActiveSyncDeviceReport
# Author: Paul Ponzeka
# Website: port25guy.com
# email ponzekap2 at gmail dot com
#
######
param(
    [Parameter(Mandatory = $true)]
    [string] $SMTPServer = “”,
    [Parameter(Mandatory = $true)]
    [string] $SMTPFrom = “”,
    [Parameter(Mandatory = $true)]
    [string] $SMTPTo = “”,
    [Parameter(Mandatory = $true)]
    [string] $exportpath = “”
    )

#######
#
# HTML Formatting Section
# Thanks to Paul Cunningham at http://exchangeserverpro.com/
#
#######
#
#
#
######
$style = “<style>BODY{font-family: Arial; font-size: 10pt;}”
$style = $style + “TABLE{border: 1px solid black; border-collapse: collapse;}”
$style = $style + “TH{border: 1px solid black; background: #dddddd; padding: 5px; }”
$style = $style + “TD{border: 1px solid black; padding: 5px; }”
$style = $style + “</style>”

$messageSubject = “ActiveSync Device Report”

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

####  Get Mailbox

$EASDevices = “”
$AllEASDevices = @()

$EASDevices = “”| select ‘User’,’PrimarySMTPAddress’,’DeviceType’,’DeviceModel’,’DeviceOS’, ‘LastSyncAttemptTime’,’LastSuccessSync’
$EasMailboxes = Get-Mailbox -ResultSize unlimited
foreach ($EASUser in $EasMailboxes) {
$EASDevices.user = $EASUser.displayname
$EASDevices.PrimarySMTPAddress = $EASUser.PrimarySMTPAddress.tostring()
    foreach ($EASUserDevices in Get-ActiveSyncDevice -Mailbox $EasUser.alias) {
    $EASDeviceStatistics = $EASUserDevices | Get-ActiveSyncDeviceStatistics
    $EASDevices.devicetype = $EASUserDevices.devicetype
    $EASDevices.devicemodel = $EASUserDevices.devicemodel
    $EASDevices.deviceos = $EASUserDevices.deviceos
    $EASDevices.lastsyncattempttime = $EASDeviceStatistics.lastsyncattempttime
    $EASDevices.lastsuccesssync = $EASDeviceStatistics.lastsuccesssync
    $AllEASDevices += $EASDevices | select user,primarysmtpaddress,devicetype,devicemodel,deviceos,lastsyncattempttime,lastsuccesssync
    }
    }
$AllEASDevices = $AllEASDevices | sort user
$AllEASDevices
$AllEASDevices | Export-Csv $exportpath\ActiveSyncReport.csv

######
#
# Send Email Report
#
########

$message.Body = $AllEasDevices | ConvertTo-Html -Head $style

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

##END OF SCRIPT

Also, special thanks to Paul Cunningham at http://exchangeserverpro.com.  He wrote the HTML formatting section in the script that makes this look nice and pretty, versus my junky plain text (http://exchangeserverpro.com/powershell-html-email-formatting).  If you haven’t check out Paul’s site you should, he has great information on there. 

Hope you find the script helpful!