/// <summary>
        /// private ErrorReady handling method that will pass the call on to any event handlers that are
        /// attached to the OnErrorReady event of this <see cref="PipelineExecutor"/> instance.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        private void SynchErrorReady(PipelineExecutor sender, ICollection <object> data)
        {
            ErrorReadyDelegate delegateErrorReadyCopy = OnErrorReady;

            if (delegateErrorReadyCopy != null)
            {
                delegateErrorReadyCopy(sender, data);
            }
        }
        /// <summary>
        /// private DataEnd handling method that will pass the call on to any handlers that are
        /// attached to the OnDataEnd event of this <see cref="PipelineExecutor"/> instance.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        private void SynchDataEnd(PipelineExecutor sender)
        {
            DataEndDelegate delegateDataEndCopy = OnDataEnd;

            if (delegateDataEndCopy != null)
            {
                delegateDataEndCopy(sender);
            }
        }
        /// <summary>
        /// private DataReady handling method that will pass the call on to any event handlers that are
        /// attached to the OnDataReady event of this <see cref="PipelineExecutor"/> instance.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        private void SynchDataReady(PipelineExecutor sender, ICollection <PSObject> data)
        {
            DataReadyDelegate delegateDataReadyCopy = OnDataReady;

            if (delegateDataReadyCopy != null)
            {
                delegateDataReadyCopy(sender, data);
            }
        }
        public void QueryVCESXBuildAsync(String vcServer)
        {
            if (vCenterCount != 0) {
                WaitQueue.Enqueue(vcServer);
            }
            else {
                try {
                    this.StopScript();
                    this.CreateRunspace();
                    StringBuilder sbConnectVIServer = new StringBuilder();

                    sbConnectVIServer.AppendFormat("Connect-VIServer {0}\n Get-VMHost", vcServer);
                    Console.Error.WriteLine("Starting PowerCLI command");
                    pipelineExecutor = new PipelineExecutor(run, this.invoker, sbConnectVIServer.ToString());
                    pipelineExecutor.OnDataReady += new PipelineExecutor.DataReadyDelegate(pipelineExecutor_OnDataReady);
                    pipelineExecutor.OnDataEnd += new PipelineExecutor.DataEndDelegate(pipelineExecutor_OnDataEnd);
                    pipelineExecutor.OnErrorReady += new PipelineExecutor.ErrorReadyDelegate(pipelineExecutor_OnErrorReady);
                    pipelineExecutor.Start();
                    vCenterCount = 1;
                }
                catch (Exception e) {
                    Console.Error.WriteLine("Stack Trace: {0}, Message: {1}", e.StackTrace, e.Message);
                }
            }
        }
        public void QueryVCBuildAsync(String vcServer)
        {
            StringBuilder sbConnectVIServer = new StringBuilder();
            sbConnectVIServer.AppendFormat("Connect-VIServer {0}", vcServer);

            pipelineExecutor = new PipelineExecutor(run, this.invoker, sbConnectVIServer.ToString());
            pipelineExecutor.OnDataReady += new PipelineExecutor.DataReadyDelegate(pipelineExecutor_OnDataReady);
            pipelineExecutor.OnDataEnd += new PipelineExecutor.DataEndDelegate(pipelineExecutor_OnDataEnd);
            pipelineExecutor.OnErrorReady += new PipelineExecutor.ErrorReadyDelegate(pipelineExecutor_OnErrorReady);
            pipelineExecutor.Start();
        }
 //http://www.codeproject.com/KB/threads/AsyncPowerShell.aspx
 private void StopScript()
 {
     Console.Error.WriteLine("In Stop Script...");
     if (pipelineExecutor != null) {
         pipelineExecutor.OnDataReady -= new PipelineExecutor.DataReadyDelegate(pipelineExecutor_OnDataReady);
         pipelineExecutor.OnDataEnd -= new PipelineExecutor.DataEndDelegate(pipelineExecutor_OnDataEnd);
         pipelineExecutor.Stop();
         pipelineExecutor = null;
         Console.Error.WriteLine("Script Stopped");
         run.Close();
         run = null;
     }
 }
 private void pipelineExecutor_OnErrorReady(PipelineExecutor sender, ICollection<object> data)
 {
     foreach (object e in data) {
         Console.Error.WriteLine(e.ToString());
     }
 }
 private void pipelineExecutor_OnDataReady(PipelineExecutor sender, ICollection<PSObject> data)
 {
     try {
         Console.Error.WriteLine("Data Finished with {0} results", data.Count);
         if (data.Count > 0) {
             Type type = data.ElementAt<PSObject>(0).BaseObject.GetType();
             switch (type.Name) {
                 case "VIServerImpl":
                     FillVCInfo(data);
                     break;
                 case "VMHostImpl":
                     FillESXInfo(data);
                     break;
             }
         }
     }
     catch (Exception e) {
         Console.Error.WriteLine("Stack Trace: {0}, Message: {1}", e.StackTrace, e.Message);
     }
 }
        private void pipelineExecutor_OnDataEnd(PipelineExecutor sender)
        {
            try {
                if (sender.Pipeline.PipelineStateInfo.State == PipelineState.Failed) {
                    Console.Error.WriteLine("Pipeline Failed but is done");
                }
                else {
                    Console.Error.WriteLine("Pipeline is done");
                    OnDataReady();
                    if (WaitQueue.Count > 0) {
                        vCenterCount = 0;

                        QueryVCESXBuildAsync((string)WaitQueue.Dequeue());

                    }
                }
            }
            catch (Exception e) {
                Console.Error.WriteLine("Stack Trace: {0}, Message: {1}", e.StackTrace, e.Message);
            }
        }
 /// <summary>
 /// private ErrorReady handling method that will pass the call on to any event handlers that are
 /// attached to the OnErrorReady event of this <see cref="PipelineExecutor"/> instance.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="data"></param>
 private void SynchErrorReady(PipelineExecutor sender, ICollection<object> data)
 {
     try {
         ErrorReadyDelegate delegateErrorReadyCopy = OnErrorReady;
         if (delegateErrorReadyCopy != null) {
             delegateErrorReadyCopy(sender, data);
         }
     }
     catch (Exception ex) {
         Console.Error.WriteLine("Stack Trace: {0}, Message: {1}", ex.StackTrace, ex.Message);
     }
 }
 /// <summary>
 /// private DataEnd handling method that will pass the call on to any handlers that are
 /// attached to the OnDataEnd event of this <see cref="PipelineExecutor"/> instance.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="data"></param>
 private void SynchDataEnd(PipelineExecutor sender)
 {
     try {
         DataEndDelegate delegateDataEndCopy = OnDataEnd;
         if (delegateDataEndCopy != null) {
             delegateDataEndCopy(sender);
         }
     }
     catch (Exception ex) {
         Console.Error.WriteLine("Stack Trace: {0}, Message: {1}", ex.StackTrace, ex.Message);
     }
 }