public CommandInfo GetCommand(string commandName, CommandTypes type, object[] arguments)
        {
            CommandInfo result = null;

            try
            {
                CommandOrigin commandOrigin = CommandOrigin.Runspace;
                if (_cmdlet != null)
                {
                    commandOrigin = _cmdlet.CommandOrigin;
                }
                else if (_context != null)
                {
                    commandOrigin = _context.EngineSessionState.CurrentScope.ScopeOrigin;
                }

                result = CommandDiscovery.LookupCommandInfo(commandName, type, SearchResolutionOptions.None, commandOrigin, _context);

                if ((result != null) && (arguments != null) && (arguments.Length > 0))
                {
                    // We've been asked to retrieve dynamic parameters
                    if (result.ImplementsDynamicParameters)
                    {
                        result = result.CreateGetCommandCopy(arguments);
                    }
                }
            }
            catch (CommandNotFoundException) { }

            return(result);
        }
        /// <summary>
        /// Generates a HelpInfo PSObject from a CmdletInfo object.
        /// </summary>
        /// <param name="input">Command info.</param>
        /// <returns>HelpInfo PSObject.</returns>
        internal static PSObject GetPSObjectFromCmdletInfo(CommandInfo input)
        {
            // Create a copy of commandInfo for GetCommandCommand so that we can generate parameter
            // sets based on Dynamic Parameters (+ optional arguments)
            CommandInfo commandInfo = input.CreateGetCommandCopy(null);

            PSObject obj = new PSObject();

            obj.TypeNames.Clear();
            obj.TypeNames.Add(string.Format(CultureInfo.InvariantCulture, "{0}#{1}#command", DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp, commandInfo.ModuleName));
            obj.TypeNames.Add(string.Format(CultureInfo.InvariantCulture, "{0}#{1}", DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp, commandInfo.ModuleName));
            obj.TypeNames.Add(DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp);
            obj.TypeNames.Add("CmdletHelpInfo");
            obj.TypeNames.Add("HelpInfo");

            if (commandInfo is CmdletInfo)
            {
                CmdletInfo cmdletInfo = commandInfo as CmdletInfo;
                bool       common     = false;
                if (cmdletInfo.Parameters != null)
                {
                    common = HasCommonParameters(cmdletInfo.Parameters);
                }

                obj.Properties.Add(new PSNoteProperty("CommonParameters", common));
                AddDetailsProperties(obj, cmdletInfo.Name, cmdletInfo.Noun, cmdletInfo.Verb, TypeNameForDefaultHelp);
                AddSyntaxProperties(obj, cmdletInfo.Name, cmdletInfo.ParameterSets, common, TypeNameForDefaultHelp);
                AddParametersProperties(obj, cmdletInfo.Parameters, common, TypeNameForDefaultHelp);
                AddInputTypesProperties(obj, cmdletInfo.Parameters);
                AddRelatedLinksProperties(obj, commandInfo.CommandMetadata.HelpUri);

                try
                {
                    AddOutputTypesProperties(obj, cmdletInfo.OutputType);
                }
                catch (PSInvalidOperationException)
                {
                    AddOutputTypesProperties(obj, new ReadOnlyCollection <PSTypeName>(new List <PSTypeName>()));
                }

                AddAliasesProperties(obj, cmdletInfo.Name, cmdletInfo.Context);

                if (HasHelpInfoUri(cmdletInfo.Module, cmdletInfo.ModuleName))
                {
                    AddRemarksProperties(obj, cmdletInfo.Name, cmdletInfo.CommandMetadata.HelpUri);
                }
                else
                {
                    obj.Properties.Add(new PSNoteProperty("remarks", HelpDisplayStrings.None));
                }

                obj.Properties.Add(new PSNoteProperty("PSSnapIn", cmdletInfo.PSSnapIn));
            }
            else if (commandInfo is FunctionInfo)
            {
                FunctionInfo funcInfo = commandInfo as FunctionInfo;
                bool         common   = HasCommonParameters(funcInfo.Parameters);

                obj.Properties.Add(new PSNoteProperty("CommonParameters", common));
                AddDetailsProperties(obj, funcInfo.Name, string.Empty, string.Empty, TypeNameForDefaultHelp);
                AddSyntaxProperties(obj, funcInfo.Name, funcInfo.ParameterSets, common, TypeNameForDefaultHelp);
                AddParametersProperties(obj, funcInfo.Parameters, common, TypeNameForDefaultHelp);
                AddInputTypesProperties(obj, funcInfo.Parameters);
                AddRelatedLinksProperties(obj, funcInfo.CommandMetadata.HelpUri);

                try
                {
                    AddOutputTypesProperties(obj, funcInfo.OutputType);
                }
                catch (PSInvalidOperationException)
                {
                    AddOutputTypesProperties(obj, new ReadOnlyCollection <PSTypeName>(new List <PSTypeName>()));
                }

                AddAliasesProperties(obj, funcInfo.Name, funcInfo.Context);

                if (HasHelpInfoUri(funcInfo.Module, funcInfo.ModuleName))
                {
                    AddRemarksProperties(obj, funcInfo.Name, funcInfo.CommandMetadata.HelpUri);
                }
                else
                {
                    obj.Properties.Add(new PSNoteProperty("remarks", HelpDisplayStrings.None));
                }
            }

            obj.Properties.Add(new PSNoteProperty("alertSet", null));
            obj.Properties.Add(new PSNoteProperty("description", null));
            obj.Properties.Add(new PSNoteProperty("examples", null));
            obj.Properties.Add(new PSNoteProperty("Synopsis", commandInfo.Syntax));
            obj.Properties.Add(new PSNoteProperty("ModuleName", commandInfo.ModuleName));
            obj.Properties.Add(new PSNoteProperty("nonTerminatingErrors", string.Empty));
            obj.Properties.Add(new PSNoteProperty("xmlns:command", "http://schemas.microsoft.com/maml/dev/command/2004/10"));
            obj.Properties.Add(new PSNoteProperty("xmlns:dev", "http://schemas.microsoft.com/maml/dev/2004/10"));
            obj.Properties.Add(new PSNoteProperty("xmlns:maml", "http://schemas.microsoft.com/maml/2004/10"));

            return(obj);
        }
