Automate FreshBooks Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

FreshBooks ADO.NET Provider

Complete read-write access to FreshBooks enables developers to search (Customers, Transactions, Invoices, Sales Receipts, etc.), update items, edit customers, and more, from any .NET application.



Are you looking for a quick and easy way to access FreshBooks data from PowerShell? We show how to use the Cmdlets for FreshBooks and the CData ADO.NET Provider for FreshBooks to connect to FreshBooks data and synchronize, automate, download, and more.

The CData Cmdlets for FreshBooks 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 FreshBooks.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the FreshBooks API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete FreshBooks data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for FreshBooks. To access FreshBooks data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for FreshBooks.

After obtaining the needed connection properties, accessing FreshBooks data in PowerShell consists of three basic steps.

To connect to FreshBooks, you can set the CompanyName and Token connection properties. Alternatively, you can use the OAuth authentication standard.

OAuth can be used to enable other users to access their own company data. To authenticate using OAuth, you will need to obtain the OAuthClientId and OAuthClientSecret by registering an app. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.

PowerShell

  1. Install the module:

    Install-Module FreshBooksCmdlets
  2. Connect:

    $freshbooks = Connect-FreshBooks -CompanyName "$CompanyName" -Token "$Token"
  3. Search for and retrieve data:

    $email = "Captain Hook" $clients = Select-FreshBooks -Connection $freshbooks -Table "Clients" -Where "Email = `'$Email`'" $clients

    You can also use the Invoke-FreshBooks cmdlet to execute SQL commands:

    $clients = Invoke-FreshBooks -Connection $freshbooks -Query 'SELECT * FROM Clients WHERE Email = @Email' -Params @{'@Email'='Captain Hook'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for FreshBooks\lib\System.Data.CData.FreshBooks.dll")
  2. Connect to FreshBooks:

    $conn= New-Object System.Data.CData.FreshBooks.FreshBooksConnection("CompanyName=CData;Token=token;") $conn.Open()
  3. Instantiate the FreshBooksDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Username, Credit from Clients" $da= New-Object System.Data.CData.FreshBooks.FreshBooksDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.username $_.credit }

Update FreshBooks Data

PowerShell

Update-FreshBooks -Connection $FreshBooks -Columns @('Username','Credit') -Values @('MyUsername', 'MyCredit') -Table Clients -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.FreshBooks.FreshBooksCommand("UPDATE Clients SET Email='Captain Hook' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.FreshBooks.FreshBooksParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert FreshBooks Data

PowerShell

Add-FreshBooks -Connection $FreshBooks -Table Clients -Columns @("Username", "Credit") -Values @("MyUsername", "MyCredit")

ADO.NET

$cmd = New-Object System.Data.CData.FreshBooks.FreshBooksCommand("INSERT INTO Clients (Email) VALUES (@myEmail)", $conn) $cmd.Parameters.Add(new System.Data.CData.FreshBooks.FreshBooksParameter("@myEmail","Captain Hook")) $cmd.ExecuteNonQuery()

Delete FreshBooks Data

PowerShell

Remove-FreshBooks -Connection $FreshBooks -Table "Clients" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.FreshBooks.FreshBooksCommand("DELETE FROM Clients WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.FreshBooks.FreshBooksParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()