Automate Gmail Integration Tasks from PowerShell
The CData ADO.NET Provider for Gmail is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Gmail.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Gmail; this tutorial shows how to use the Provider to create, retrieve, update, and delete Gmail data.
Once you have acquired the necessary connection properties, accessing Gmail data in PowerShell can be enabled in three 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, register an application to obtain the OAuth JWT values.
In addition to the OAuth values, provide the User. See the "Getting Started" chapter in the help documentation for a guide to using OAuth.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Gmail\lib\System.Data.CData.Gmail.dll") -
Connect to Gmail:
$conn= New-Object System.Data.CData.Gmail.GmailConnection("User=username;Password=password;") $conn.Open() -
Instantiate the GmailDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Subject, Size from Inbox" $da= New-Object System.Data.CData.Gmail.GmailDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.subject $_.size }
Update Gmail Data
$cmd = New-Object System.Data.CData.Gmail.GmailCommand("UPDATE Inbox SET From='[email protected]' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Gmail.GmailParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Gmail Data
$cmd = New-Object System.Data.CData.Gmail.GmailCommand("INSERT INTO Inbox (From) VALUES (@myFrom)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Gmail.GmailParameter("@myFrom","[email protected]")))
$cmd.ExecuteNonQuery()
Delete Gmail Data
$cmd = New-Object System.Data.CData.Gmail.GmailCommand("DELETE FROM Inbox WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Gmail.GmailParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject