Ready to get started?

Learn more about the Gmail PowerShell Cmdlets or download a free trial:

Download Now

PowerShell Scripting to Replicate Gmail Data to MySQL

Write a simple PowerShell script to replicate Gmail data to a MySQL database.

The CData Cmdlets for Gmail offer live access to Gmail data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for Gmail and the CData Cmdlets for MySQL in PowerShell to replicate Gmail data to a MySQL database.

After obtaining the needed connection properties, accessing Gmail data in PowerShell and preparing for replication consists of four basic steps.

There are two ways to authenticate to Gmail. Before selecting one, first ensure that you have enabled IMAP access in your Gmail account settings. See the "Connecting to Gmail" section under "Getting Started" in the installed documentation for a guide.

The User and Password properties, under the Authentication section, can be set to valid Gmail user credentials.

Alternatively, instead of providing the Password, you can use the OAuth authentication standard. To access Google APIs on behalf on individual users, you can use the embedded credentials or you can register your own OAuth app.

OAuth also enables you to use a service account to connect on behalf of users in a Google Apps domain. To authenticate with a service account, you will need to register an application to obtain the OAuth JWT values.

In addition to the OAuth values, you will need to provide the User. See the "Getting Started" chapter in the help documentation for a guide to using OAuth.

Collecting Gmail Data

  1. Install the module:

    Install-Module GmailCmdlets
  2. Connect to Gmail:

    $gmail = Connect-Gmail -User $User -Password $Password
  3. Retrieve the data from a specific resource:

    $data = Select-Gmail -Connection $gmail -Table "Inbox"

    You can also use the Invoke-Gmail cmdlet to execute pure SQL-92 statements:

    $data = Invoke-Gmail -Connection $gmail -Query 'SELECT * FROM Inbox WHERE From = @From' -Params @{'@From'='test@test.com'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting Gmail Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the Gmail data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the Gmail resource (Inbox) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Inbox" -Columns $columns -Values $values }

You have now replicated your Gmail data to a MySQL database. This gives you freedom to work with Gmail data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to Gmail and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-Gmail -Connection $gmail -Table "Inbox" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Inbox" -Columns $columns -Values $values }
  • If you wish to replicate the Gmail data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-Gmail cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}