Workflow Soluiton

 View Only
  • 1.  Running powershell script from code components

    Posted Feb 11, 2016 02:16 AM

    hi,

    i'm using this code to run powershell script from a workflow using the run code component.

    my code is:

    // Source: http://www.codeproject.com/KB/cs/HowToRunPowerShel...

    // create Powershell runspace

    Runspace runspace = RunspaceFactory.CreateRunspace();

    // open it

    runspace.Open();

    // create a pipeline and feed it the script text

    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(PSInput);

    // add an extra command to transform the script
    // output objects into nicely formatted strings

    // remove this line to get the actual objects
    // that the script returns. For example, the script

    // "Get-Process" returns a collection
    // of System.Diagnostics.Process instances.

    pipeline.Commands.Add("Out-String");

    // execute the script

    Collection<PSObject> results = pipeline.Invoke();
    // close the runspace

    runspace.Close();

    // convert the script result into a single string

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
    stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();

    the problem is that the code run OK in debug mode but when publishing to iis i get the following error:

     

    System.Exception: Execution Engine Exception  ---> System.Exception: Execution Exception ---> System.Exception: exception component created:  ---> LogicBase.Framework.BusinessRuleException: The critical error failed execution on component Code (Script) Component : Exception has been thrown by the target of an invocation. :    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at LogicBase.Components.Scripting.ScriptedComponent.Run(IData data)
       at LogicBase.Core.ExecutionEngine.SinglePathProcessComponentExecutionDelegate.Execute(IData data, IOrchestrationComponent comp, String&amp; outputPath, IExecutionEngine engine, TLExecutionContext context)
       at LogicBase.Core.ExecutionEngine.AbstractExecutionEngine.RunComponent(TLExecutionContext context, IData data, IOrchestrationComponent comp)
       --- End of inner exception stack trace ---
       at LogicBase.Core.ExecutionEngine.ExceptionComponentDelegate.Execute(IData data, IOrchestrationComponent comp, String& outputPath, IExecutionEngine engine, TLExecutionContext context)
       --- End of inner exception stack trace ---
       at LogicBase.Core.ExecutionEngine.AbstractExecutionEngine.RunEngine(List`1 executionQueue)
       --- End of inner exception stack trace ---
       at LogicBase.Core.ExecutionEngine.AbstractExecutionEngine.RunEngine(List`1 executionQueue)
       at LogicBase.Core.ExecutionEngine.AbstractExecutionEngine.ExecuteComponent(IData data, IOrchestrationComponent component)
       at LogicBase.Core.Models.Workflow.WorkflowExecutionEngine.StartInternal(String trackingId, IData data, String modelName, IData globalData, String testRunId, TestingScenario testingScenario)
       at LogicBase.Core.Models.Workflow.WorkflowExecutionEngine.Start(IData data, String modelName)
    

    can anyone help? is there a native powershell component i can use?

     

    thanks

    mordechai



  • 2.  RE: Running powershell script from code components

    Posted Feb 12, 2016 03:17 AM

    Hi,

    Try to add a new application pool in IIS and in the Advanced Settings... you can change the Identity with which your workflow runs with. You can try with your account first if you do not have a service account. As far as I know when running in debug mode it uses the account with which you have started the Workflow Manager. And when published it uses the built in IIS account. And that account might not have permissions required to run your powershell script.

    Stefan



  • 3.  RE: Running powershell script from code components

    Posted Feb 12, 2016 03:44 AM

    There's a couple of components you could use, save the file to the server then run them with either of the following

    • Execute Process (LogicBase.Components.Default.ExecuteProcessComponent)
    • Execute Batch (LogicBase.Components.Default.Process.ExecuteBatch)


  • 4.  RE: Running powershell script from code components

    Posted Feb 12, 2016 08:31 AM

    There should be an Execute Process component and a Execute Process and Wait component.

    If you have a .ps1 file already out there (you can actually deploy it as a resource to the project, if needed), you could have your command be something like "powershell.exe blah.ps1" and it would invoke the powershell like that.



  • 5.  RE: Running powershell script from code components

    Posted Feb 14, 2016 05:29 AM

    Hi,

    i tried using execute batch and start a ps1 file but again, on the deployed app i now get System.IO.IOException: The file exists.

    i'll try to change the iis app pool id and check again.

    thanks

    mordechai