In-process PowerShell adapter for Coral8

A few days ago a thought crept into my head; wouldn’t it be nice if I could have a Coral8 in-process adapter to run PowerShell code? If it worked it could potentially be a “universal” adapter, limiting the need for custom one-off adapters.

Coral8, out-of-the-box, supports two different types of adapters. In-process adapters which must be written in C (enough said?), and out-of-process adapters which can be written in the language of your choice, but then have startup sequence, configuration, and deployment work associated with them.

Using the PoShAdapter I’ve been developing, let’s take a look at the adapter as a message sink. In this case a  “sent to Gmail” adapter.

CREATE SCHEMA TSchema ( toAddr STRING , subject string, message as string); 
CREATE OUTPUT STREAM outGmail SCHEMA TSchema; 
ATTACH OUTPUT ADAPTER PoShAdapter TYPE PoShAdapter  TO STREAM outGmail  PROPERTIES          
BEGINBLOCK  = [[   
    Add-Type -AssemblyName System.Security     
    Add-Type -Path "C:\Program Files\Coral8\Server\bin\GmailHelper.dll"   
    $encPassword = "AQQbRXQr…kg=="   
    $password =[Text.Encoding]::UTF8.GetString([Security.Cryptography.ProtectedData]::UnProtect([System.Convert]::FromBase64String($encPassword),$null,"CurrentUser"))
]],
PROCESS     = [[             
    foreach ($t in $input)             
    {                  
        [RC.Gmail.GmailMessage]::SendFromGmail("from.user", $password,
                       $t["toAddr"], $t["subject"], $t[“message”])
    }
    ]];

The posh adapter, like PowerShell scripts and functions have begin, process, and end blocks.

In the beginblock we load the assemblies for System.Security and GmailHelper, as well as decrypt the gmail password.

The process block will have as its $input set to a collection of the Coral8 tuples, so it simply loops through them sends out an email for each one. Because we the Runspace is kept for the lifetime of the adapter, context from the beginblock is available in the process block.

In later posts I’ll explore using the PoShAdapter to transform data (as in a select or map operator), and look into the possibility of using the adapter to listen for WMI or other system events.

No Comments