Exemple #3
0
        internal override IEnumerable <PSTypeName> GetInferredType(CompletionContext context)
        {
            PseudoBindingInfo iteratorVariable0 = new PseudoParameterBinder().DoPseudoParameterBinding(this, null, null, false);

            if (iteratorVariable0.CommandInfo != null)
            {
                AstParameterArgumentPair iteratorVariable1;
                string key = "Path";
                if (!iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1))
                {
                    key = "LiteralPath";
                    iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1);
                }
                CommandInfo commandInfo       = iteratorVariable0.CommandInfo;
                AstPair     iteratorVariable4 = iteratorVariable1 as AstPair;
                if ((iteratorVariable4 != null) && (iteratorVariable4.Argument is StringConstantExpressionAst))
                {
                    string str = ((StringConstantExpressionAst)iteratorVariable4.Argument).Value;
                    try
                    {
                        commandInfo = commandInfo.CreateGetCommandCopy(new string[] { "-" + key, str });
                    }
                    catch (InvalidOperationException)
                    {
                    }
                }
                CmdletInfo iteratorVariable5 = commandInfo as CmdletInfo;
                if (iteratorVariable5 != null)
                {
                    if (iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.NewObjectCommand", StringComparison.Ordinal))
                    {
                        AstParameterArgumentPair iteratorVariable6;
                        if (iteratorVariable0.BoundArguments.TryGetValue("TypeName", out iteratorVariable6))
                        {
                            AstPair iteratorVariable7 = iteratorVariable6 as AstPair;
                            if ((iteratorVariable7 != null) && (iteratorVariable7.Argument is StringConstantExpressionAst))
                            {
                                yield return(new PSTypeName(((StringConstantExpressionAst)iteratorVariable7.Argument).Value));
                            }
                        }
                        goto Label_0579;
                    }
                    if (iteratorVariable5.ImplementingType.Equals(typeof(WhereObjectCommand)) || iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.SortObjectCommand", StringComparison.Ordinal))
                    {
                        PipelineAst parent = this.Parent as PipelineAst;
                        if (parent != null)
                        {
                            int iteratorVariable9 = 0;
                            while (iteratorVariable9 < parent.PipelineElements.Count)
                            {
                                if (parent.PipelineElements[iteratorVariable9] == this)
                                {
                                    break;
                                }
                                iteratorVariable9++;
                            }
                            if (iteratorVariable9 > 0)
                            {
                                foreach (PSTypeName iteratorVariable10 in parent.PipelineElements[iteratorVariable9 - 1].GetInferredType(context))
                                {
                                    yield return(iteratorVariable10);
                                }
                            }
                        }
                        goto Label_0579;
                    }
                    if (iteratorVariable5.ImplementingType.Equals(typeof(ForEachObjectCommand)))
                    {
                        AstParameterArgumentPair iteratorVariable11;
                        if (iteratorVariable0.BoundArguments.TryGetValue("Begin", out iteratorVariable11))
                        {
                            foreach (PSTypeName iteratorVariable12 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                            {
                                yield return(iteratorVariable12);
                            }
                        }
                        if (iteratorVariable0.BoundArguments.TryGetValue("Process", out iteratorVariable11))
                        {
                            foreach (PSTypeName iteratorVariable13 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                            {
                                yield return(iteratorVariable13);
                            }
                        }
                        if (iteratorVariable0.BoundArguments.TryGetValue("End", out iteratorVariable11))
                        {
                            foreach (PSTypeName iteratorVariable14 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                            {
                                yield return(iteratorVariable14);
                            }
                        }
                    }
                }
                foreach (PSTypeName iteratorVariable15 in commandInfo.OutputType)
                {
                    yield return(iteratorVariable15);
                }
            }
Label_0579:
            yield break;
        }
        /// <summary>
        /// Generates a HelpInfo PSObject from a CmdletInfo object
        /// </summary>
        /// <param name="input">command info</param>
        /// <returns>HelpInfo PSObject</returns>
        internal static PSObject GetPSObjectFromCmdletInfo(CommandInfo input)
        {
            // Create a copy of commandInfo for GetCommandCommand so that we can generate parameter
            // sets based on Dynamic Parameters (+ optional arguments)
            CommandInfo commandInfo = input.CreateGetCommandCopy(null);

            PSObject obj = new PSObject();

            obj.TypeNames.Clear();
            obj.TypeNames.Add(String.Format(CultureInfo.InvariantCulture, "{0}#{1}#command", DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp, commandInfo.ModuleName));
            obj.TypeNames.Add(String.Format(CultureInfo.InvariantCulture, "{0}#{1}", DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp, commandInfo.ModuleName));
            obj.TypeNames.Add(DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp);
            obj.TypeNames.Add("CmdletHelpInfo");
            obj.TypeNames.Add("HelpInfo");

            if (commandInfo is CmdletInfo)
            {
                CmdletInfo cmdletInfo = commandInfo as CmdletInfo;
                bool common = false;
                bool commonWorkflow = false;
                if (cmdletInfo.Parameters != null)
                {
                    common = HasCommonParameters(cmdletInfo.Parameters);
                    commonWorkflow = ((cmdletInfo.CommandType & CommandTypes.Workflow) == CommandTypes.Workflow);
                }

                obj.Properties.Add(new PSNoteProperty("CommonParameters", common));
                obj.Properties.Add(new PSNoteProperty("WorkflowCommonParameters", commonWorkflow));
                AddDetailsProperties(obj, cmdletInfo.Name, cmdletInfo.Noun, cmdletInfo.Verb, TypeNameForDefaultHelp);
                AddSyntaxProperties(obj, cmdletInfo.Name, cmdletInfo.ParameterSets, common, commonWorkflow, TypeNameForDefaultHelp);
                AddParametersProperties(obj, cmdletInfo.Parameters, common, commonWorkflow, TypeNameForDefaultHelp);
                AddInputTypesProperties(obj, cmdletInfo.Parameters);
                AddRelatedLinksProperties(obj, commandInfo.CommandMetadata.HelpUri);

                try
                {
                    AddOutputTypesProperties(obj, cmdletInfo.OutputType);
                }
                catch (PSInvalidOperationException)
                {
                    AddOutputTypesProperties(obj, new ReadOnlyCollection<PSTypeName>(new List<PSTypeName>()));
                }

                AddAliasesProperties(obj, cmdletInfo.Name, cmdletInfo.Context);

                if (HasHelpInfoUri(cmdletInfo.Module, cmdletInfo.ModuleName))
                {
                    AddRemarksProperties(obj, cmdletInfo.Name, cmdletInfo.CommandMetadata.HelpUri);
                }
                else
                {
                    obj.Properties.Add(new PSNoteProperty("remarks", HelpDisplayStrings.None));
                }

                obj.Properties.Add(new PSNoteProperty("PSSnapIn", cmdletInfo.PSSnapIn));
            }
            else if (commandInfo is FunctionInfo)
            {
                FunctionInfo funcInfo = commandInfo as FunctionInfo;
                bool common = HasCommonParameters(funcInfo.Parameters);
                bool commonWorkflow = ((commandInfo.CommandType & CommandTypes.Workflow) == CommandTypes.Workflow);

                obj.Properties.Add(new PSNoteProperty("CommonParameters", common));
                obj.Properties.Add(new PSNoteProperty("WorkflowCommonParameters", commonWorkflow));
                AddDetailsProperties(obj, funcInfo.Name, String.Empty, String.Empty, TypeNameForDefaultHelp);
                AddSyntaxProperties(obj, funcInfo.Name, funcInfo.ParameterSets, common, commonWorkflow, TypeNameForDefaultHelp);
                AddParametersProperties(obj, funcInfo.Parameters, common, commonWorkflow, TypeNameForDefaultHelp);
                AddInputTypesProperties(obj, funcInfo.Parameters);
                AddRelatedLinksProperties(obj, funcInfo.CommandMetadata.HelpUri);

                try
                {
                    AddOutputTypesProperties(obj, funcInfo.OutputType);
                }
                catch (PSInvalidOperationException)
                {
                    AddOutputTypesProperties(obj, new ReadOnlyCollection<PSTypeName>(new List<PSTypeName>()));
                }

                AddAliasesProperties(obj, funcInfo.Name, funcInfo.Context);

                if (HasHelpInfoUri(funcInfo.Module, funcInfo.ModuleName))
                {
                    AddRemarksProperties(obj, funcInfo.Name, funcInfo.CommandMetadata.HelpUri);
                }
                else
                {
                    obj.Properties.Add(new PSNoteProperty("remarks", HelpDisplayStrings.None));
                }
            }

            obj.Properties.Add(new PSNoteProperty("alertSet", null));
            obj.Properties.Add(new PSNoteProperty("description", null));
            obj.Properties.Add(new PSNoteProperty("examples", null));
            obj.Properties.Add(new PSNoteProperty("Synopsis", commandInfo.Syntax));
            obj.Properties.Add(new PSNoteProperty("ModuleName", commandInfo.ModuleName));
            obj.Properties.Add(new PSNoteProperty("nonTerminatingErrors", String.Empty));
            obj.Properties.Add(new PSNoteProperty("xmlns:command", "http://schemas.microsoft.com/maml/dev/command/2004/10"));
            obj.Properties.Add(new PSNoteProperty("xmlns:dev", "http://schemas.microsoft.com/maml/dev/2004/10"));
            obj.Properties.Add(new PSNoteProperty("xmlns:maml", "http://schemas.microsoft.com/maml/2004/10"));

            return obj;
        }
Exemple #5
0
        internal static PSObject GetPSObjectFromCmdletInfo(CommandInfo input)
        {
            CommandInfo info = input.CreateGetCommandCopy(null);
            PSObject    obj2 = new PSObject();

            obj2.TypeNames.Clear();
            obj2.TypeNames.Add(string.Format(CultureInfo.InvariantCulture, "{0}#{1}#command", new object[] { TypeNameForDefaultHelp, info.ModuleName }));
            obj2.TypeNames.Add(string.Format(CultureInfo.InvariantCulture, "{0}#{1}", new object[] { TypeNameForDefaultHelp, info.ModuleName }));
            obj2.TypeNames.Add(TypeNameForDefaultHelp);
            obj2.TypeNames.Add("CmdletHelpInfo");
            obj2.TypeNames.Add("HelpInfo");
            if (info is CmdletInfo)
            {
                CmdletInfo info2 = info as CmdletInfo;
                bool       flag  = false;
                bool       flag2 = false;
                if (info2.Parameters != null)
                {
                    flag  = HasCommonParameters(info2.Parameters);
                    flag2 = (info2.CommandType & CommandTypes.Workflow) == CommandTypes.Workflow;
                }
                obj2.Properties.Add(new PSNoteProperty("CommonParameters", flag));
                obj2.Properties.Add(new PSNoteProperty("WorkflowCommonParameters", flag2));
                AddDetailsProperties(obj2, info2.Name, info2.Noun, info2.Verb, TypeNameForDefaultHelp, null);
                AddSyntaxProperties(obj2, info2.Name, info2.ParameterSets, flag, flag2, TypeNameForDefaultHelp);
                AddParametersProperties(obj2, info2.Parameters, flag, flag2, TypeNameForDefaultHelp);
                AddInputTypesProperties(obj2, info2.Parameters);
                AddRelatedLinksProperties(obj2, info.CommandMetadata.HelpUri);
                try
                {
                    AddOutputTypesProperties(obj2, info2.OutputType);
                }
                catch (PSInvalidOperationException)
                {
                    AddOutputTypesProperties(obj2, new ReadOnlyCollection <PSTypeName>(new List <PSTypeName>()));
                }
                AddAliasesProperties(obj2, info2.Name, info2.Context);
                if (HasHelpInfoUri(info2.Module, info2.ModuleName))
                {
                    AddRemarksProperties(obj2, info2.Name, info2.CommandMetadata.HelpUri);
                }
                else
                {
                    obj2.Properties.Add(new PSNoteProperty("remarks", HelpDisplayStrings.None));
                }
                obj2.Properties.Add(new PSNoteProperty("PSSnapIn", info2.PSSnapIn));
            }
            else if (info is FunctionInfo)
            {
                FunctionInfo info3 = info as FunctionInfo;
                bool         flag3 = HasCommonParameters(info3.Parameters);
                bool         flag4 = (info.CommandType & CommandTypes.Workflow) == CommandTypes.Workflow;
                obj2.Properties.Add(new PSNoteProperty("CommonParameters", flag3));
                obj2.Properties.Add(new PSNoteProperty("WorkflowCommonParameters", flag4));
                AddDetailsProperties(obj2, info3.Name, string.Empty, string.Empty, TypeNameForDefaultHelp, null);
                AddSyntaxProperties(obj2, info3.Name, info3.ParameterSets, flag3, flag4, TypeNameForDefaultHelp);
                AddParametersProperties(obj2, info3.Parameters, flag3, flag4, TypeNameForDefaultHelp);
                AddInputTypesProperties(obj2, info3.Parameters);
                AddRelatedLinksProperties(obj2, info3.CommandMetadata.HelpUri);
                try
                {
                    AddOutputTypesProperties(obj2, info3.OutputType);
                }
                catch (PSInvalidOperationException)
                {
                    AddOutputTypesProperties(obj2, new ReadOnlyCollection <PSTypeName>(new List <PSTypeName>()));
                }
                AddAliasesProperties(obj2, info3.Name, info3.Context);
                if (HasHelpInfoUri(info3.Module, info3.ModuleName))
                {
                    AddRemarksProperties(obj2, info3.Name, info3.CommandMetadata.HelpUri);
                }
                else
                {
                    obj2.Properties.Add(new PSNoteProperty("remarks", HelpDisplayStrings.None));
                }
            }
            obj2.Properties.Add(new PSNoteProperty("alertSet", null));
            obj2.Properties.Add(new PSNoteProperty("description", null));
            obj2.Properties.Add(new PSNoteProperty("examples", null));
            obj2.Properties.Add(new PSNoteProperty("Synopsis", info.Syntax));
            obj2.Properties.Add(new PSNoteProperty("ModuleName", info.ModuleName));
            obj2.Properties.Add(new PSNoteProperty("nonTerminatingErrors", string.Empty));
            obj2.Properties.Add(new PSNoteProperty("xmlns:command", "http://schemas.microsoft.com/maml/dev/command/2004/10"));
            obj2.Properties.Add(new PSNoteProperty("xmlns:dev", "http://schemas.microsoft.com/maml/dev/2004/10"));
            obj2.Properties.Add(new PSNoteProperty("xmlns:maml", "http://schemas.microsoft.com/maml/2004/10"));
            return(obj2);
        }
        private bool IsCommandMatch(ref CommandInfo current)
        {
            bool flag = false;

            if (!this.IsDuplicate(current))
            {
                if ((current.CommandType & this.CommandType) != 0)
                {
                    flag = true;
                }
                if ((current.CommandType == CommandTypes.Cmdlet) || (((this.verbs.Length > 0) || (this.nouns.Length > 0)) && (((current.CommandType == CommandTypes.Function) || (current.CommandType == CommandTypes.Filter)) || ((current.CommandType == CommandTypes.Workflow) || (current.CommandType == CommandTypes.Alias)))))
                {
                    if (!this.IsNounVerbMatch(current))
                    {
                        flag = false;
                    }
                }
                else if (((this._modulePatterns != null) && (this._modulePatterns.Count > 0)) && !SessionStateUtilities.MatchesAnyWildcardPattern(current.ModuleName, this._modulePatterns, true))
                {
                    flag = false;
                }
                if (flag)
                {
                    if (this.ArgumentList != null)
                    {
                        AliasInfo info = current as AliasInfo;
                        if (info != null)
                        {
                            current = info.ResolvedCommand;
                            if (current == null)
                            {
                                return(false);
                            }
                        }
                        else if (!(current is CmdletInfo) && !(current is IScriptCommandInfo))
                        {
                            base.ThrowTerminatingError(new ErrorRecord(PSTraceSource.NewArgumentException("ArgumentList", "DiscoveryExceptions", "CommandArgsOnlyForSingleCmdlet", new object[0]), "CommandArgsOnlyForSingleCmdlet", ErrorCategory.InvalidArgument, current));
                        }
                    }
                    bool implementsDynamicParameters = false;
                    try
                    {
                        implementsDynamicParameters = current.ImplementsDynamicParameters;
                    }
                    catch (PSSecurityException)
                    {
                    }
                    catch (RuntimeException)
                    {
                    }
                    if (!implementsDynamicParameters)
                    {
                        return(flag);
                    }
                    try
                    {
                        CommandInfo info2 = current.CreateGetCommandCopy(this.ArgumentList);
                        if (this.ArgumentList != null)
                        {
                            ReadOnlyCollection <CommandParameterSetInfo> parameterSets = info2.ParameterSets;
                        }
                        current = info2;
                    }
                    catch (MetadataException exception)
                    {
                        base.WriteError(new ErrorRecord(exception, "GetCommandMetadataError", ErrorCategory.MetadataError, current));
                    }
                    catch (ParameterBindingException exception2)
                    {
                        if (!exception2.ErrorRecord.FullyQualifiedErrorId.StartsWith("GetDynamicParametersException", StringComparison.Ordinal))
                        {
                            throw;
                        }
                    }
                }
            }
            return(flag);
        }