Cmdlets for DigitalOcean

Build 20.0.7587

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 DigitalOcean 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 DigitalOceanCmdlets

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

Import-Module DigitalOceanCmdlets;

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

$conn = Connect-DigitalOcean -OAuthClientId 'abc123' -OAuthClientSecret 'def456' -CallbackURL 'http://localhost:33333'

Authenticating to DigitalOcean

DigitalOcean uses OAuth 2.0 authentication.

Authenticate via OAuth Authentication

You can connect without setting any connection properties for your user credentials.

When you connect the cmdlet opens the DigitalOcean OAuth endpoint in your default browser. Log in and grant permissions to the cmdlet. The cmdlet then completes the OAuth process.

See Using OAuth Authentication for other OAuth authentication flows.

Retrieving Data

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

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

You will notice that we piped the results from Select-DigitalOcean 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-DigitalOcean -OAuthClientId 'abc123' -OAuthClientSecret 'def456' -CallbackURL 'http://localhost:33333'
PS C:\> $row = Select-DigitalOcean -Connection $conn -Table "Droplets" -Columns (Id, Name) -Where "Industry = 'Floppy Disks'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Droplets",
  "Columns":  [

  ],
  "Id":  "MyId",
  "Name":  "MyName"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-DigitalOcean -Connection $conn -Table Droplets -Where "Industry = 'Floppy Disks'" | Remove-DigitalOcean

Updating Data

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

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

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