VMware Cloud Community
StefanSchnell
Enthusiast
Enthusiast

Tip: How to Integrate C# Programming Language Seamlessly

VMware Aria Automation offers a polyglot programming environment. JavaScript, PowerShell and Python are offered as programming languages. The current PowerShell 7.3 container, of release 8.13.1, contains the Common Language Runtime (CLR) 7.0.9 dotNET run-time environment. With this we have the opportunity to expand this range to include additional programming languages. This post shows how the C# programming language can be seamlessly used. In addition an approach is shown, to save the C# code in Aria Automation and to executes it directly.

Here are the necessary actions required for this:

actionsCSharp.jpg

1. testCSharp: Contains the C# source code.
2. getActionAsText: JavaScript that reads the C# source code.
3. executeCSharp: PowerShell source to execute the C# source code.
4. invokeCSharp: JavaScript that invokes the C# source code.

1. Save C# Code as Action

In the first step we build an action, who we called testCSharp, in which we store the C# code to be executed. We simply save the C# source code here.

 

using System;

public class EntryPoint {
  public static object main() {

    // Console.WriteLine("Hello World!");
    return "Hello World";

  }
}

 

testCSharp.jpg

As we can see, this code doesn't do anything special, it just returns Hello World. Not imaginative, but sufficient for our example. The class and method names are also important here, because these are required for the invoke later.

2. Read Action as Text

In the second step we build an JavaScript action, called getActionAsText. As the name suggests, the content of an action is read out as text. With this action we read the C# code and pass it for execution.

 

/**
 * @param {string} in_moduleName - Module name which contains the action
 * @param {string} in_actionName - Action name which contains the text
 * @returns {string} actionAsText
 *
 * @AUTHOR Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * @Example
 * var in_moduleName = "com.vmware.basic";
 * var in_actionName = "createDirectory";
 *
 * @Example
 * var in_moduleName = "com.vmware.library.powershell.converter";
 * var in_actionName = "getConverter";
 *
 * Checked with Aria Automation 8.12.0 and 8.13.1
 */

var _getActionAsText = {

  main : function(moduleName, actionName) {

    var allActions =
      System.getModule("com.vmware.library.action").getAllActions();

    var allActionsInModule = allActions.filter( function(actionItems) {
      return actionItems.module.name === moduleName;
    });
    if (allActionsInModule.length === 0) {
      throw new Error("No actions were found in the module " +
        moduleName + ".");
    }

    var action = allActionsInModule.filter( function(actionItem) {
      return actionItem.name === actionName;
    });
    if (action.length === 0) {
      throw new Error("The action " + actionName +
        " was not found in the module " + moduleName + ".");
    } else if (action.length > 1) {
      System.warn("Too many actions, with the name " + actionName +
        ", were found in the module " + moduleName + ".");
      action.forEach( function(actionItem) {
        System.warn("ID: " + actionItem.id);
      });
      throw new Error("Too many actions, with the name " + actionName +
        ", were found in the module " + moduleName + ".");
    }

    return action[0].script;

  }

};

if (
  String(in_moduleName).trim() !== "" &&
  String(in_actionName).trim() !== ""
) {
  return _getActionAsText.main(
    in_moduleName,
    in_actionName
  );
} else {
  throw new Error(
    "in_moduleName or in_actionName argument can not be null"
  );
}

 

Here all actions are filtered, first by the module name and then by the action name, to return the content of the specified action.

3. PowerShell to Execute C#

In the third step we build a PowerShell action to execute the C# code.

 

<#
 # @param {string} csharpSource
 # @returns {Properties}
 #
 # @AUTHOR Stefan Schnell <mail@stefan-schnell.de>
 # @license MIT
 # @version 0.1.0
 #
 # Checked with Aria Automation 8.12.0
 #>

function Handler($context, $inputs) {

  Add-Type -TypeDefinition $inputs.csharpSource -Language CSharp;

  $result = [EntryPoint]::main();

  $output = @{
    status = "done"
    result = $result
  };

  return $output;

}

 

This executes the C# code. The class and method names appear again here. They are added with the Add-Type cmdlet and then called.

4. Invoke C# Code

In the fourth and final step we build an action to invoke the C# code.

 

/**
 * Invokes C# code.
 *
 * @param {string} in_moduleName - Module name which contains the action
 * @param {string} in_actionName - Action name which contains the C# code
 * @param {boolean} in_showOutput - Flag whether output should be displayed
 * @returns {string} result - Result of C# code execution
 *
 * @AUTHOR Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * @Example
 * var in_moduleName = "de.stschnell";
 * var in_actionName = "testCSharp";
 * var in_showOutput = false;
 *
 * Checked with Aria Automation 8.12.0
 */

var _invokeCSharpNS = {

  main : function(moduleName, actionName, showOutput) {

    try {

      var csharpSource = System.getModule("de.stschnell")
        .getActionAsText(moduleName, actionName);
      if (showOutput) {
        System.log(csharpSource);
      }

      var csharpExecute = System.getModule("de.stschnell")
        .executeCSharp(csharpSource);
      if (showOutput) {
        System.log(String(csharpExecute.result));
      }

      return String(csharpExecute.result);

    } catch (exception) {
      System.log(exception);
    }

  }

}

if (String(in_moduleName).trim() !== "" && String(in_actionName).trim() !== "") {
  return _invokeCSharpNS.main(
    in_moduleName,
    in_actionName,
    in_showOutput
  );
} else {
  throw new Error("in_moduleName or in_actionName argument can not be null");
}

 

This is the "bracket" which invokes all the other modules. It calls the action getActionAsText, it executes the C# code and then it delivers the result.

invokeCSharp001.jpg

invokeCSharp002.jpg

Conlusion

This approach shows that it is very easy to integrate C# directly into VMware Aria Automation, and also save the source code. A little combination of the possibilities of the polyglot programming environment allows us to do this.


More interesting information at blog.stschnell.de

1 Reply
LucD
Leadership
Leadership

Moved to VMware Aria Automation Tools Discussions


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos