Av rating:
Total votes: 9
Total comments: 6


Ben Lye
Reporting on Mobile Device Activity Using Exchange 2007 ActiveSync Logs
10 October 2008

Top Tips for SysAdmins No 1.

In this new column giving practical advice on all things Sys Admin related, Ben Lye takes on the often difficult task of keeping track of mobile device activity.

I was recently asked to generate reports on how many users are using mobile devices to access their Exchange mailbox, what kind of devices are being used, and how that use has changed over time.  Fortunately, Exchange 2007 includes a PowerShell cmdlet which will parse the IIS log files on a client access server and produce CSV output files detailing the Exchange ActiveSync usage.  So, with a small amount of effort it’s possible to extract the relevant data from the Exchange logs and produce some interesting reports.

The command for exporting ActiveSync logs is intuitively called Export-ActiveSyncLog.  It takes an IIS log file as input, and generates six CSV files as output. 

Output Filename

Description

Users.csv

ActiveSync activity by user, including items sent and received, as well as device type and ID

Servers.csv

ActiveSync activity by client access server

Hourly.csv

Hour-by-hour report of ActiveSync activity

StatusCodes.csv

Summary of the HTTP response codes issued in response to ActiveSync requests

PolicyCompliance.csv

Report on device compliance with ActiveSync policy

UserAgents.csv

Summary of the different user agents used to access Exchange

For my purposes Users.csv is the most interesting part of the output as it can be used to identify who the users are, which device types are the most popular, and how much use the service is getting.  It’s worth noting that the data in the reports is taken from the server’s perspective, so “Total Emails Sent” refers to the number of emails that the server sent to the client device.

In an Exchange environment with multiple client access servers (such as an environment with servers in multiple Active Directory sites, or one using an internet-facing network-load-balancing array) you will need to export the logs from all client access servers which mobile devices connect to.  If you have a single client access server exposed to the internet which all mobile devices connect to, you’ll only need to export the logs from that one.

To use Export-ActiveSyncLog you need:

  • The Exchange Server Administrator role
  • Read-only access to the directory that contains the IIS log files

This example will export the ActiveSync data from the IIS log file of September 1st 2008.  It will use UTC times, and will put the output in C:\Temp\EASReports.

Export-ActiveSyncLog –FileName "C:\Windows\System32\LogFiles\W2SVC1\ex080901.log" –UseGMT:$true –OutputPath "C:\Temp\EASReports"

That will work fine for a single log file, but what if you need to export multiple log files?  Well, you can list all the log files in a directory using Get-ChildItem, which you can in turn pipe to the Export-ActiveSync command:

Get-ChildItem "C:\Windows\System32\LogFiles\W3SVC1" | Export-ActiveSyncLog –UseGMT:$true –OutputPath "C:\Temp\EASReports"

This syntax will combine the data from each log file and give you produce a single set of CSV files covering the entire range of the input log files.  Because I need to be able to report on usage over time this approach won’t give me what I need.

Another way to process multiple log files is to produce a set of CSV files for each log file.  However because the CSV files would typically all use the same names I also need to specify a prefix for the name of the output CSV files, which will ensure I get all the output.  For that I use the -OutputPrefix parameter of the Export-ActiveSyncLog cmdlet.

This command will create CSV files prefixed with the name of the log file they were generated from:

Get-ChildItem "C:\Windows\System32\LogFiles\W3SVC1" | ForEach { Export-ActiveSyncLog -FileName $_.FullName -OutputPath "C:\Temp\EASReports" -OutputPrefix $_.Name.Replace(".log","_") -UseGMT:$true}

Now that I have the CSV files for all my log files I can import the data into a database and run reports.  For the database I have an SQL database which consists of a single table based on the Users.csv file, with the addition of an ID field as the primary key, and a date field to store the date of the log file. 

Getting data from PowerShell into the database is a little bit more complicated.  This PowerShell script will import all the Users.csv log files which were exported with the previous command into the SQL database.

# Script for importing Exchange ActiveSync Users.csv files into a SQL database

 

# Set up the parameters for connecting to the SQL database

$dbserver = "dbserver.company.com"

$dbname = "EASReports"

$dbuser = "dbusername"

$dbpass = "dbpassword"

 

# Create the ADO database object

$objConnection = New-Object -comobject ADODB.Connection

 

# Open the database connection

$objConnection.Open("PROVIDER=SQLOLEDB;DATA SOURCE=$dbserver;UID=$dbuser;PWD=$dbpass;DATABASE=$dbname")

     

# Find all the Users.csv files and import them

Get-ChildItem "C:\Temp\EASReports\*Users.csv" | ForEach {

      # Get the date from the name of the file

      $Date = ($_.Name).SubString(2,6)

      $Year = "20" + $Date.SubString(0,2)

      $Month = $Date.SubString(2,2)

      $Day = $Date.SubString(4,2)

      $Date = Get-Date -Year $Year -Month $Month -Day $Day -Hour 0 -Minute 0 -Second 0

     

      # Import the CSV file

      $CSVFile = Import-Csv $_

 

      # Get the column names from the first line of the CSV file

      $CSVFileProperties = Get-Content "$_" -totalcount 1 | % {$_.split(",")}

 

      # Loop through each entry in the CSV file

      ForEach ($Entry in $CSVFile) {

 

            # Ignore lines with an empty Device ID

            If ($Entry."Device ID" -ne "") {

                  # Construct the SQL insert statement

                  $SQLString = "INSERT INTO Users ("

                  Foreach ($Prop in $CSVFileProperties) {

                        $SQLString = $SQLString + "[$Prop],"

                  }

                 

                  $SQLString = $SQLString + "[Date]) VALUES ("

                 

                  Foreach ($Prop in $CSVFileProperties) {

                        $SQLString = $SQLString + "'" + $Entry."$Prop" + "',"

                  }

     

                  $SQLString = $SQLString + "'$Date')"

                 

                  # Add the record to the database

                  $null = $objConnection.Execute($SQLString)

            }

 

      }

}

 

# Close the database connection

$objConnection.Close()

With the data in an SQL database I can then use Excel to connect to the database and analyze the data.  The resulting output looks like this::

As I said at the beginning, it takes a small amount of effort to extract the data and get it into a format suitable for long-term reports, but once the pieces are in place it’s a relatively simple task.

More information on the Export-ActiveSyncLog cmdlet can be found on the Microsoft Exchange TechNet web site can be found on the Microsoft Exchange TechNet web site



This article has been viewed 3669 times.
Ben Lye

Author profile: Ben Lye

Ben Lye is a senior systems administrator at a multi-national software company. He has over 10 years experience supporting and administering Windows and Exchange, and has been MCSE and MCP certified since 1999. Ben is passionate about automating and streamlining routine tasks, and enjoys creating and using tools which make day-to-day administration easier.

Search for other articles by Ben Lye

Rate this article:   Avg rating: from a total of 9 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: The ActiveSync reporting
Posted by: Andrew (view profile)
Posted on: Monday, January 05, 2009 at 6:42 PM
Message: I'd love to use this process in my own environment but I'm getting errors on the import. I'm guessing that I need to have some stuff pre-created in the DB before I can run the script? Unfortunately the article doesn't go into that part. As it stands, I get an exception on the "execute" with the message "Invalid object name 'Users'."

Subject: Database Schema
Posted by: benlye (view profile)
Posted on: Tuesday, January 13, 2009 at 6:53 AM
Message: The database table is called 'Users', and it simply contains a column for every field in the CSV file, plus an Identity column called 'ID'.

You can create the table by hand using the Users.csv file as a template, or use this create table script from SQL Server 2005. Beware of line wraps when you copy and paste it.

/* Begin SQL Script */
USE [EASReports]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[User Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Device ID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Device Type] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Items Sent] [int] NULL,
[Items Received] [int] NULL,
[Hits] [int] NULL,
[Total Bytes Sent] [int] NULL,
[Total Bytes Received] [int] NULL,
[Total Emails Sent] [int] NULL,
[Total Emails Received] [int] NULL,
[Total Calendar Sent] [int] NULL,
[Total Calendar Received] [int] NULL,
[Total Contacts Sent] [int] NULL,
[Total Contacts Received] [int] NULL,
[Total Tasks Sent] [int] NULL,
[Total Tasks Received] [int] NULL,
[Total OOF Messages Set by Client] [int] NULL,
[Total OOF Messages Retrieved from Server] [int] NULL,
[Total Searches requested] [int] NULL,
[Total SharePoint Access] [int] NULL,
[Total UNC Access] [int] NULL,
[Total Attachment Downloads] [int] NULL,
[Total Attachment Size] [int] NULL,
[Currently Compliant with Policy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Date] [datetime] NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
/* End SQL Script */

Subject: Reporting on Mobile Device
Posted by: V.ASM (view profile)
Posted on: Friday, January 23, 2009 at 1:35 PM
Message: Worked great for me (both collecting logs and exporting to SQL) so thanks very much. I was able to modify the script to collect logs from multiple CAS servers and export to SQL. Is there a way to export that data to a simple chart? The goal is to schedule the script and email that chart to a Sharepoint site.

Subject: Reporting on Mobile Device
Posted by: V.ASM (view profile)
Posted on: Friday, January 23, 2009 at 5:23 PM
Message: Worked great for me (both collecting logs and exporting to SQL) so thanks very much. I was able to modify the script to collect logs from multiple CAS servers and export to SQL. Is there a way to export that data to a simple chart? The goal is to schedule the script and email that chart to a Sharepoint site.

Subject: Exporting data to a chart
Posted by: benlye (view profile)
Posted on: Tuesday, January 27, 2009 at 12:52 PM
Message: You might want to take a look at PowerGadgets for exporting the data to a chart. I haven't used it personally, but from the look of it this would be something it could do.

Subject: Getting device OS version
Posted by: Howserx (view profile)
Posted on: Wednesday, June 17, 2009 at 11:04 AM
Message: I've just finished implementing this and everything works great. I have one question... The UserAgents.csv has the PhoneType & OSVersion concatenated together in the UserAgent column. The csv has the amount of hits and unique devices, is there a way (I assume using one of the other IIS logs) to find out which devices have what OS version. We'd like to be able to get our devices to specific versions.

 

Cluster Continuous Replication Network Design
 Cluster continuous replication (CCR) is a means of providing a more resilient email system with faster... Read more...

Using Exchange 2007 Transport Rules to Protect the First Entry in the Address Book
 Global Address Lists in MS Exchange can cause problems because the first person in the list often gets... Read more...

Emulating the Exchange 2003 RUS for Out-of-Band Mailbox Provisioning in Exchange 2007
 Exchange's Recipient Update Service was important in Exchange 2000 or 2003 in order to complete the... Read more...

The Postmasters
 The Exchange Team introduces themselves, and keeps you up-to-date Read more...

For this Exchange Server Archiver, “Transparency” Fits
 Sometimes, it is a great relief when a user of your software gives it a tough test and then reports... Read more...

Using Exchange 2007 for Resource Booking
 The process of booking various resources to go with a meeting room just got a whole lot easier with... Read more...

Free Exchange Server eBook
 Simple-Talk has teamed up with Sybex to give you a free copy of "Best of Exchange Server 2007" Read more...

Managing Exchange 2007 Mailbox Quotas with Windows PowerShell
 The use of PowerShell with Exchange Server 2007 can do a great deal to ease the task of managing... Read more...

Message Hygiene in Exchange Server 2007
 Around four out of every five email messages are spam. Now that the nuisance threatens to engulf what... Read more...

Controlling Email Messages using Exchange's Transport Rules
 Some tasks that should have been easy in previous versions of Exchange just weren't. Now, with... Read more...

Over 150,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk