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 FreshDesk 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 FreshDeskCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module FreshDeskCmdlets;
You can then use the Connect-FreshDesk cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-FreshDesk -Domain "MyDomain" -APIKey "myAPIKey"
Connecting to Freshdesk
Freshdesk makes use of basic authentication. To connect to data, set the following connection properties:
- Domain: Set this to the domain associated with your Freshdesk account. For example, in your URL, https://{domainValue}.freshdesk.com, thus the connection string should be:
Domain=domainValue
- APIKey: Set this to the API key associated with your Freshdesk account. To retrieve your API key, log in to your support Portal. Click the profile picture in the upper-right corner and select the profile settings page. The API key is available below the change password section to the right.
Retrieving Data
The Select-FreshDesk cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-FreshDesk -Connection $conn -Table "Tickets" -Columns @("Id, Name") -Where "Status='2'"The Invoke-FreshDesk 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-FreshDesk -Connection $conn -Table Tickets -Where "Status = '2'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myTicketsData.csv -NoTypeInformation
You will notice that we piped the results from Select-FreshDesk 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-FreshDesk -Domain "MyDomain" -APIKey "myAPIKey" PS C:\> $row = Select-FreshDesk -Connection $conn -Table "Tickets" -Columns (Id, Name) -Where "Status = '2'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Tickets", "Columns": [ ], "Id": "MyId", "Name": "MyName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-FreshDesk -Connection $conn -Table Tickets -Where "Status = '2'" | Remove-FreshDesk
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Freshdesk, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyTicketsUpdates.csv | %{ $record = Select-FreshDesk -Connection $conn -Table Tickets -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-FreshDesk -Connection $conn -Table Tickets -Columns @("Id","Name") -Values @($_.Id, $_.Name) -Where "Id = `'$_.Id`'" }else{ Add-FreshDesk -Connection $conn -Table Tickets -Columns @("Id","Name") -Values @($_.Id, $_.Name) } }