Ejemplo n.º 1
0
        public IEnumerable <PSObject> RunCmdlet(string cmdlet, bool throwExceptionOnError = true)
        {
            ScriptParseResult scriptParseResult = this.cmdletValidator.ParseCmdletScript(cmdlet);

            if (scriptParseResult.IsSuccessful)
            {
                return(this.ExecuteCmdlet(scriptParseResult.Command, scriptParseResult.Parameters, throwExceptionOnError));
            }
            if (throwExceptionOnError)
            {
                throw new CmdletExecutionException(string.Format("Command passed {0} failed validation", cmdlet));
            }
            return(new List <PSObject>());
        }
Ejemplo n.º 2
0
        public ScriptParseResult ParseCmdletScript(string cmdlet)
        {
            ScriptParseResult scriptParseResult = new ScriptParseResult();

            Token[]      array  = null;
            ParseError[] array2 = null;
            Parser.ParseInput(cmdlet, out array, out array2);
            if (array2 != null && array2.Count <ParseError>() > 0)
            {
                TaskLogger.Log(Strings.CmdletValidatorParseErrors(string.Join(", ", from error in array2
                                                                              select error.Message)));
                return(scriptParseResult);
            }
            int num = 0;

            if (array == null || array[0].TokenFlags != TokenFlags.CommandName)
            {
                TaskLogger.Log(Strings.CmdletValidatorParseErrors("No valid command specified"));
                return(scriptParseResult);
            }
            if (array.Any((Token token) => (token.TokenFlags & TokenFlags.SpecialOperator) != TokenFlags.None || (token.TokenFlags & TokenFlags.TypeName) != TokenFlags.None))
            {
                TaskLogger.Log(Strings.CmdletValidatorParseErrors("Special Operator and types not supported"));
                return(scriptParseResult);
            }
            scriptParseResult.Command = array[num].Text;
            if (!this.AllowedCommands.Contains(scriptParseResult.Command))
            {
                TaskLogger.Log(Strings.CmdletValidatorParseErrors(string.Format("Command {0} not allowed", scriptParseResult.Command)));
                return(scriptParseResult);
            }
            List <Token> list = null;
            string       text = null;
            int          num2 = array.Count <Token>();

            try
            {
                while (++num < num2 && array[num].Kind != TokenKind.EndOfInput)
                {
                    if (array[num].Kind == TokenKind.Parameter)
                    {
                        CmdletValidator.AddParameter(text, list, scriptParseResult.Parameters);
                        text = array[num].Text;
                        list = null;
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(text))
                        {
                            TaskLogger.Log(Strings.CmdletValidatorParseErrors(string.Format("Invalid token {0} of kind {1}, {2} encountered without a parameter", array[num].Text, array[num].Kind, array[num].TokenFlags)));
                            return(scriptParseResult);
                        }
                        if (list == null)
                        {
                            list = new List <Token>();
                        }
                        list.Add(array[num]);
                    }
                }
                CmdletValidator.AddParameter(text, list, scriptParseResult.Parameters);
                scriptParseResult.IsSuccessful = this.Validate(scriptParseResult.Command, scriptParseResult.Parameters);
            }
            catch (InvalidOperationException ex)
            {
                TaskLogger.Log(Strings.CmdletValidatorParseErrors(ex.ToString()));
            }
            return(scriptParseResult);
        }