![]() |
VOOZH | about |
The CData Cmdlets for Square 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 Square.
The Cmdlets are not only a PowerShell interface to Square, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Square data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Square. To access Square data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Square.
Once you have acquired the necessary connection properties, accessing Square data in PowerShell can be enabled in three steps.
Square uses the OAuth authentication standard. To authenticate using OAuth, register an app with Square to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.
Additionally, you must specify the LocationId. You can retrieve the Ids for your Locations by querying the Locations table. Alternatively, you can set the LocationId in the search criteria of your query.
Install the module:
Install-Module SquareCmdlets
Connect:
$square = Connect-Square -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -LocationId "$LocationId" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$type = "FULL" $refunds = Select-Square -Connection $square -Table "Refunds" -Where "Type = `'$Type`'" $refunds
You can also use the Invoke-Square cmdlet to execute SQL commands:
$refunds = Invoke-Square -Connection $square -Query 'SELECT * FROM Refunds WHERE Type = @Type' -Params @{'@Type'='FULL'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Square\lib\System.Data.CData.Square.dll")
Connect to Square:
$conn= New-Object System.Data.CData.Square.SquareConnection("OAuthClientId=MyAppId;OAuthClientSecret=MyAppSecret;CallbackURL=http://localhost:33333;LocationId=MyDefaultLocation;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the SquareDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Reason, RefundedMoneyAmount from Refunds"
$da= New-Object System.Data.CData.Square.SquareDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.reason $_.refundedmoneyamount
}
Update-Square -Connection $Square -Columns @('Reason','RefundedMoneyAmount') -Values @('MyReason', 'MyRefundedMoneyAmount') -Table Refunds -Id "MyId"
$cmd = New-Object System.Data.CData.Square.SquareCommand("UPDATE Refunds SET Type='FULL' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Square.SquareParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-Square -Connection $Square -Table Refunds -Columns @("Reason", "RefundedMoneyAmount") -Values @("MyReason", "MyRefundedMoneyAmount")
$cmd = New-Object System.Data.CData.Square.SquareCommand("INSERT INTO Refunds (Type) VALUES (@myType)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Square.SquareParameter("@myType","FULL")))
$cmd.ExecuteNonQuery()
Remove-Square -Connection $Square -Table "Refunds" -Id "MyId"
$cmd = New-Object System.Data.CData.Square.SquareCommand("DELETE FROM Refunds WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Square.SquareParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Square Data Provider to get started:
Download NowLearn more:
👁 Square IconEasy-to-use Square client enables .NET-based applications to easily consume Square Transactions, Items, Subscriptions, etc.