We are proud to share our inclusion in the 2024 Gartner Magic Quadrant for Data Integration Tools. We believe this recognition reflects the differentiated business outcomes CData delivers to our customers.
Get the Report →Automate ActiveCampaign Integration Tasks from PowerShell
Are you in search of a quick and easy way to access ActiveCampaign data from PowerShell? This article demonstrates how to utilize the ActiveCampaign Cmdlets for tasks like connecting to ActiveCampaign data, automating operations, downloading data, and more.
The CData Cmdlets for ActiveCampaign 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 ActiveCampaign.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to ActiveCampaign, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete ActiveCampaign data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for ActiveCampaign. To access ActiveCampaign data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for ActiveCampaign.
Once you have acquired the necessary connection properties, accessing ActiveCampaign data in PowerShell can be enabled in three steps.
ActiveCampaign supports authenticating with the API Key. To connect to ActiveCampaign, set the following:
- URL: This can be found in your account on the My Settings page under the Developer tab. For example: https://{yourAccountName}.api-us1.com
- APIKey: This can be found in your account on the Settings page under the Developer tab. Each user in your ActiveCampaign account has their own unique API key.
PowerShell
-
Install the module:
Install-Module ActiveCampaignCmdlets
-
Connect:
$activecampaign = Connect-ActiveCampaign -URL "$URL" -APIKey "$APIKey"
-
Search for and retrieve data:
$lastname = "Smith" $contacts = Select-ActiveCampaign -Connection $activecampaign -Table "Contacts" -Where "LastName = `'$LastName`'" $contacts
You can also use the Invoke-ActiveCampaign cmdlet to execute SQL commands:
$contacts = Invoke-ActiveCampaign -Connection $activecampaign -Query 'SELECT * FROM Contacts WHERE LastName = @LastName' -Params @{'@LastName'='Smith'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for ActiveCampaign\lib\System.Data.CData.ActiveCampaign.dll")
-
Connect to ActiveCampaign:
$conn= New-Object System.Data.CData.ActiveCampaign.ActiveCampaignConnection("URL=yourUrl;APIKey=yourApiKey") $conn.Open()
-
Instantiate the ActiveCampaignDataAdapter, execute an SQL query, and output the results:
$sql="SELECT LastName, Email from Contacts" $da= New-Object System.Data.CData.ActiveCampaign.ActiveCampaignDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.lastname $_.email }
Update ActiveCampaign Data
PowerShell
Update-ActiveCampaign -Connection $ActiveCampaign -Columns @('LastName','Email') -Values @('MyLastName', 'MyEmail') -Table Contacts -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.ActiveCampaign.ActiveCampaignCommand("UPDATE Contacts SET LastName='Smith' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ActiveCampaign.ActiveCampaignParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert ActiveCampaign Data
PowerShell
Add-ActiveCampaign -Connection $ActiveCampaign -Table Contacts -Columns @("LastName", "Email") -Values @("MyLastName", "MyEmail")
ADO.NET
$cmd = New-Object System.Data.CData.ActiveCampaign.ActiveCampaignCommand("INSERT INTO Contacts (LastName) VALUES (@myLastName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ActiveCampaign.ActiveCampaignParameter("@myLastName","Smith")))
$cmd.ExecuteNonQuery()
Delete ActiveCampaign Data
PowerShell
Remove-ActiveCampaign -Connection $ActiveCampaign -Table "Contacts" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.ActiveCampaign.ActiveCampaignCommand("DELETE FROM Contacts WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ActiveCampaign.ActiveCampaignParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject