protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { //Check if activity is allowed to run ActivityActivationReference.IsAllowed(this, __Context.Web); //Resolve tags in string (variables, etc) NWWorkflowContext ctx = NWWorkflowContext.GetContext(__Context, new Guid(__ListId), __ListItem.Id, WorkflowInstanceId, this); //Activity has begun executing base.LogProgressStart(ctx); LogHelper.LogInfo(Cat, "NTX PowerShell running at: " + this.__Context.CurrentItemUrl); //Determine if a Workflow Constant is being used for authentication string runtimeUsername = null; string runtimePassword = null; if (LoginUserName.Contains("WFConstant") & LoginPassword == "") { CredentialValue.DetermineRuntimeCredentials(LoginUserName, LoginPassword, out runtimeUsername, out runtimePassword, ctx.Context.Web.ID, ctx.Context.Site.ID); } else { runtimeUsername = LoginUserName; runtimePassword = LoginPassword; } String script = ctx.AddContextDataToString(PSScript); try { var ps = new PSHelper(); LogHelper.LogInfo(Cat, "Executing script as " + runtimeUsername + " on " + ComputerName + AppName + ":" + PortNumber + " SSL Enabled: " + SSLEnabled); ps.CreatePowerShellRunspace(SSLEnabled, ComputerName, PortNumber, AppName, ShellUri, runtimeUsername, runtimePassword); ps.SetRunSpaceVariable("NTXWorkflowContext", __Context); ps.SetRunSpaceVariable("NTXListID", new Guid(__ListId)); ps.SetRunSpaceVariable("NTXWorkflowInstanceID", new Guid(__ListId)); LogHelper.LogInfo(Cat, "NTX PowerShell RunSpaceVariables loaded"); ps.AddScriptToPipeLine(script); LogHelper.LogInfo(Cat, "NTX PowerShell Script loaded: " + script); ps.ConvertPipeContentsToString(); ResultOutput = ps.ProcessResultsToString(ps.InvokePipeline()); //Added to ensure runspaces are closed after execution completes. https://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspacefactory(v=vs.85).aspx ps.DisposePowerShellRunspace(); } catch (Exception e) { ResultOutput = e.ToString(); LogHelper.LogException(Cat, e); } //activity has stopped executing base.LogProgressEnd(ctx, executionContext); LogHelper.LogInfo(Cat, "NTX PowerShell completed at: " + __Context.CurrentItemUrl); return(ActivityExecutionStatus.Closed); }