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 Salesforce Marketing Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Salesforce Marketing data from PowerShell? This article demonstrates how to utilize the Salesforce Marketing Cmdlets for tasks like connecting to Salesforce Marketing data, automating operations, downloading data, and more.
The CData Cmdlets for Salesforce Marketing 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 Salesforce Marketing.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Salesforce Marketing, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Salesforce Marketing data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Salesforce Marketing Cloud. To access Salesforce Marketing data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Salesforce Marketing Cloud.
Once you have acquired the necessary connection properties, accessing Salesforce Marketing data in PowerShell can be enabled in three steps.
Authenticating to the Salesforce Marketing Cloud APIs
Set the User and Password to your login credentials, or to the credentials for a sandbox user if you are connecting to a sandbox account.
Connecting to the Salesforce Marketing Cloud APIs
By default, the data provider connects to production environments. Set UseSandbox to true to use a Salesforce Marketing Cloud sandbox account.
The default Instance is s7 of the Web Services API; however, if you use a different instance, you can set Instance.
PowerShell
-
Install the module:
Install-Module SFMarketingCloudCmdlets
-
Connect:
$sfmarketingcloud = Connect-SFMarketingCloud -User "$User" -Password "$Password"
-
Search for and retrieve data:
$emailaddress = "[email protected]" $subscriber = Select-SFMarketingCloud -Connection $sfmarketingcloud -Table "Subscriber" -Where "EmailAddress = `'$EmailAddress`'" $subscriber
You can also use the Invoke-SFMarketingCloud cmdlet to execute SQL commands:
$subscriber = Invoke-SFMarketingCloud -Connection $sfmarketingcloud -Query 'SELECT * FROM Subscriber WHERE EmailAddress = @EmailAddress' -Params @{'@EmailAddress'='[email protected]'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Salesforce Marketing Cloud\lib\System.Data.CData.SFMarketingCloud.dll")
-
Connect to Salesforce Marketing:
$conn= New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudConnection("User=myUser;Password=myPassword;InitiateOAuth=GETANDREFRESH") $conn.Open()
-
Instantiate the SFMarketingCloudDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Status from Subscriber" $da= New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.status }
Update Salesforce Marketing Data
PowerShell
Update-SFMarketingCloud -Connection $SFMarketingCloud -Columns @('Id','Status') -Values @('MyId', 'MyStatus') -Table Subscriber -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudCommand("UPDATE Subscriber SET EmailAddress='[email protected]' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Salesforce Marketing Data
PowerShell
Add-SFMarketingCloud -Connection $SFMarketingCloud -Table Subscriber -Columns @("Id", "Status") -Values @("MyId", "MyStatus")
ADO.NET
$cmd = New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudCommand("INSERT INTO Subscriber (EmailAddress) VALUES (@myEmailAddress)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudParameter("@myEmailAddress","[email protected]")))
$cmd.ExecuteNonQuery()
Delete Salesforce Marketing Data
PowerShell
Remove-SFMarketingCloud -Connection $SFMarketingCloud -Table "Subscriber" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudCommand("DELETE FROM Subscriber WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SFMarketingCloud.SFMarketingCloudParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject