REQUEST A DEMO

More fun with Powershell

powershell[4][1]I was recently working on porting some existing ClearBasic batch (cbbatch) scripts to PowerShell.

I’ve blogged in the past about using our SDK with Powershell.

For this particular project, I had multiple scripts to port, and I was looking to extract some common functions and utilities, so that they could be used between multiple scripts.

Configuration

When using the Dovetail SDK, one of the first tasks is to load up the configuration information and connect to the database.

That would normally look something like:

$connectionString = “Data Source=localhost;Initial Catalog=dovetail;uid=user;pwd=pass"
$databaseType = “MSSQL”;    

[system.reflection.assembly]::LoadWithPartialName("fcsdk") > $null;
[system.reflection.assembly]::LoadWithPartialName("FChoice.Toolkits.Clarify") > $null;

$config = new-object -typename System.Collections.Specialized.NameValueCollection;
$config.Add("fchoice.connectionstring",$connectionString);
$config.Add("fchoice.dbtype",$databaseType);
$config.Add("fchoice.disableloginfromfcapp", "false");
$config.Add("fchoice.nocachefile", "true");

#Create and initialize the clarifyApplication object
$ClarifyApplication = [Fchoice.Foundation.Clarify.ClarifyApplication];
if ($ClarifyApplication::IsInitialized -eq $false ){
   $ClarifyApplication::initialize($config) > $null;
}

Code for re-use

I didn’t want to repeat that in multiple scripts, so we can place that inside a function:

function create-clarify-application
{
  $connectionString = “Data Source=localhost;Initial Catalog=dovetail;uid=user;pwd=pass"
  $databaseType = “MSSQL”;    
  <snip> remainder of code here</snip>
}

I added that function to a script named DovetailCommonFunctions.ps1

So now any script can use those functions by simply dot sourcing this script, such as:

. .DovetailCommonFunctions.ps1

Notice that the line above is a dot, followed by a space, then the path to the script. If the script is in the same directory, then you would use dot slash, as shown. 

Config files

But I didn’t want to have that configuration information hardcoded within that function.

Keith Hill has a nice blog post where he talks about using a configuration file for for Powershell scripts.

I created a config file named dovetail.config:

<?xml version="1.0"?>
<configuration>
  <startup>
  </startup>
  <appSettings>
    <add key="connectionString" value="Data Source=localhost;Initial Catalog=dovetail;uid=user;pwd=pass" />
    <add key="databaseType" value="MSSQL" />
  </appSettings>
</configuration>

Then, using Keith’s LoadConfig script, my create-clarify-application function becomes:

function create-clarify-application
{
    #Load the configuration file
    .LoadConfig dovetail.config

    $connectionString = $appSettings["connectionString"];
    $databaseType = $appSettings["databaseType"];      
  
    $config = new-object -typename System.Collections.Specialized.NameValueCollection;
    $config.Add("fchoice.connectionstring",$connectionString);
    $config.Add("fchoice.dbtype",$databaseType);

  <snip> remainder of code here</snip>
}

So now I have one config file for setting application settings, including database connection information.

More code for re-use

Within DovetailCommonFunctions.ps1, I added another function for creating a clarify session:

function create-clarify-session
{
    $clarifyapp = $args[0]
    $ClarifySession = $clarifyapp::Instance.CreateSession();
    return $ClarifySession
}

Now we can finally get to the task at hand.

Dispatching a Solution

I needed to create a script that would dispatch a solution to a given queue.

My script is now pretty simple:

param([string] $solutionId, [string] $queueName);

. .DovetailCommonFunctions.ps1

$ClarifyApplication = create-clarify-application;
$ClarifySession = create-clarify-session $ClarifyApplication;

$interToolkit= new-object FChoice.Toolkits.Clarify.Interfaces.InterfacesToolkit( $ClarifySession );
$DispatchSolutionSetup = new-object FChoice.Toolkits.Clarify.Interfaces.DispatchSolutionSetup($solutionId, $queueName);
$result = $interToolkit.DispatchSolution($DispatchSolutionSetup);

Summary

So now I have the start of a common library of Dovetail functions (create-clarify-application, create-clarify-session), as well as a way to use a config file for application configuration information, including database connections.

The more I use Powershell, the more I like it. I’ll continue to share my learnings as I progress.