Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →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 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 Provider?
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
-
Install the module:
Install-Module FreshBooksCmdlets
-
Connect:
$freshbooks = Connect-FreshBooks -CompanyName "$CompanyName" -Token "$Token"
-
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
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for FreshBooks\lib\System.Data.CData.FreshBooks.dll")
-
Connect to FreshBooks:
$conn= New-Object System.Data.CData.FreshBooks.FreshBooksConnection("CompanyName=CData;Token=token;") $conn.Open()
-
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-Object 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-Object 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-Object System.Data.CData.FreshBooks.FreshBooksParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject