コード例 #1
0
        public Collection<PSObject> InvokeScript(string script, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
        {
            ScriptBlock scriptBlock = NewScriptBlock(script);
            var executionVisitor = new ExecutionVisitor(executionContext, commandRuntime);
            scriptBlock.Ast.Visit(executionVisitor);

            return new Collection<PSObject>();
        }
コード例 #2
0
ファイル: Command.cs プロジェクト: nickchal/pash
 internal Command(Command command)
 {
     this._mergeInstructions = new PipelineResultTypes[4];
     this._parameters = new CommandParameterCollection();
     this._command = string.Empty;
     this._isScript = command._isScript;
     this._useLocalScope = command._useLocalScope;
     this._command = command._command;
     this._mergeInstructions = command._mergeInstructions;
     this._mergeMyResult = command._mergeMyResult;
     this._mergeToResult = command._mergeToResult;
     this._mergeUnclaimedPreviousCommandResults = command._mergeUnclaimedPreviousCommandResults;
     this.isEndOfStatement = command.isEndOfStatement;
     foreach (CommandParameter parameter in command.Parameters)
     {
         this.Parameters.Add(new CommandParameter(parameter.Name, parameter.Value));
     }
 }
コード例 #3
0
 public Collection<PSObject> InvokeScript(string script, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
 {
     var context = useNewScope ? executionContext.Clone(ScopeUsages.NewScriptScope)
                               : executionContext;
     //Let's see on the long run if there is an easier solution for this #ExecutionContextChange
     //we need to change the global execution context to change the scope we are currently operating in
     executionContext.CurrentRunspace.ExecutionContext = context;
     try
     {
         ScriptBlock scriptBlock = NewScriptBlock(script);
         var executionVisitor = new ExecutionVisitor(context, commandRuntime);
         // sburnicki - handle ExitException
         scriptBlock.Ast.Visit(executionVisitor);
     }
     finally //make sure we set back the old execution context, no matter what happened
     {
         executionContext.CurrentRunspace.ExecutionContext = executionContext;
     }
     return new Collection<PSObject>();
 }
コード例 #4
0
        public Collection <PSObject> InvokeScript(string script, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
        {
            var context = useNewScope ? executionContext.Clone(ScopeUsages.NewScriptScope)
                                      : executionContext;

            //Let's see on the long run if there is an easier solution for this #ExecutionContextChange
            //we need to change the global execution context to change the scope we are currently operating in
            executionContext.CurrentRunspace.ExecutionContext = context;
            try
            {
                ScriptBlock scriptBlock      = NewScriptBlock(script);
                var         executionVisitor = new ExecutionVisitor(context, commandRuntime);
                // sburnicki - handle ExitException
                scriptBlock.Ast.Visit(executionVisitor);
            }
            finally //make sure we set back the old execution context, no matter what happened
            {
                executionContext.CurrentRunspace.ExecutionContext = executionContext;
            }
            return(new Collection <PSObject>());
        }
コード例 #5
0
        internal static Command FromPSObjectForRemoting(PSObject commandAsPSObject)
        {
            if (commandAsPSObject == null)
            {
                throw PSTraceSource.NewArgumentNullException("commandAsPSObject");
            }
            string              propertyValue = RemotingDecoder.GetPropertyValue <string>(commandAsPSObject, "Cmd");
            bool                isScript      = RemotingDecoder.GetPropertyValue <bool>(commandAsPSObject, "IsScript");
            bool?               useLocalScope = RemotingDecoder.GetPropertyValue <bool?>(commandAsPSObject, "UseLocalScope");
            Command             command       = new Command(propertyValue, isScript, useLocalScope);
            PipelineResultTypes myResult      = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeMyResult");
            PipelineResultTypes toResult      = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeToResult");

            command.MergeMyResults(myResult, toResult);
            command.MergeUnclaimedPreviousCommandResults = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergePreviousResults");
            if (commandAsPSObject.Properties["MergeError"] != null)
            {
                command.MergeInstructions[0] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeError");
            }
            if (commandAsPSObject.Properties["MergeWarning"] != null)
            {
                command.MergeInstructions[1] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeWarning");
            }
            if (commandAsPSObject.Properties["MergeVerbose"] != null)
            {
                command.MergeInstructions[2] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeVerbose");
            }
            if (commandAsPSObject.Properties["MergeDebug"] != null)
            {
                command.MergeInstructions[3] = RemotingDecoder.GetPropertyValue <PipelineResultTypes>(commandAsPSObject, "MergeDebug");
            }
            foreach (PSObject obj2 in RemotingDecoder.EnumerateListProperty <PSObject>(commandAsPSObject, "Args"))
            {
                command.Parameters.Add(CommandParameter.FromPSObjectForRemoting(obj2));
            }
            return(command);
        }
コード例 #6
0
ファイル: Command.cs プロジェクト: Ventero/Pash
 public void MergeMyResults(PipelineResultTypes myResult, PipelineResultTypes toResult)
 {
     MergeMyResult = myResult;
     MergeToResult = toResult;
 }
コード例 #7
0
 public void InvokePipeToOutput(CommandInfo commandInfo, Hashtable args, PipelineResultTypes pipelineResultTypes)
 {
     if (commandInfo == null) throw new ArgumentNullException("commandInfo");
     _invokeCommand.InvokeScript("param($c, $a) . $c @a", true, pipelineResultTypes, null, commandInfo, args);
 }
コード例 #8
0
ファイル: Command.cs プロジェクト: zhuyue1314/Pash
 public void MergeMyResults(PipelineResultTypes myResult, PipelineResultTypes toResult)
 {
     MergeMyResult = myResult;
     MergeToResult = toResult;
 }
コード例 #9
0
 public Collection<PSObject> InvokeScript(string script, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
 {
     if (script == null)
     {
         throw new ArgumentNullException("script");
     }
     ScriptBlock sb = ScriptBlock.Create(this._context, script);
     return this.InvokeScript(sb, useNewScope, writeToPipeline, input, args);
 }
コード例 #10
0
 private Collection<PSObject> InvokeScript(ScriptBlock sb, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
 {
     object obj2;
     if (this._cmdlet != null)
     {
         this._cmdlet.ThrowIfStopping();
     }
     Cmdlet contextCmdlet = null;
     ScriptBlock.ErrorHandlingBehavior writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe;
     if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output)
     {
         contextCmdlet = this._cmdlet;
         writeToPipeline &= ~PipelineResultTypes.Output;
     }
     if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error)
     {
         writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe;
         writeToPipeline &= ~PipelineResultTypes.Error;
     }
     if (writeToPipeline != PipelineResultTypes.None)
     {
         throw PSTraceSource.NewNotImplementedException();
     }
     if (contextCmdlet != null)
     {
         sb.InvokeUsingCmdlet(contextCmdlet, useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args);
         obj2 = AutomationNull.Value;
     }
     else
     {
         obj2 = sb.DoInvokeReturnAsIs(useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args);
     }
     if (obj2 == AutomationNull.Value)
     {
         return new Collection<PSObject>();
     }
     Collection<PSObject> collection = obj2 as Collection<PSObject>;
     if (collection == null)
     {
         collection = new Collection<PSObject>();
         IEnumerator enumerator = null;
         enumerator = LanguagePrimitives.GetEnumerator(obj2);
         if (enumerator != null)
         {
             while (enumerator.MoveNext())
             {
                 object current = enumerator.Current;
                 collection.Add(LanguagePrimitives.AsPSObjectOrNull(current));
             }
             return collection;
         }
         collection.Add(LanguagePrimitives.AsPSObjectOrNull(obj2));
     }
     return collection;
 }
コード例 #11
0
 public Collection <PSObject> InvokeScript(string script, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
 {
     throw new NotImplementedException();
 }
コード例 #12
0
 public Collection<PSObject> InvokeScript(string script, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
 {
     throw new NotImplementedException();
 }
コード例 #13
0
ファイル: Command.cs プロジェクト: nickchal/pash
 internal Command(string command, bool isScript, bool? useLocalScope, bool mergeUnclaimedPreviousErrorResults) : this(command, isScript, useLocalScope)
 {
     if (mergeUnclaimedPreviousErrorResults)
     {
         this._mergeUnclaimedPreviousCommandResults = PipelineResultTypes.Warning;
     }
 }
コード例 #14
0
ファイル: Command.cs プロジェクト: nickchal/pash
 public void MergeMyResults(PipelineResultTypes myResult, PipelineResultTypes toResult)
 {
     if ((myResult == PipelineResultTypes.None) && (toResult == PipelineResultTypes.None))
     {
         this._mergeMyResult = myResult;
         this._mergeToResult = toResult;
         for (int i = 0; i < 4; i++)
         {
             this._mergeInstructions[i] = PipelineResultTypes.None;
         }
     }
     else
     {
         if ((myResult == PipelineResultTypes.None) || (myResult == PipelineResultTypes.Output))
         {
             throw PSTraceSource.NewArgumentException("myResult", "RunspaceStrings", "InvalidMyResultError", new object[0]);
         }
         if ((myResult == PipelineResultTypes.Error) && (toResult != PipelineResultTypes.Output))
         {
             throw PSTraceSource.NewArgumentException("toResult", "RunspaceStrings", "InvalidValueToResultError", new object[0]);
         }
         if ((toResult != PipelineResultTypes.Output) && (toResult != PipelineResultTypes.Null))
         {
             throw PSTraceSource.NewArgumentException("toResult", "RunspaceStrings", "InvalidValueToResult", new object[0]);
         }
         if (myResult == PipelineResultTypes.Error)
         {
             this._mergeMyResult = myResult;
             this._mergeToResult = toResult;
         }
         if ((myResult == PipelineResultTypes.Error) || (myResult == PipelineResultTypes.All))
         {
             this._mergeInstructions[0] = toResult;
         }
         if ((myResult == PipelineResultTypes.Warning) || (myResult == PipelineResultTypes.All))
         {
             this._mergeInstructions[1] = toResult;
         }
         if ((myResult == PipelineResultTypes.Verbose) || (myResult == PipelineResultTypes.All))
         {
             this._mergeInstructions[2] = toResult;
         }
         if ((myResult == PipelineResultTypes.Debug) || (myResult == PipelineResultTypes.All))
         {
             this._mergeInstructions[3] = toResult;
         }
     }
 }
コード例 #15
0
ファイル: Command.cs プロジェクト: nickchal/pash
 private Pipe GetRedirectionPipe(PipelineResultTypes toType, MshCommandRuntime mcr)
 {
     if (toType == PipelineResultTypes.Output)
     {
         return mcr.OutputPipe;
     }
     return new Pipe { NullPipe = true };
 }
コード例 #16
0
        /// <summary>
        /// Merges this commands results.
        /// </summary>
        /// <param name="myResult">
        /// Pipeline stream to be redirected.
        /// </param>
        /// <param name="toResult">
        /// Pipeline stream in to which myResult is merged
        /// </param>
        /// <exception cref="ArgumentException">
        /// myResult parameter is not PipelineResultTypes.Error or
        /// toResult parameter is not PipelineResultTypes.Output
        /// </exception>
        /// <remarks>
        /// Currently only operation supported is to merge error of command to output of
        /// command.
        /// </remarks>
        public void MergeMyResults(PipelineResultTypes myResult, PipelineResultTypes toResult)
        {
            if (myResult == PipelineResultTypes.None && toResult == PipelineResultTypes.None)
            {
                // For V2 backwards compatibility.
                MergeMyResult = myResult;
                MergeToResult = toResult;

                for (int i = 0; i < MaxMergeType; ++i)
                {
                    MergeInstructions[i] = PipelineResultTypes.None;
                }

                return;
            }

            // Validate parameters.
            if (myResult == PipelineResultTypes.None || myResult == PipelineResultTypes.Output)
            {
                throw PSTraceSource.NewArgumentException("myResult", RunspaceStrings.InvalidMyResultError);
            }

            if (myResult == PipelineResultTypes.Error && toResult != PipelineResultTypes.Output)
            {
                throw PSTraceSource.NewArgumentException("toResult", RunspaceStrings.InvalidValueToResultError);
            }

            if (toResult != PipelineResultTypes.Output && toResult != PipelineResultTypes.Null)
            {
                throw PSTraceSource.NewArgumentException("toResult", RunspaceStrings.InvalidValueToResult);
            }

            // For V2 backwards compatibility.
            if (myResult == PipelineResultTypes.Error)
            {
                MergeMyResult = myResult;
                MergeToResult = toResult;
            }

            // Set internal merge instructions.
            if (myResult == PipelineResultTypes.Error || myResult == PipelineResultTypes.All)
            {
                MergeInstructions[(int)MergeType.Error] = toResult;
            }

            if (myResult == PipelineResultTypes.Warning || myResult == PipelineResultTypes.All)
            {
                MergeInstructions[(int)MergeType.Warning] = toResult;
            }

            if (myResult == PipelineResultTypes.Verbose || myResult == PipelineResultTypes.All)
            {
                MergeInstructions[(int)MergeType.Verbose] = toResult;
            }

            if (myResult == PipelineResultTypes.Debug || myResult == PipelineResultTypes.All)
            {
                MergeInstructions[(int)MergeType.Debug] = toResult;
            }

            if (myResult == PipelineResultTypes.Information || myResult == PipelineResultTypes.All)
            {
                MergeInstructions[(int)MergeType.Information] = toResult;
            }
        }
コード例 #17
0
        private void ConvertCommand(CommandAst commandAst, bool isTrustedInput)
        {
            // First need command name.
            var commandName = GetCommandName(commandAst.CommandElements[0], isTrustedInput);

            var command = new Command(commandName, isScript: false, useLocalScope: _createLocalScope);

            // Handle redirections, if any (there can really be just 0 or 1).
            if (commandAst.Redirections.Count > 0)
            {
                Diagnostics.Assert(commandAst.Redirections.Count == 1, "only 1 kind of redirection is supported");
                Diagnostics.Assert(commandAst.Redirections[0] is MergingRedirectionAst, "unexpected redirection type");

                PipelineResultTypes toType = PipelineResultTypes.Output;
                PipelineResultTypes fromType;
                switch (commandAst.Redirections[0].FromStream)
                {
                case RedirectionStream.Error:
                    fromType = PipelineResultTypes.Error;
                    break;

                case RedirectionStream.Warning:
                    fromType = PipelineResultTypes.Warning;
                    break;

                case RedirectionStream.Verbose:
                    fromType = PipelineResultTypes.Verbose;
                    break;

                case RedirectionStream.Debug:
                    fromType = PipelineResultTypes.Debug;
                    break;

                case RedirectionStream.Information:
                    fromType = PipelineResultTypes.Information;
                    break;

                case RedirectionStream.All:
                    fromType = PipelineResultTypes.All;
                    break;

                default:
                    // Default to Error->Output to be compatible with V2.
                    fromType = PipelineResultTypes.Error;
                    break;
                }

                command.MergeMyResults(fromType, toType);
            }

            _powershell.AddCommand(command);

            // Now the parameters and arguments.
            foreach (var ast in commandAst.CommandElements.Skip(1))
            {
                var exprAst = ast as ExpressionAst;
                if (exprAst != null)
                {
                    VariableExpressionAst variableAst = null;

                    var usingExprAst = ast as UsingExpressionAst;
                    if (usingExprAst != null)
                    {
                        string usingAstKey = PsUtils.GetUsingExpressionKey(usingExprAst);
                        object usingValue  = _usingValueMap[usingAstKey];
                        variableAst = usingExprAst.SubExpression as VariableExpressionAst;
                        if (variableAst != null && variableAst.Splatted)
                        {
                            // Support the splatting of a dictionary
                            var parameters = usingValue as System.Collections.IDictionary;
                            if (parameters != null)
                            {
                                _powershell.AddParameters(parameters);
                            }
                            else
                            {
                                // Support the splatting of an array
                                var arguments = usingValue as System.Collections.IEnumerable;
                                if (arguments != null)
                                {
                                    foreach (object argument in arguments)
                                    {
                                        _powershell.AddArgument(argument);
                                    }
                                }
                                else
                                {
                                    // Splat the object directly.
                                    _powershell.AddArgument(usingValue);
                                }
                            }
                        }
                        else
                        {
                            _powershell.AddArgument(usingValue);
                        }

                        continue;
                    }

                    variableAst = ast as VariableExpressionAst;
                    if (variableAst != null && variableAst.Splatted)
                    {
                        GetSplattedVariable(variableAst);
                    }
                    else
                    {
                        var    constantExprAst = ast as ConstantExpressionAst;
                        object argument;
                        if (constantExprAst != null && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(constantExprAst.StaticType)))
                        {
                            var commandArgumentText = constantExprAst.Extent.Text;
                            argument = constantExprAst.Value;
                            if (!commandArgumentText.Equals(constantExprAst.Value.ToString(), StringComparison.Ordinal))
                            {
                                // The wrapped number will actually return a PSObject which could end holding a reference to
                                // a typetable, making the object runspace specific.  We should find a better way to avoid
                                // any possibility of sharing problems, though this is unlikely to cause problems.
                                argument = ParserOps.WrappedNumber(argument, commandArgumentText);
                            }
                        }
                        else
                        {
                            if (!isTrustedInput)
                            {
                                try
                                {
                                    argument = GetSafeValueVisitor.GetSafeValue(exprAst, _context, GetSafeValueVisitor.SafeValueContext.GetPowerShell);
                                }
                                catch (System.Exception)
                                {
                                    throw new ScriptBlockToPowerShellNotSupportedException(
                                              "CantConvertWithDynamicExpression",
                                              null,
                                              AutomationExceptions.CantConvertWithDynamicExpression,
                                              exprAst.Extent.Text);
                                }
                            }
                            else
                            {
                                argument = GetExpressionValue(exprAst, isTrustedInput);
                            }
                        }

                        _powershell.AddArgument(argument);
                    }
                }
                else
                {
                    AddParameter((CommandParameterAst)ast, isTrustedInput);
                }
            }
        }
コード例 #18
0
        private void ConvertCommand(CommandAst commandAst)
        {
            Command command = new Command(this.GetCommandName(commandAst.CommandElements[0]), false, this._createLocalScope);

            if (commandAst.Redirections.Count > 0)
            {
                PipelineResultTypes all;
                PipelineResultTypes output = PipelineResultTypes.Output;
                switch (commandAst.Redirections[0].FromStream)
                {
                case RedirectionStream.All:
                    all = PipelineResultTypes.All;
                    break;

                case RedirectionStream.Error:
                    all = PipelineResultTypes.Error;
                    break;

                case RedirectionStream.Warning:
                    all = PipelineResultTypes.Warning;
                    break;

                case RedirectionStream.Verbose:
                    all = PipelineResultTypes.Verbose;
                    break;

                case RedirectionStream.Debug:
                    all = PipelineResultTypes.Debug;
                    break;

                default:
                    all = PipelineResultTypes.Error;
                    break;
                }
                command.MergeMyResults(all, output);
            }
            this._powershell.AddCommand(command);
            foreach (CommandElementAst ast in commandAst.CommandElements.Skip <CommandElementAst>(1))
            {
                ExpressionAst exprAst = ast as ExpressionAst;
                if (exprAst != null)
                {
                    VariableExpressionAst variableAst = null;
                    UsingExpressionAst    ast4        = ast as UsingExpressionAst;
                    if (ast4 != null)
                    {
                        variableAst = ast4.SubExpression as VariableExpressionAst;
                        if ((variableAst != null) && variableAst.Splatted)
                        {
                            IDictionary parameters = this._usingValues[ast4.RuntimeUsingIndex] as IDictionary;
                            if (parameters != null)
                            {
                                this._powershell.AddParameters(parameters);
                            }
                            else
                            {
                                IEnumerable enumerable = this._usingValues[ast4.RuntimeUsingIndex] as IEnumerable;
                                if (enumerable != null)
                                {
                                    foreach (object obj2 in enumerable)
                                    {
                                        this._powershell.AddArgument(obj2);
                                    }
                                }
                                else
                                {
                                    this._powershell.AddArgument(this._usingValues[ast4.RuntimeUsingIndex]);
                                }
                            }
                        }
                        else
                        {
                            this._powershell.AddArgument(this._usingValues[ast4.RuntimeUsingIndex]);
                        }
                    }
                    else
                    {
                        variableAst = ast as VariableExpressionAst;
                        if ((variableAst != null) && variableAst.Splatted)
                        {
                            this.GetSplattedVariable(variableAst);
                        }
                        else
                        {
                            object expressionValue;
                            ConstantExpressionAst ast5 = ast as ConstantExpressionAst;
                            if ((ast5 != null) && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(ast5.StaticType)))
                            {
                                string text = ast5.Extent.Text;
                                expressionValue = ast5.Value;
                                if (!text.Equals(ast5.Value.ToString(), StringComparison.Ordinal))
                                {
                                    expressionValue = ParserOps.WrappedNumber(expressionValue, text);
                                }
                            }
                            else
                            {
                                expressionValue = this.GetExpressionValue(exprAst);
                            }
                            this._powershell.AddArgument(expressionValue);
                        }
                    }
                }
                else
                {
                    this.AddParameter((CommandParameterAst)ast);
                }
            }
        }