![]() |
VOOZH | about |
The CData Cmdlets for HubDB 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 HubDB.
The Cmdlets are not only a PowerShell interface to HubDB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete HubDB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for HubDB. To access HubDB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for HubDB.
Once you have acquired the necessary connection properties, accessing HubDB data in PowerShell can be enabled in three steps.
There are two authentication methods available for connecting to HubDB data source: OAuth Authentication with a public HubSpot application and authentication with a Private application token.
AuthScheme must be set to "OAuth" in all OAuth flows. Be sure to review the Help documentation for the required connection properties for you specific authentication needs (desktop applications, web applications, and headless machines).
Follow the steps below to register an application and obtain the OAuth client credentials:
Under Scopes, select any scopes you need for your application's intended functionality.
A minimum of the following scopes is required to access tables:
To connect using a HubSpot private application token, set the AuthScheme property to "PrivateApp."
You can generate a private application token by following the steps below:
To connect, set PrivateAppToken to the private application token you retrieved.
Install the module:
Install-Module HubDBCmdlets
Connect:
$hubdb = Connect-HubDB -AuthScheme "$AuthScheme" -OAuthClientID "$OAuthClientID" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$id = "1" $northwindproducts = Select-HubDB -Connection $hubdb -Table "NorthwindProducts" -Where "Id = `'$Id`'" $northwindproducts
You can also use the Invoke-HubDB cmdlet to execute SQL commands:
$northwindproducts = Invoke-HubDB -Connection $hubdb -Query 'SELECT * FROM NorthwindProducts WHERE Id = @Id' -Params @{'@Id'='1'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for HubDB\lib\System.Data.CData.HubDB.dll")
Connect to HubDB:
$conn= New-Object System.Data.CData.HubDB.HubDBConnection("AuthScheme=OAuth;OAuthClientID=MyOAuthClientID;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the HubDBDataAdapter, execute an SQL query, and output the results:
$sql="SELECT PartitionKey, Name from NorthwindProducts"
$da= New-Object System.Data.CData.HubDB.HubDBDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.partitionkey $_.name
}
Update-HubDB -Connection $HubDB -Columns @('PartitionKey','Name') -Values @('MyPartitionKey', 'MyName') -Table NorthwindProducts -Id "MyId"
$cmd = New-Object System.Data.CData.HubDB.HubDBCommand("UPDATE NorthwindProducts SET Id='1' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.HubDB.HubDBParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-HubDB -Connection $HubDB -Table NorthwindProducts -Columns @("PartitionKey", "Name") -Values @("MyPartitionKey", "MyName")
$cmd = New-Object System.Data.CData.HubDB.HubDBCommand("INSERT INTO NorthwindProducts (Id) VALUES (@myId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.HubDB.HubDBParameter("@myId","1")))
$cmd.ExecuteNonQuery()
Remove-HubDB -Connection $HubDB -Table "NorthwindProducts" -Id "MyId"
$cmd = New-Object System.Data.CData.HubDB.HubDBCommand("DELETE FROM NorthwindProducts WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.HubDB.HubDBParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the HubDB Data Provider to get started:
Download NowLearn more:
👁 HubDB IconRapidly create and deploy powerful .NET applications that integrate with HubDB.