Cmdlets for AWS Data Management

Build 20.0.7654

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 AWSDataManagement 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 AWSDataManagementCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module AWSDataManagementCmdlets;

You can then use the Connect-AWSDataManagement cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-AWSDataManagement -AWSAccessKey 'myAccessKey' -Account 'myAccountName'

Obtaining the Access Key

To obtain the credentials for an IAM user, follow the steps below:

  1. Sign into the IAM console.
  2. In the navigation pane, select Users.
  3. To create or manage the access keys for a user, select the user and then select the Security Credentials tab.

To obtain the credentials for your AWS root account, follow the steps below:

  1. Sign into the AWS Management console with the credentials for your root account.
  2. Select your account name or number and select My Security Credentials in the menu that is displayed.
  3. Click Continue to Security Credentials and expand the Access Keys section to manage or create root account access keys.

Authenticating with Root Credentials

To authenticate using account root credentials, set the following:

  • AuthScheme: Set this to AwsRootKeys.
  • AWSAccessKey: The access key associated with the AWS root account.
  • AWSSecretKey: The secret key associated with the AWS root account.

Note: Use of this authentication scheme is discouraged by Amazon for anything but simple tests. The account root credentials have the full permissions of the user, making this the least secure authentication method.

Authenticating with Temporary Credentials

To authenticate using temporary credentials, specify the following:

  • AuthScheme: Set this to TemporaryCredentials.
  • AWSAccessKey: The access key of the IAM user to assume the role for.
  • AWSSecretKey: The secret key of the IAM user to assume the role for.
  • AWSSessionToken: Your AWS session token. This will have been provided alongside your temporary credentials. See this link for more info.

The cmdlet can now request resources using the same permissions provided by long-term credentials (such as IAM user credentials) for the lifespan of the temporary credentials.

If you are also using an IAM role to authenticate, you must additionally specify the following:

  • AWSRoleARN: Specify the Role ARN for the role you'd like to authenticate with. This will cause the cmdlet to attempt to retrieve credentials for the specified role.
  • AWSExternalId: Only if required when you assume a role in another account.

Authenticating as an AWS Role

In many situations it may be preferable to use an IAM role for authentication instead of the direct security credentials of an AWS root user.

To authenticate as an AWS role, set the following:

  • AuthScheme: Set this to AwsIAMRoles.
  • AWSRoleARN: Specify the Role ARN for the role you'd like to authenticate with. This will cause the cmdlet to attempt to retrieve credentials for the specified role.
  • AWSExternalId: Only if required when you assume a role in another account.
If you are connecting to AWS (instead of already being connected such as on an EC2 instance), you must additionally specify the following:
  • AWSAccessKey: The access key of the IAM user to assume the role for.
  • AWSSecretKey: The secret key of the IAM user to assume the role for.

Note: Roles may not be used when specifying the AWSAccessKey and AWSSecretKey of an AWS root user.

Connecting to AWSDataManagement

Once you have configured the cmdlet for your desired authentication method, you can optionally set the following to refine data access:

  • Domain: Set this if you want to use a domain name you have associated with AWS.
  • AWSRegion: Set this to the region where your AWS Data Management data is hosted.
Optionally, you can set Domain if you want to use a domain name you have associated with AWS and the AWSRegion to specify your AWS hosting region.

Retrieving Data

The Select-AWSDataManagement cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-AWSDataManagement -Connection $conn -Table "NorthwindProducts" -Columns @("PartitionKey, Name") -Where "Industry='Floppy Disks'"
The Invoke-AWSDataManagement 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-AWSDataManagement -Connection $conn -Table NorthwindProducts -Where "Industry = 'Floppy Disks'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myNorthwindProductsData.csv -NoTypeInformation

You will notice that we piped the results from Select-AWSDataManagement 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-AWSDataManagement -AWSAccessKey 'myAccessKey' -Account 'myAccountName'
PS C:\> $row = Select-AWSDataManagement -Connection $conn -Table "NorthwindProducts" -Columns (PartitionKey, Name) -Where "Industry = 'Floppy Disks'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "NorthwindProducts",
  "Columns":  [

  ],
  "PartitionKey":  "MyPartitionKey",
  "Name":  "MyName"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-AWSDataManagement -Connection $conn -Table NorthwindProducts -Where "Industry = 'Floppy Disks'" | Remove-AWSDataManagement

Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into AWS Data Management, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyNorthwindProductsUpdates.csv | %{
  $record = Select-AWSDataManagement -Connection $conn -Table NorthwindProducts -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-AWSDataManagement -Connection $conn -Table NorthwindProducts -Columns @("PartitionKey","Name") -Values @($_.PartitionKey, $_.Name) -Where "Id  = `'$_.Id`'"
  }else{
    Add-AWSDataManagement -Connection $conn -Table NorthwindProducts -Columns @("PartitionKey","Name") -Values @($_.PartitionKey, $_.Name)
  }
}

Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 20.0.7654