Automate MailChimp Integration Tasks from PowerShell
The CData ADO.NET Provider for MailChimp 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 MailChimp.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for MailChimp; this tutorial shows how to use the Provider to create, retrieve, update, and delete MailChimp data.
Once you have acquired the necessary connection properties, accessing MailChimp data in PowerShell can be enabled in three steps.
You can set the APIKey to the key you generate in your account settings, or, instead of providing your APIKey, you can use the OAuth standard to authenticate the application. OAuth can be used to enable other users to access their own data. To authenticate using OAuth, obtain the OAuthClientId, OAuthClientSecret, and CallbackURL by registering an app with MailChimp.
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 MailChimp\lib\System.Data.CData.MailChimp.dll") -
Connect to MailChimp:
$conn= New-Object System.Data.CData.MailChimp.MailChimpConnection("APIKey=myAPIKey;") $conn.Open() -
Instantiate the MailChimpDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, Stats_AvgSubRate from Lists" $da= New-Object System.Data.CData.MailChimp.MailChimpDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.stats_avgsubrate }
Update MailChimp Data
$cmd = New-Object System.Data.CData.MailChimp.MailChimpCommand("UPDATE Lists SET Contact_Country='US' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MailChimp.MailChimpParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert MailChimp Data
$cmd = New-Object System.Data.CData.MailChimp.MailChimpCommand("INSERT INTO Lists (Contact_Country) VALUES (@myContact_Country)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MailChimp.MailChimpParameter("@myContact_Country","US")))
$cmd.ExecuteNonQuery()
Delete MailChimp Data
$cmd = New-Object System.Data.CData.MailChimp.MailChimpCommand("DELETE FROM Lists WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MailChimp.MailChimpParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject