Ready to get started?

Download a free trial of the Paylocity Cmdlets to get started:

 Download Now

Learn more:

Paylocity Icon Paylocity Data Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to Paylocity. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

PowerShell Scripting to Replicate Paylocity Data to MySQL



Write a simple PowerShell script to replicate Paylocity data to a MySQL database.

The CData Cmdlets for Paylocity offer live access to Paylocity data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for Paylocity and the CData Cmdlets for MySQL in PowerShell to replicate Paylocity data to a MySQL database.

After obtaining the needed connection properties, accessing Paylocity data in PowerShell and preparing for replication consists of four basic steps.

Set the following to establish a connection to Paylocity:

  • RSAPublicKey: Set this to the RSA Key associated with your Paylocity, if the RSA Encryption is enabled in the Paylocity account.

    This property is required for executing Insert and Update statements, and it is not required if the feature is disabled.

  • UseSandbox: Set to true if you are using sandbox account.
  • CustomFieldsCategory: Set this to the Customfields category. This is required when IncludeCustomFields is set to true. The default value for this property is PayrollAndHR.
  • Key: The AES symmetric key(base 64 encoded) encrypted with the Paylocity Public Key. It is the key used to encrypt the content.

    Paylocity will decrypt the AES key using RSA decryption.
    It is an optional property if the IV value not provided, The driver will generate a key internally.

  • IV: The AES IV (base 64 encoded) used when encrypting the content. It is an optional property if the Key value not provided, The driver will generate an IV internally.

Connect Using OAuth Authentication

You must use OAuth to authenticate with Paylocity. OAuth requires the authenticating user to interact with Paylocity using the browser. For more information, refer to the OAuth section in the Help documentation.

The Pay Entry API

The Pay Entry API is completely separate from the rest of the Paylocity API. It uses a separate Client ID and Secret, and must be explicitly requested from Paylocity for access to be granted for an account. The Pay Entry API allows you to automatically submit payroll information for individual employees, and little else. Due to the extremely limited nature of what is offered by the Pay Entry API, we have elected not to give it a separate schema, but it may be enabled via the UsePayEntryAPI connection property.

Please be aware that when setting UsePayEntryAPI to true, you may only use the CreatePayEntryImportBatch & MergePayEntryImportBatchgtable stored procedures, the InputTimeEntry table, and the OAuth stored procedures. Attempts to use other features of the product will result in an error. You must also store your OAuthAccessToken separately, which often means setting a different OAuthSettingsLocation when using this connection property.

Collecting Paylocity Data

  1. Install the module:

    Install-Module PaylocityCmdlets
  2. Connect to Paylocity:

    $paylocity = Connect-Paylocity -OAuthClientID $OAuthClientID -OAuthClientSecret $OAuthClientSecret -RSAPublicKey $RSAPublicKey -Key $Key -IV $IV
  3. Retrieve the data from a specific resource:

    $data = Select-Paylocity -Connection $paylocity -Table "Employee"

    You can also use the Invoke-Paylocity cmdlet to execute pure SQL-92 statements:

    $data = Invoke-Paylocity -Connection $paylocity -Query 'SELECT * FROM Employee WHERE EmployeeId = @EmployeeId' -Params @{'@EmployeeId'='1234'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting Paylocity Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the Paylocity data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the Paylocity resource (Employee) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Employee" -Columns $columns -Values $values }

You have now replicated your Paylocity data to a MySQL database. This gives you freedom to work with Paylocity data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to Paylocity and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-Paylocity -Connection $paylocity -Table "Employee" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Employee" -Columns $columns -Values $values }
  • If you wish to replicate the Paylocity data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-Paylocity cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}