Ready to get started?

Learn more about PowerShell Connectivity Solutions

Learn More

Create a Data Access Object for PowerShell Scripts using JDBI

A brief overview of creating a SQL Object API for PowerShell scripts in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for PowerShell integrates connectivity to live PowerShell scripts in Java applications. By pairing these technologies, you gain simple, programmatic access to PowerShell scripts. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write PowerShell scripts.

Create a DAO for the PowerShell Process Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyProcessDAO { //insert new data into PowerShell @SqlUpdate("INSERT INTO Process (ProcessName, CPU) values (:processName, :cPU)") void insert(@Bind("processName") String processName, @Bind("cPU") String cPU); //request specific data from PowerShell (String type is used for simplicity) @SqlQuery("SELECT CPU FROM Process WHERE ProcessName = :processName") String findCPUByProcessName(@Bind("processName") String processName); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to PowerShell

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to PowerShell.

The ScriptLocation, under the Data section, must be set to a valid script location.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the PowerShell JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.powershell.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for PowerShell will typically look like the following:

jdbc:powershell:ScriptLocation='%Public%\Documents\CData PowerShell Scripts';ExecuteQuery=True;

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:powershell:ScriptLocation='%Public%\Documents\CData PowerShell Scripts';ExecuteQuery=True;"); MyProcessDAO dao =; //do stuff with the DAO dao.close();

Read PowerShell Scripts

With the connection open to PowerShell, simply call the previously defined method to retrieve data from the Process entity in PowerShell.

//disply the result of our 'find' method String cPU = dao.findCPUByProcessName("RemoteConnectorService"); System.out.println(cPU);

Write PowerShell Scripts

It is also simple to write data to PowerShell, using the previously defined method.

//add a new entry to the Process entity dao.insert(newProcessName, newCPU);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for PowerShell by integrating with the CData JDBC Driver for PowerShell. Download a free trial and work with live PowerShell scripts in custom Java applications today.