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 QuickBase 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 QuickBaseCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module QuickBaseCmdlets;
You can then use the Connect-QuickBase cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-QuickBase -User "[email protected]" -Password "test_1234" -Domain "test.quickbase.com" -ApplicationToken "bwkxrb5da2wn57bzfh9xn24"
Authenticating to Quickbase
User Authentication
Set the AuthScheme to Basic to authenticate with this method.
To authenthenticate with user credentials, specify the following connection properties:
- Set the Quickbase User and Password.
- If your application requires an ApplicationToken, you must provide it otherwise an error will be thrown. You can find the ApplicationToken under MyAppName > Settings > App properties > Advanced settings > Security options > Require Application Tokens > Manage Application Token.
User Token
Set the AuthScheme to Token to authenticate with this method.
To authenthenticate with a user token, specify the following connection properties:
- Set UserToken and you are ready to connect. You can find the UserToken under Quick Base > My Preferences > My User Information > Manage User Tokens.
Retrieving Data
The Select-QuickBase cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-QuickBase -Connection $conn -Table "[CData].[QuickBase].SampleTable_1" -Columns @("Id, Column1") -Where "Column2='Bob'"The Invoke-QuickBase 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-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Where "Column2 = 'Bob'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[CData].[QuickBase].SampleTable_1Data.csv -NoTypeInformation
You will notice that we piped the results from Select-QuickBase 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-QuickBase -User "[email protected]" -Password "test_1234" -Domain "test.quickbase.com" -ApplicationToken "bwkxrb5da2wn57bzfh9xn24" PS C:\> $row = Select-QuickBase -Connection $conn -Table "[CData].[QuickBase].SampleTable_1" -Columns (Id, Column1) -Where "Column2 = 'Bob'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "[CData].[QuickBase].SampleTable_1", "Columns": [ ], "Id": "MyId", "Column1": "MyColumn1" }
Deleting Data
The following line deletes any records that match the criteria:
Select-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Where "Column2 = 'Bob'" | Remove-QuickBase
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Quickbase, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\My[CData].[QuickBase].SampleTable_1Updates.csv | %{ $record = Select-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1) -Where "Id = `'$_.Id`'" }else{ Add-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1) } }