![]() |
VOOZH | about |
The CData Cmdlets for SendGrid 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 SendGrid.
The Cmdlets are not only a PowerShell interface to SendGrid, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete SendGrid data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for SendGrid. To access SendGrid data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for SendGrid.
Once you have acquired the necessary connection properties, accessing SendGrid data in PowerShell can be enabled in three steps.
To make use of all the available features, provide the User and Password connection properties.
To connect with limited features, you can set the APIKey connection property instead. See the "Getting Started" chapter of the help documentation for a guide to obtaining the API key.
Install the module:
Install-Module SendGridCmdlets
Connect:
$sendgrid = Connect-SendGrid -User "$User" -Password "$Password"
Search for and retrieve data:
$type = "Device" $advancedstats = Select-SendGrid -Connection $sendgrid -Table "AdvancedStats" -Where "Type = `'$Type`'" $advancedstats
You can also use the Invoke-SendGrid cmdlet to execute SQL commands:
$advancedstats = Invoke-SendGrid -Connection $sendgrid -Query 'SELECT * FROM AdvancedStats WHERE Type = @Type' -Params @{'@Type'='Device'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SendGrid\lib\System.Data.CData.SendGrid.dll")
Connect to SendGrid:
$conn= New-Object System.Data.CData.SendGrid.SendGridConnection("User=admin;Password=abc123;")
$conn.Open()
Instantiate the SendGridDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, Clicks from AdvancedStats"
$da= New-Object System.Data.CData.SendGrid.SendGridDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.name $_.clicks
}
Update-SendGrid -Connection $SendGrid -Columns @('Name','Clicks') -Values @('MyName', 'MyClicks') -Table AdvancedStats -Id "MyId"
$cmd = New-Object System.Data.CData.SendGrid.SendGridCommand("UPDATE AdvancedStats SET Type='Device' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SendGrid.SendGridParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-SendGrid -Connection $SendGrid -Table AdvancedStats -Columns @("Name", "Clicks") -Values @("MyName", "MyClicks")
$cmd = New-Object System.Data.CData.SendGrid.SendGridCommand("INSERT INTO AdvancedStats (Type) VALUES (@myType)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SendGrid.SendGridParameter("@myType","Device")))
$cmd.ExecuteNonQuery()
Remove-SendGrid -Connection $SendGrid -Table "AdvancedStats" -Id "MyId"
$cmd = New-Object System.Data.CData.SendGrid.SendGridCommand("DELETE FROM AdvancedStats WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SendGrid.SendGridParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the SendGrid Data Provider to get started:
Download NowLearn more:
👁 SendGrid IconRapidly create and deploy powerful .NET applications that integrate with SendGrid account data including Lists, Recipients, Schedules, Segments, and more!