![]() |
VOOZH | about |
The CData Cmdlets for MailChimp are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MailChimp.
The Cmdlets are not only a PowerShell interface to MailChimp, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete MailChimp data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for MailChimp. To access MailChimp data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for MailChimp.
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.
Install the module:
Install-Module MailChimpCmdlets
Connect:
$mailchimp = Connect-MailChimp -APIKey "$APIKey"
Search for and retrieve data:
$contact_country = "US" $lists = Select-MailChimp -Connection $mailchimp -Table "Lists" -Where "Contact_Country = `'$Contact_Country`'" $lists
You can also use the Invoke-MailChimp cmdlet to execute SQL commands:
$lists = Invoke-MailChimp -Connection $mailchimp -Query 'SELECT * FROM Lists WHERE Contact_Country = @Contact_Country' -Params @{'@Contact_Country'='US'}
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 -Connection $MailChimp -Columns @('Name','Stats_AvgSubRate') -Values @('MyName', 'MyStats_AvgSubRate') -Table Lists -Id "MyId"
$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()
Add-MailChimp -Connection $MailChimp -Table Lists -Columns @("Name", "Stats_AvgSubRate") -Values @("MyName", "MyStats_AvgSubRate")
$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()
Remove-MailChimp -Connection $MailChimp -Table "Lists" -Id "MyId"
$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
Download a free trial of the MailChimp Data Provider to get started:
Download NowLearn more:
👁 MailChimp IconComplete read-write access to MailChimp enables developers to search (Lists, Campaigns, Reports, etc.), update items, edit customers, and more, from any .NET application.