Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Marketo Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Connecting to Marketo
The following connection options are required when connecting to Marketo.- URL: The URL of the REST API Service. You can locate this in Admin -> Integration -> Web Services -> REST API. Example values: https://123-abc-456.mktorest.com/ or https://123-abc-456.mktorest.com/rest.
- OAuthClientId: The OAuth Client ID associated with your custom service.
- OAuthClientSecret: The OAuth Client Secret associated with your custom service.
Obtaining OAuth Credentials
Before obtaining OAuth credentials you need to create a custom service if you do not already have one. To do so follow the instructions provided in Creating a Custom Service.
To obtain OAuth credentials for a custom service:
- Go to the Admin tab.
- Click on the LaunchPoint option.
- Select the service and click on View Details.
- Marketo displays a window that shows the authentication credentials. Use "Client Id" as the value for OAuthClientId and "Client Secret" for OAuthClientSecret.
Creating a Connection Object
You can then use the Connect-Marketo cmdlet to create a connection object that can be passed to other cmdlets:
$conn = -Connect-Marketo -URL 'https://MyMarketoUrl.mktorest.com/' -OAuthClientId 'MyOAuthClientId' -OAuthClientSecret 'MyOAuthClientSecret'
Retrieving Data
The Select-Marketo cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Marketo -Connection $conn -Table "Leads" -Columns @("Id, Email") -Where "Email='[email protected]'"
The Invoke-Marketo cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-Marketo -Connection $conn -Table Leads -Where "Email = '[email protected]'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myLeadsData.csv -NoTypeInformation
You will notice that we piped the results from Select-Marketo into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = -Connect-Marketo -URL 'https://MyMarketoUrl.mktorest.com/' -OAuthClientId 'MyOAuthClientId' -OAuthClientSecret 'MyOAuthClientSecret' PS C:\> $row = Select-Marketo -Connection $conn -Table "Leads" -Columns (Id, Email) -Where "Email = '[email protected]'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Leads", "Columns": [ ], "Id": "MyId", "Email": "MyEmail" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Marketo -Connection $conn -Table Leads -Where "Email = '[email protected]'" | Remove-Marketo
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Marketo, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyLeadsUpdates.csv | %{
$record = Select-Marketo -Connection $conn -Table Leads -Where ("Id = `'"+$_.Id+"`'")
if($record){
Update-Marketo -Connection $conn -Table Leads -Columns @("Id","Email") -Values @($_.Id, $_.Email) -Where "Id = `'$_.Id`'"
}else{
Add-Marketo -Connection $conn -Table Leads -Columns @("Id","Email") -Values @($_.Id, $_.Email)
}
}