Automate Couchbase Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Couchbase ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Couchbase NoSQL databases.



Are you looking for a quick and easy way to access Couchbase data from PowerShell? We show how to use the Cmdlets for Couchbase and the CData ADO.NET Provider for Couchbase to connect to Couchbase data and synchronize, automate, download, and more.

The CData Cmdlets for Couchbase are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Couchbase.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Couchbase API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Couchbase data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Couchbase. To access Couchbase data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Couchbase.

After obtaining the needed connection properties, accessing Couchbase data in PowerShell consists of three basic steps.

To connect using the Login method, set User, Password, and Server to the credentials for the account and the address of the server you want to connect to.

PowerShell

  1. Install the module:

    Install-Module CouchbaseCmdlets
  2. Connect:

    $couchbase = Connect-Couchbase -User "$User" -Password "$Password" -Server "$Server"
  3. Search for and retrieve data:

    $firstname = "Bob" $customer = Select-Couchbase -Connection $couchbase -Table "Customer" -Where "FirstName = `'$FirstName`'" $customer

    You can also use the Invoke-Couchbase cmdlet to execute SQL commands:

    $customer = Invoke-Couchbase -Connection $couchbase -Query 'SELECT * FROM Customer WHERE FirstName = @FirstName' -Params @{'@FirstName'='Bob'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Couchbase\lib\System.Data.CData.Couchbase.dll")
  2. Connect to Couchbase:

    $conn= New-Object System.Data.CData.Couchbase.CouchbaseConnection("User=myuseraccount;Password=mypassword;Server=http://mycouchbaseserver;") $conn.Open()
  3. Instantiate the CouchbaseDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT FirstName, TotalDue from Customer" $da= New-Object System.Data.CData.Couchbase.CouchbaseDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.firstname $_.totaldue }

Update Couchbase Data

PowerShell

Update-Couchbase -Connection $Couchbase -Columns @('FirstName','TotalDue') -Values @('MyFirstName', 'MyTotalDue') -Table Customer -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Couchbase.CouchbaseCommand("UPDATE Customer SET FirstName='Bob' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Couchbase.CouchbaseParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Couchbase Data

PowerShell

Add-Couchbase -Connection $Couchbase -Table Customer -Columns @("FirstName", "TotalDue") -Values @("MyFirstName", "MyTotalDue")

ADO.NET

$cmd = New-Object System.Data.CData.Couchbase.CouchbaseCommand("INSERT INTO Customer (FirstName) VALUES (@myFirstName)", $conn) $cmd.Parameters.Add(new System.Data.CData.Couchbase.CouchbaseParameter("@myFirstName","Bob")) $cmd.ExecuteNonQuery()

Delete Couchbase Data

PowerShell

Remove-Couchbase -Connection $Couchbase -Table "Customer" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Couchbase.CouchbaseCommand("DELETE FROM Customer WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Couchbase.CouchbaseParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()