Cmdlets for SendGrid

Build 24.0.9060

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 SendGrid Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module SendGridCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module SendGridCmdlets;

You can then use the Connect-SendGrid cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-SendGrid -APIKey 'MyAPIKey'

You must use an API key to authenticate through the SendGrid cmdlet.

Obtaining an API Key

To obtain an API key, follow the steps below:

  1. Log into SendGrid.
  2. Click Settings.
  3. Click API keys.
  4. Click Create API Key.
  5. Choose the Permissions the API Key should have, either Full Access or Restricted Access. The API key is then displayed.

NOTE: If you want access to Email events, you must purchase the Email Activity History add-in of SendGrid. If using a Restricted Access key, you must give it the messages.read and email_activity.read scopes.

Retrieving Data

The Select-SendGrid cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-SendGrid -Connection $conn -Table "MarketingCampaigns" -Columns @("Title, Subject") -Where "Id='17693'"
The Invoke-SendGrid 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-SendGrid -Connection $conn -Table MarketingCampaigns -Where "Id = '17693'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myMarketingCampaignsData.csv -NoTypeInformation

You will notice that we piped the results from Select-SendGrid 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-SendGrid -APIKey 'MyAPIKey'
PS C:\> $row = Select-SendGrid -Connection $conn -Table "MarketingCampaigns" -Columns (Title, Subject) -Where "Id = '17693'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "MarketingCampaigns",
  "Columns":  [

  ],
  "Title":  "MyTitle",
  "Subject":  "MySubject"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-SendGrid -Connection $conn -Table MarketingCampaigns -Where "Id = '17693'" | Remove-SendGrid

Modifying Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into SendGrid, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyMarketingCampaignsUpdates.csv | %{
  $record = Select-SendGrid -Connection $conn -Table MarketingCampaigns -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-SendGrid -Connection $conn -Table MarketingCampaigns -Columns @("Title","Subject") -Values @($_.Title, $_.Subject) -Where "Id  = `'$_.Id`'"
  }else{
    Add-SendGrid -Connection $conn -Table MarketingCampaigns -Columns @("Title","Subject") -Values @($_.Title, $_.Subject)
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060