Running an external application from within a workflow

  • 27 June 2016
  • 5 replies
  • 59 views

Badge +5

To run an executable from within a workflow k2 for visual studios should be used, as we will need to execute custom code from within the workflow which can easily be done from K2 for Visual Studio.

 

 

 

 

Firstly, on the workflow we will need to add a server event.

 

 

 

 

 

11474i134A1AF1D754EB3B.png

 

 

 

 

 

Right click on the server event and select View Code and then Event Item.

 

 

 

 

 

11064i08D813915C720CA5.png

 

 

 

 

 

After event item was selected a new tab will open in Visual Studio.

 

 

We can then add code to start an external application with parameters as shown in the following example

 

 

 

 

 

11186i173DBD069881D507.png

 

 

 

 

 

This snipped of code will execute the “Executable.exe” on the local server as specified by the location @”c:TempExecutable.exe” with a parameter in this case being a value received from the “K2.ProcessInstance.folio”

 

 

 

 

 

note that after the executable filename and path the “,” comma character allows you to add an argument.

 

 

 

 

 

This project can then be deployed and the Application specified in the custom code will be executed on this event in the workflow.

 

 

For executing external applications from within a workflow over a network, the following link may server as some guide lines on how this can be achieved.

 

 

 

 

 

https://social.msdn.microsoft.com/Forums/vstudio/en-US/2b714d4f-92c1-4bd3-8ff1-226a278d129c/start-process-on-remote-machine?forum=csharpgeneral

 


5 replies

Badge +4

I have tried this and I cannot get the remote application to launch.

Basically what i am doing is trying to start powershell and running a powershell script but for some reason this will not launch it.  I have code around Process.Start that verifies that the code is running without error but i cannot figure out why the Process.Start is not kicking off.  It never errors out, it just does not launch the powershell portion.

 

Any ideas?

 

 

My code looks like this:

 

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Diagnostics;
using System.Net.Mail;
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using SourceCode.KO;
using SourceCode.Workflow.Common.Extenders;

using hostContext = Project.EventItemContext;
namespace ExtenderProject
{
public partial class EventItem : ICodeExtender<hostContext>
{
public void Main(Project.EventItemContext K2)
{
MailMessage mail = new MailMessage("email@email.com", "email@email.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "mail.com";

try
{
//Found alternate way of launching powershell, not working
PowerShell ps = PowerShell.Create();
ps.AddScript(@"C:Program FilesTIDALTESCmdLinein
unbook.ps1");
ps.Invoke();

//This is the suggested code and it does not launch
string strCmdText;
strCmdText = @"c:
unbook.ps1";
System.Diagnostics.Process.Start(@"c:windowssystem32windowspowershellv1.0powershell.exe", strCmdText);

//This mail code launches successfully and i receive the email it sends.
mail.Subject = "Mail from K2 Server Event";
mail.Body = "This email is from the server event in the K2 runbook workflow. " +
strCmdText.ToString();
client.Send(mail);
}
catch(Exception e)
{
mail.Subject = "Mail from K2 Server Event";
mail.Body = e.Message;
client.Send(mail);
}
}
}
}
Badge +5

Have you looked at these options instead of creating your own code:

 

Powershell projects on the K2 Market:

 

http://community.k2.com/t5/K2-blackpearl/PowerShell-Wizard/ba-p/981

 

http://community.k2.com/t5/K2-blackpearl/PowerShell-Service-Object/ba-p/1025#

 

Badge +4

I was using the suggested method provided by K2 support, I didnt realize that the two projects you listed existed.

 

I tried the downloading the two options you suggested but something is wrong with the zip files on both, they cannot be extracted because they are seen as invalid by Windows.

Badge +4

I cannot get these to work, have you used them?  Do they not work on 4.6.9?

Danny are you able to find solution.After all approvals i need to run powershell script.Do you have any idea to launch powershell and execute script by passing values to script

Reply