Esempio n. 1
0
        public bool Execute(string directory, Mapping mapToRun, BuildData buildData)
        {
            try
            {
                DeploymentHost host = new DeploymentHost();
                Runspace space = RunspaceFactory.CreateRunspace(host);
                space.Open();

                this.PopulateVariables(space, mapToRun, buildData);

                string command = this.GeneratePipelineCommand(directory, mapToRun);
                this._scriptRun = command;

                this.EnsureExecutionPolicy(space);
                Pipeline pipeline = space.CreatePipeline(command);
                Collection<PSObject> outputObjects = pipeline.Invoke();

                if (pipeline.PipelineStateInfo.State != PipelineState.Failed)
                {
                    this._errorOccured = false;
                }

                string output = this.GenerateOutputFromObjects(outputObjects);
                this._output = output;

                space.Close();
            }
            catch (Exception ex)
            {
                this._errorOccured = true;
                this._output = ex.ToString();
            }

            return this.ErrorOccured;
        }
Esempio n. 2
0
        public bool Execute(string directory, Mapping mapToRun, BuildData buildData)
        {
            try
            {
                DeploymentHost host  = new DeploymentHost();
                Runspace       space = RunspaceFactory.CreateRunspace(host);
                space.Open();

                this.PopulateVariables(space, mapToRun, buildData);

                string command = this.GeneratePipelineCommand(directory, mapToRun);
                this._scriptRun = command;

                this.EnsureExecutionPolicy(space);
                Pipeline pipeline = space.CreatePipeline(command);
                Collection <PSObject> outputObjects = pipeline.Invoke();

                if (pipeline.PipelineStateInfo.State != PipelineState.Failed)
                {
                    this._errorOccured = false;
                }

                string output = this.GenerateOutputFromObjects(outputObjects);
                this._output = output;

                space.Close();
            }
            catch (Exception ex)
            {
                this._errorOccured = true;
                this._output       = ex.ToString();
            }

            return(this.ErrorOccured);
        }
Esempio n. 3
0
        //private void EnsureExecutionPolicy(Runspace space)
        //{
        //    Pipeline executionPolicyPipeline = space.CreatePipeline("Set-ExecutionPolicy Unrestricted");
        //    executionPolicyPipeline.Invoke();
        //}

        public void ExecuteCommand(string command, IDictionary <string, object> variables)
        {
            Runspace space = null;

            try
            {
                this._errorOccurred = true;

                DeploymentHost host = new DeploymentHost(null);
                space = RunspaceFactory.CreateRunspace(host);
                space.Open();

                if (null != variables)
                {
                    foreach (string key in variables.Keys)
                    {
                        space.SessionStateProxy.SetVariable(key, variables[key]);
                    }
                }
                // this prevents TfsDeployer running as a non-admin user.
                // installation documentation should describe setting execution policy and possibly using signed scripts
                //this.EnsureExecutionPolicy(space);


                Collection <PSObject> outputObjects;

                Pipeline pipeline = space.CreatePipeline(command + " | Out-String -Stream ;");
                outputObjects = pipeline.Invoke();

                if (pipeline.PipelineStateInfo.State != PipelineState.Failed)
                {
                    this._errorOccurred = false;
                }

                string output = this.GenerateOutputFromObjects(outputObjects);
                this._output = output;

                space.Close();
            }
            catch (RuntimeException ex)
            {
                this._errorOccurred = true;
                ErrorRecord record = ex.ErrorRecord;
                var         sb     = new StringBuilder();
                sb.AppendLine(record.Exception.ToString());
                sb.AppendLine(record.InvocationInfo.PositionMessage);
                this._output = sb.ToString();
            }
            catch (Exception ex)
            {
                this._errorOccurred = true;
                this._output        = ex.ToString();
            }
            finally
            {
                if (null != space)
                {
                    space.Close();
                }
            }
        }