Exemple #1
0
        private string GetCommandName(CommandElementAst commandNameAst)
        {
            string        name;
            ExpressionAst exprAst = commandNameAst as ExpressionAst;

            if (exprAst != null)
            {
                object expressionValue = this.GetExpressionValue(exprAst);
                if (expressionValue == null)
                {
                    ScriptBlockToPowerShellChecker.ThrowError(new ScriptBlockToPowerShellNotSupportedException("CantConvertWithScriptBlockInvocation", null, AutomationExceptions.CantConvertWithScriptBlockInvocation, new object[0]), exprAst);
                }
                if (expressionValue is CommandInfo)
                {
                    name = ((CommandInfo)expressionValue).Name;
                }
                else
                {
                    name = expressionValue as string;
                }
            }
            else
            {
                name = commandNameAst.Extent.Text;
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ScriptBlockToPowerShellNotSupportedException("CantConvertWithScriptBlockInvocation", null, AutomationExceptions.CantConvertWithScriptBlockInvocation, new object[0]);
            }
            return(name);
        }
Exemple #2
0
        private string GetCommandName(CommandElementAst commandNameAst, bool isTrustedInput)
        {
            var    exprAst = commandNameAst as ExpressionAst;
            string commandName;

            if (exprAst != null)
            {
                var value = GetExpressionValue(exprAst, isTrustedInput);
                if (value == null)
                {
                    ScriptBlockToPowerShellChecker.ThrowError(
                        new ScriptBlockToPowerShellNotSupportedException(
                            "CantConvertWithScriptBlockInvocation", null, AutomationExceptions.CantConvertWithScriptBlockInvocation),
                        exprAst);
                }

                if (value is CommandInfo)
                {
                    commandName = ((CommandInfo)value).Name;
                }
                else
                {
                    commandName = value as string;
                }
            }
            else
            {
                // If this assertion fires, the command name is determined incorrectly.
                Diagnostics.Assert(commandNameAst is CommandParameterAst, "Unexpected element not handled correctly.");
                commandName = commandNameAst.Extent.Text;
            }

            if (string.IsNullOrWhiteSpace(commandName))
            {
                // TODO: could use a better error here
                throw new ScriptBlockToPowerShellNotSupportedException(
                          "CantConvertWithScriptBlockInvocation",
                          null,
                          AutomationExceptions.CantConvertWithScriptBlockInvocation);
            }

            return(commandName);
        }