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 Avalara 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 AvalaraCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module AvalaraCmdlets;
You can then use the Connect-Avalara cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Avalara -User "MYUser" -Password "myPassword"
Connecting to Avalara
By default, the cmdlet connects to a production environment. Set UseSandbox to true to configure the cmdlet to connect to a Avalara sandbox environment.
Authenticating to Avalara
There are two authentication methods available for connecting to the Avalara data source: Basic and LicenseKey.
Login Credentials
To use login credentials for authentication, set the following:
- AuthScheme: Set this to Basic.
- User: Set this to your Avalara username.
- Password: Set this to your Avalara password.
Account Number and License Key
Alternatively, you can authenticate using your account number and license key. Connect to the data source using the following:
- AuthScheme: Set this to LicenseKey.
- AccountId: Set this to your Avalara Account Id. The Account Id is listed in the upper right-hand corner of the Avalara admin console.
- LicenseKey: Set this to your Avalara license key. You can generate a license key by logging into Avalara as an account adminstrator and navigating to Settings > Reset License Key.
Retrieving Data
The Select-Avalara cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Avalara -Connection $conn -Table "Transactions" -Columns @("Id, TotalTax") -Where "Code='051349'"The Invoke-Avalara 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-Avalara -Connection $conn -Table Transactions -Where "Code = '051349'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myTransactionsData.csv -NoTypeInformation
You will notice that we piped the results from Select-Avalara 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-Avalara -User "MYUser" -Password "myPassword" PS C:\> $row = Select-Avalara -Connection $conn -Table "Transactions" -Columns (Id, TotalTax) -Where "Code = '051349'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Transactions", "Columns": [ ], "Id": "MyId", "TotalTax": "MyTotalTax" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Avalara -Connection $conn -Table Transactions -Where "Code = '051349'" | Remove-Avalara
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Avalara, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyTransactionsUpdates.csv | %{ $record = Select-Avalara -Connection $conn -Table Transactions -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-Avalara -Connection $conn -Table Transactions -Columns @("Id","TotalTax") -Values @($_.Id, $_.TotalTax) -Where "Id = `'$_.Id`'" }else{ Add-Avalara -Connection $conn -Table Transactions -Columns @("Id","TotalTax") -Values @($_.Id, $_.TotalTax) } }