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 ADP Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Connecting to ADP
Before configuring your connection, ensure you have obtained and formatted your SSL client certificate as described in Before You Connect.
To connect to ADP, set the following properties:
- OAuthClientId: The client Id of the custom OAuth application you obtained from ADP.
- OAuthClientSecret: The custom OAuth application's client secret.
- SSLClientCert: The full file path to your SSL client certificate. If using a .pem file, ensure it contains both the Private Key and Public Certificate. If using a .pfx file, ensure it was created with the correct export password. See Before You Connect for more information.
- SSLClientCertPassword: The password for the SSL client certificate, if applicable.
- UseUAT: The cmdlet makes requests to the production environment by default. If using a developer account, set UseUAT = true.
- RowScanDepth: The maximum number of rows to scan for the custom fields columns available in the table (default=100). Setting a high value may decrease performance.
Creating a Connection Object
You can then use the Connect-ADP cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-ADP -InitiateoAuth "GETANDREFRESH" -OAuthClientId "YourClientId" -OAuthClientSecret "YourClientSecret" -SSLClientCert "c:\\cert.pfx" -SSLClientCertPassword="admin@123"
Retrieving Data
The Select-ADP cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-ADP -Connection $conn -Table "Workers" -Columns @("AssociateOID, WorkerID") -Where "AssociateOID='G3349PZGBADQY8H8'"
The Invoke-ADP 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-ADP -Connection $conn -Table Workers -Where "AssociateOID = 'G3349PZGBADQY8H8'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myWorkersData.csv -NoTypeInformation
You will notice that we piped the results from Select-ADP 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-ADP -InitiateoAuth "GETANDREFRESH" -OAuthClientId "YourClientId" -OAuthClientSecret "YourClientSecret" -SSLClientCert "c:\\cert.pfx" -SSLClientCertPassword="admin@123"
PS C:\> $row = Select-ADP -Connection $conn -Table "Workers" -Columns (AssociateOID, WorkerID) -Where "AssociateOID = 'G3349PZGBADQY8H8'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
"Connection": {
},
"Table": "Workers",
"Columns": [
],
"AssociateOID": "MyAssociateOID",
"WorkerID": "MyWorkerID"
}
Deleting Data
The following line deletes any records that match the criteria:
Select-ADP -Connection $conn -Table Workers -Where "AssociateOID = 'G3349PZGBADQY8H8'" | Remove-ADP
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into ADP, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyWorkersUpdates.csv | %{
$record = Select-ADP -Connection $conn -Table Workers -Where ("Id = `'"+$_.Id+"`'")
if($record){
Update-ADP -Connection $conn -Table Workers -Columns @("AssociateOID","WorkerID") -Values @($_.AssociateOID, $_.WorkerID) -Where "Id = `'$_.Id`'"
}else{
Add-ADP -Connection $conn -Table Workers -Columns @("AssociateOID","WorkerID") -Values @($_.AssociateOID, $_.WorkerID)
}
}