コード例 #1
0
        protected override void Execute(CodeActivityContext context)
        {
            Runspace runspace = null;
            Pipeline pipeline = null;

            try
            {
                runspace = RunspaceFactory.CreateRunspace();
                runspace.Open();
                pipeline = runspace.CreatePipeline();
                string  _commandText = CommandText.Get(context);
                Command cmd          = new Command(_commandText, this.IsScript);
                if (this.parameters != null)
                {
                    foreach (KeyValuePair <string, InArgument> parameter in this.parameters)
                    {
                        if (parameter.Value.Expression != null)
                        {
                            cmd.Parameters.Add(parameter.Key, parameter.Value.Get(context));
                        }
                        else
                        {
                            cmd.Parameters.Add(parameter.Key, true);
                        }
                    }
                }
                if (this.PowerShellVariables != null)
                {
                    foreach (KeyValuePair <string, Argument> powerShellVariable in this.PowerShellVariables)
                    {
                        if ((powerShellVariable.Value.Direction == ArgumentDirection.In) || (powerShellVariable.Value.Direction == ArgumentDirection.InOut))
                        {
                            runspace.SessionStateProxy.SetVariable(powerShellVariable.Key, powerShellVariable.Value.Get(context));
                        }
                    }
                }

                pipeline.Commands.Add(cmd);
                IEnumerable pipelineInput = this.Input.Get(context);
                if (pipelineInput != null)
                {
                    foreach (object inputItem in pipelineInput)
                    {
                        pipeline.Input.Write(inputItem);
                    }
                }
                pipeline.Input.Close();
                Collection <T> _result = new Collection <T>();
                T result_;
                foreach (PSObject result in pipeline.Invoke())
                {
                    result_ = (T)result.BaseObject;
                    _result.Add(result_);
                }
                Output.Set(context, _result);
                pipeline.Dispose();
                runspace.Close();
            }
            catch (Exception e)
            {
                if (runspace != null)
                {
                    runspace.Dispose();
                }

                if (pipeline != null)
                {
                    pipeline.Dispose();
                }
                if (!ContinueOnError.Get(context))
                {
                    throw new InvalidOperationException(e.Message, e);
                }
            }
        }