Automate MarkLogic Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

MarkLogic ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with MarkLogic.



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

The CData Cmdlets for MarkLogic 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 MarkLogic.

Cmdlets or ADO.NET?

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

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

Set User, Password, and Server to the credentials for the MarkLogic account and the address of the server you want to connect to. You should also specify the REST API Port if you want to use a specific instance of a REST Server.

PowerShell

  1. Install the module:

    Install-Module MarkLogicCmdlets
  2. Connect:

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

    $id = "1" $customer = Select-MarkLogic -Connection $marklogic -Table "Customer" -Where "Id = `'$Id`'" $customer

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

    $customer = Invoke-MarkLogic -Connection $marklogic -Query 'SELECT * FROM Customer WHERE Id = @Id' -Params @{'@Id'='1'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.MarkLogic.MarkLogicConnection("User='myusername';Password='mypassword';Server='http://marklogic';") $conn.Open()
  3. Instantiate the MarkLogicDataAdapter, execute an SQL query, and output the results:

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

Update MarkLogic Data

PowerShell

Update-MarkLogic -Connection $MarkLogic -Columns @('Name','TotalDue') -Values @('MyName', 'MyTotalDue') -Table Customer -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.MarkLogic.MarkLogicCommand("UPDATE Customer SET Id='1' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.MarkLogic.MarkLogicParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert MarkLogic Data

PowerShell

Add-MarkLogic -Connection $MarkLogic -Table Customer -Columns @("Name", "TotalDue") -Values @("MyName", "MyTotalDue")

ADO.NET

$cmd = New-Object System.Data.CData.MarkLogic.MarkLogicCommand("INSERT INTO Customer (Id) VALUES (@myId)", $conn) $cmd.Parameters.Add(new System.Data.CData.MarkLogic.MarkLogicParameter("@myId","1")) $cmd.ExecuteNonQuery()

Delete MarkLogic Data

PowerShell

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

ADO.NET

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