Ready to get started?

Download a free trial of the FreshBooks Data Provider to get started:

 Download Now

Learn more:

FreshBooks Icon 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.

Automate FreshBooks Integration Tasks from PowerShell



Are you in search of a quick and easy way to access FreshBooks data from PowerShell? This article demonstrates how to utilize the FreshBooks Cmdlets and the CData ADO.NET Provider for FreshBooks for tasks like connecting to FreshBooks data, automating operations, downloading data, 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.

PowerShell Cmdlets or ADO.NET Driver?

The Cmdlets are not only a PowerShell interface to FreshBooks, 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.

Once you have acquired the necessary connection properties, accessing FreshBooks data in PowerShell can be enabled in three 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()