protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { if (context == null) { throw new ArgumentNullException("context"); } Runspace runspace = null; Pipeline pipeline = null; PipelineInvokerAsyncResult pipelineInvokerAsyncResult = null; var script = this.ResolveScript( this.BuildWorkspace.Get(context), this.Script.Get(context), this.Arguments.Get(context)); context.Track(new System.Activities.Tracking.CustomTrackingRecord(string.Format(CultureInfo.CurrentCulture, "Script resolved to {0}", script))); //context.TrackBuildMessage(string.Format(CultureInfo.CurrentCulture, "Script resolved to {0}", script), BuildMessageImportance.Low); try { runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); pipeline = runspace.CreatePipeline(script); context.UserState = pipeline; pipelineInvokerAsyncResult = new PipelineInvokerAsyncResult(pipeline, callback, state); return(pipelineInvokerAsyncResult); } finally { if (runspace != null) { runspace.Dispose(); } if (pipeline != null) { pipeline.Dispose(); } if (pipelineInvokerAsyncResult != null) { pipelineInvokerAsyncResult.Dispose(); } } }
protected override PSObject[] EndExecute(AsyncCodeActivityContext context, IAsyncResult result) { PipelineInvokerAsyncResult asyncResult = result as PipelineInvokerAsyncResult; Pipeline pipeline = context.UserState as Pipeline; try { if (asyncResult.Exception != null) { throw new PowerShellExecutionException(asyncResult.Exception, asyncResult.ErrorRecords); } return(asyncResult.PipelineOutput.ToArray()); } finally { DisposePipeline(pipeline); } }