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 Reckon 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 ReckonCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module ReckonCmdlets;
You can then use the Connect-Reckon cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Reckon
Connecting to Reckon
The cmdlet makes requests to Reckon through the CData QuickBooks Desktop Gateway. It runs on the same machine as Reckon and accepts connections through a lightweight, embedded Web server. The server supports SSL/TLS, enabling users to connect securely from remote machines. The first time you connect, you will need to authorize the cmdlet with Reckon. For more information, refer to our Using the QuickBooks Gateway guide.
Retrieving Data
The Select-Reckon cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Reckon -Connection $conn -Table "Customers" -Columns @("Id, Name") -Where "Name='Cook, Brian'"The Invoke-Reckon 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-Reckon -Connection $conn -Table Customers -Where "Name = 'Cook, Brian'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myCustomersData.csv -NoTypeInformation
You will notice that we piped the results from Select-Reckon 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-Reckon PS C:\> $row = Select-Reckon -Connection $conn -Table "Customers" -Columns (Id, Name) -Where "Name = 'Cook, Brian'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Customers", "Columns": [ ], "Id": "MyId", "Name": "MyName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Reckon -Connection $conn -Table Customers -Where "Name = 'Cook, Brian'" | Remove-Reckon
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Reckon, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyCustomersUpdates.csv | %{ $record = Select-Reckon -Connection $conn -Table Customers -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-Reckon -Connection $conn -Table Customers -Columns @("Id","Name") -Values @($_.Id, $_.Name) -Where "Id = `'$_.Id`'" }else{ Add-Reckon -Connection $conn -Table Customers -Columns @("Id","Name") -Values @($_.Id, $_.Name) } }