예제 #1
0
        /// <summary>
        /// Returns all cmdlets whose names match the pattern...
        /// </summary>
        /// <returns>A list of CmdletInfo objects...</returns>
        public List <CmdletInfo> GetCmdlets(string pattern)
        {
            if (pattern == null)
            {
                throw PSTraceSource.NewArgumentNullException(nameof(pattern));
            }

            List <CmdletInfo> cmdlets = new List <CmdletInfo>();

            CmdletInfo current = null;

            CommandSearcher searcher = new CommandSearcher(
                pattern,
                SearchResolutionOptions.CommandNameIsPattern,
                CommandTypes.Cmdlet,
                _context);

            while (true)
            {
                try
                {
                    if (!searcher.MoveNext())
                    {
                        break;
                    }
                }
                catch (ArgumentException)
                {
                    continue;
                }
                catch (PathTooLongException)
                {
                    continue;
                }
                catch (FileLoadException)
                {
                    continue;
                }
                catch (MetadataException)
                {
                    continue;
                }
                catch (FormatException)
                {
                    continue;
                }

                current = ((IEnumerator)searcher).Current as CmdletInfo;
                if (current != null)
                {
                    cmdlets.Add(current);
                }
            }

            return(cmdlets);
        }
예제 #2
0
        internal override IEnumerable <HelpInfo> ExactMatchHelp(HelpRequest helpRequest)
        {
            int             iteratorVariable0            = 0;
            string          target                       = helpRequest.Target;
            Hashtable       iteratorVariable2            = new Hashtable(StringComparer.OrdinalIgnoreCase);
            CommandSearcher commandSearcherForExactMatch = this.GetCommandSearcherForExactMatch(target, this._context);

            Label_PostSwitchInIterator :;
            while (commandSearcherForExactMatch.MoveNext())
            {
                CommandInfo current = commandSearcherForExactMatch.Current;
                if (SessionState.IsVisible(helpRequest.CommandOrigin, current))
                {
                    CmdletInfo cmdletInfo = current as CmdletInfo;
                    HelpInfo   helpInfo   = null;
                    string     key        = null;
                    if (cmdletInfo != null)
                    {
                        helpInfo = this.GetHelpInfo(cmdletInfo, true);
                        key      = cmdletInfo.FullName;
                    }
                    else
                    {
                        IScriptCommandInfo scriptCommandInfo = current as IScriptCommandInfo;
                        if (scriptCommandInfo != null)
                        {
                            key      = current.Name;
                            helpInfo = this.GetHelpInfo(scriptCommandInfo, true, false);
                        }
                    }
                    if ((helpInfo != null) && (key != null))
                    {
                        if ((helpInfo.ForwardHelpCategory == helpRequest.HelpCategory) && helpInfo.ForwardTarget.Equals(helpRequest.Target, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new PSInvalidOperationException(HelpErrors.CircularDependencyInHelpForwarding);
                        }
                        if (!iteratorVariable2.ContainsKey(key) && Match(helpInfo, helpRequest, current))
                        {
                            iteratorVariable0++;
                            iteratorVariable2.Add(key, null);
                            yield return(helpInfo);

                            if ((iteratorVariable0 >= helpRequest.MaxResults) && (helpRequest.MaxResults > 0))
                            {
                                break;
                            }
                            goto Label_PostSwitchInIterator;
                        }
                    }
                }
            }
        }
예제 #3
0
        internal IEnumerable <CommandInfo> GetCommands(string name, CommandTypes commandTypes, SearchResolutionOptions options, CommandOrigin?commandOrigin = null)
        {
            CommandSearcher searcher = new CommandSearcher(
                name,
                options,
                commandTypes,
                _context);

            if (commandOrigin != null)
            {
                searcher.CommandOrigin = commandOrigin.Value;
            }

            while (true)
            {
                try
                {
                    if (!searcher.MoveNext())
                    {
                        break;
                    }
                }
                catch (ArgumentException)
                {
                    continue;
                }
                catch (PathTooLongException)
                {
                    continue;
                }
                catch (FileLoadException)
                {
                    continue;
                }
                catch (MetadataException)
                {
                    continue;
                }
                catch (FormatException)
                {
                    continue;
                }

                CommandInfo commandInfo = ((IEnumerator)searcher).Current as CommandInfo;
                if (commandInfo != null)
                {
                    yield return(commandInfo);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Returns the CmdletInfo object that corresponds to the name argument.
        /// </summary>
        /// <param name="commandName">The name of the cmdlet to look for.</param>
        /// <param name="context">The execution context instance to use for lookup.</param>
        /// <returns>The cmdletInfo object if found, null otherwise.</returns>
        internal static CmdletInfo GetCmdlet(string commandName, ExecutionContext context)
        {
            CmdletInfo current = null;

            CommandSearcher searcher = new CommandSearcher(
                commandName,
                SearchResolutionOptions.None,
                CommandTypes.Cmdlet,
                context);

            while (true)
            {
                try
                {
                    if (!searcher.MoveNext())
                    {
                        break;
                    }
                }
                catch (ArgumentException)
                {
                    continue;
                }
                catch (PathTooLongException)
                {
                    continue;
                }
                catch (FileLoadException)
                {
                    continue;
                }
                catch (MetadataException)
                {
                    continue;
                }
                catch (FormatException)
                {
                    continue;
                }

                current = ((IEnumerator)searcher).Current as CmdletInfo;
            }

            return(current);
        }
        public IEnumerable <CommandInfo> GetCommands(string name, CommandTypes commandTypes, bool nameIsPattern)
        {
            if (name == null)
            {
                throw PSTraceSource.NewArgumentNullException("name");
            }
            CommandSearcher iteratorVariable0 = new CommandSearcher(name, nameIsPattern ? (SearchResolutionOptions.CommandNameIsPattern | SearchResolutionOptions.ResolveFunctionPatterns | SearchResolutionOptions.ResolveAliasPatterns) : SearchResolutionOptions.None, commandTypes, this._context);

Label_0066:
            try
            {
                if (!iteratorVariable0.MoveNext())
                {
                    goto Label_00C2;
                }
            }
            catch (ArgumentException)
            {
                goto Label_0066;
            }
            catch (PathTooLongException)
            {
                goto Label_0066;
            }
            catch (FileLoadException)
            {
                goto Label_0066;
            }
            catch (MetadataException)
            {
                goto Label_0066;
            }
            catch (FormatException)
            {
                goto Label_0066;
            }
            CommandInfo current = ((IEnumerator)iteratorVariable0).Current as CommandInfo;

            if (current != null)
            {
                yield return(current);
            }
            goto Label_0066;
            Label_00C2 :;
        }
        public List <CmdletInfo> GetCmdlets(string pattern)
        {
            if (pattern == null)
            {
                throw PSTraceSource.NewArgumentNullException("pattern");
            }
            List <CmdletInfo> list     = new List <CmdletInfo>();
            CmdletInfo        item     = null;
            CommandSearcher   searcher = new CommandSearcher(pattern, SearchResolutionOptions.CommandNameIsPattern, CommandTypes.Cmdlet, this._context);

Label_0025:
            try
            {
                if (!searcher.MoveNext())
                {
                    return(list);
                }
            }
            catch (ArgumentException)
            {
                goto Label_0025;
            }
            catch (PathTooLongException)
            {
                goto Label_0025;
            }
            catch (FileLoadException)
            {
                goto Label_0025;
            }
            catch (MetadataException)
            {
                goto Label_0025;
            }
            catch (FormatException)
            {
                goto Label_0025;
            }
            item = ((IEnumerator)searcher).Current as CmdletInfo;
            if (item != null)
            {
                list.Add(item);
            }
            goto Label_0025;
        }
        internal static CmdletInfo GetCmdlet(string commandName, System.Management.Automation.ExecutionContext context)
        {
            CmdletInfo      current  = null;
            CommandSearcher searcher = new CommandSearcher(commandName, SearchResolutionOptions.None, CommandTypes.Cmdlet, context);

Label_000C:
            try
            {
                if (!searcher.MoveNext())
                {
                    return(current);
                }
            }
            catch (ArgumentException)
            {
                goto Label_000C;
            }
            catch (PathTooLongException)
            {
                goto Label_000C;
            }
            catch (FileLoadException)
            {
                goto Label_000C;
            }
            catch (MetadataException)
            {
                goto Label_000C;
            }
            catch (FormatException)
            {
                goto Label_000C;
            }
            current = ((IEnumerator)searcher).Current as CmdletInfo;
            goto Label_000C;
        }
예제 #8
0
        /// <summary>
        /// Search an alias help target.
        /// </summary>
        /// <remarks>
        /// This will,
        ///     a. use _sessionState object to get a list of alias that match the target.
        ///     b. for each alias, retrieve help info as in ExactMatchHelp.
        /// </remarks>
        /// <param name="helpRequest">Help request object.</param>
        /// <param name="searchOnlyContent">
        /// If true, searches for pattern in the help content. Individual
        /// provider can decide which content to search in.
        ///
        /// If false, searches for pattern in the command names.
        /// </param>
        /// <returns>A IEnumerable of helpinfo object.</returns>
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            // aliases do not have help content...so doing nothing in that case
            if (!searchOnlyContent)
            {
                string    target    = helpRequest.Target;
                string    pattern   = target;
                Hashtable hashtable = new Hashtable(StringComparer.OrdinalIgnoreCase);

                if (!WildcardPattern.ContainsWildcardCharacters(target))
                {
                    pattern += "*";
                }

                WildcardPattern matcher = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase);
                IDictionary <string, AliasInfo> aliasTable = _sessionState.Internal.GetAliasTable();

                foreach (string name in aliasTable.Keys)
                {
                    if (matcher.IsMatch(name))
                    {
                        HelpRequest exactMatchHelpRequest = helpRequest.Clone();
                        exactMatchHelpRequest.Target = name;
                        // Duplicates??
                        foreach (HelpInfo helpInfo in ExactMatchHelp(exactMatchHelpRequest))
                        {
                            // Component/Role/Functionality match is done only for SearchHelp
                            // as "get-help * -category alias" should not forwad help to
                            // CommandHelpProvider..(ExactMatchHelp does forward help to
                            // CommandHelpProvider)
                            if (!Match(helpInfo, helpRequest))
                            {
                                continue;
                            }

                            if (hashtable.ContainsKey(name))
                            {
                                continue;
                            }

                            hashtable.Add(name, null);

                            yield return(helpInfo);
                        }
                    }
                }

                CommandSearcher searcher =
                    new CommandSearcher(
                        pattern,
                        SearchResolutionOptions.ResolveAliasPatterns, CommandTypes.Alias,
                        _context);

                while (searcher.MoveNext())
                {
                    CommandInfo current = ((IEnumerator <CommandInfo>)searcher).Current;

                    if (_context.CurrentPipelineStopping)
                    {
                        yield break;
                    }

                    AliasInfo alias = current as AliasInfo;

                    if (alias != null)
                    {
                        string      name = alias.Name;
                        HelpRequest exactMatchHelpRequest = helpRequest.Clone();
                        exactMatchHelpRequest.Target = name;

                        // Duplicates??
                        foreach (HelpInfo helpInfo in ExactMatchHelp(exactMatchHelpRequest))
                        {
                            // Component/Role/Functionality match is done only for SearchHelp
                            // as "get-help * -category alias" should not forwad help to
                            // CommandHelpProvider..(ExactMatchHelp does forward help to
                            // CommandHelpProvider)
                            if (!Match(helpInfo, helpRequest))
                            {
                                continue;
                            }

                            if (hashtable.ContainsKey(name))
                            {
                                continue;
                            }

                            hashtable.Add(name, null);

                            yield return(helpInfo);
                        }
                    }
                }

                foreach (CommandInfo current in ModuleUtils.GetMatchingCommands(pattern, _context, helpRequest.CommandOrigin))
                {
                    if (_context.CurrentPipelineStopping)
                    {
                        yield break;
                    }

                    AliasInfo alias = current as AliasInfo;

                    if (alias != null)
                    {
                        string name = alias.Name;

                        HelpInfo helpInfo = AliasHelpInfo.GetHelpInfo(alias);

                        if (hashtable.ContainsKey(name))
                        {
                            continue;
                        }

                        hashtable.Add(name, null);

                        yield return(helpInfo);
                    }
                }
            }
        }
예제 #9
0
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            string item = helpRequest.Target;
            Collection <string> iteratorVariable1 = new Collection <string>();
            WildcardPattern     pattern           = null;
            bool iteratorVariable3 = !WildcardPattern.ContainsWildcardCharacters(helpRequest.Target);

            if (!searchOnlyContent)
            {
                if (iteratorVariable3)
                {
                    if (item.IndexOf('-') >= 0)
                    {
                        iteratorVariable1.Add(item + "*");
                    }
                    else
                    {
                        iteratorVariable1.Add("*" + item + "*");
                    }
                }
                else
                {
                    iteratorVariable1.Add(item);
                }
            }
            else
            {
                iteratorVariable1.Add("*");
                string target = helpRequest.Target;
                if (iteratorVariable3)
                {
                    target = "*" + helpRequest.Target + "*";
                }
                pattern = new WildcardPattern(target, WildcardOptions.IgnoreCase | WildcardOptions.Compiled);
            }
            int       iteratorVariable4 = 0;
            Hashtable iteratorVariable5 = new Hashtable(StringComparer.OrdinalIgnoreCase);
            Hashtable iteratorVariable6 = new Hashtable(StringComparer.OrdinalIgnoreCase);

            foreach (string iteratorVariable7 in iteratorVariable1)
            {
                CommandSearcher commandSearcherForSearch = this.GetCommandSearcherForSearch(iteratorVariable7, this._context);
                while (commandSearcherForSearch.MoveNext())
                {
                    if (this._context.CurrentPipelineStopping)
                    {
                        break;
                    }
                    CommandInfo current    = commandSearcherForSearch.Current;
                    CmdletInfo  cmdletInfo = current as CmdletInfo;
                    HelpInfo    helpInfo   = null;
                    string      key        = null;
                    if (cmdletInfo != null)
                    {
                        helpInfo = this.GetHelpInfo(cmdletInfo, !iteratorVariable3);
                        key      = cmdletInfo.FullName;
                    }
                    else
                    {
                        IScriptCommandInfo scriptCommandInfo = current as IScriptCommandInfo;
                        if (scriptCommandInfo != null)
                        {
                            key      = current.Name;
                            helpInfo = this.GetHelpInfo(scriptCommandInfo, !iteratorVariable3, searchOnlyContent);
                        }
                    }
                    if (helpInfo != null)
                    {
                        if (!SessionState.IsVisible(helpRequest.CommandOrigin, current))
                        {
                            if (!iteratorVariable6.ContainsKey(key))
                            {
                                iteratorVariable6.Add(key, null);
                            }
                        }
                        else if ((!iteratorVariable5.ContainsKey(key) && Match(helpInfo, helpRequest, current)) && (!searchOnlyContent || helpInfo.MatchPatternInContent(pattern)))
                        {
                            iteratorVariable5.Add(key, null);
                            iteratorVariable4++;
                            yield return(helpInfo);

                            if ((iteratorVariable4 < helpRequest.MaxResults) || (helpRequest.MaxResults <= 0))
                            {
                                continue;
                            }
                            break;
                        }
                    }
                }
                if (this.HelpCategory == (System.Management.Automation.HelpCategory.Cmdlet | System.Management.Automation.HelpCategory.Alias))
                {
                    foreach (CommandInfo iteratorVariable13 in ModuleUtils.GetMatchingCommands(iteratorVariable7, this._context, helpRequest.CommandOrigin, false))
                    {
                        if (this._context.CurrentPipelineStopping)
                        {
                            break;
                        }
                        if (SessionState.IsVisible(helpRequest.CommandOrigin, iteratorVariable13))
                        {
                            CmdletInfo iteratorVariable14 = iteratorVariable13 as CmdletInfo;
                            HelpInfo   iteratorVariable15 = null;
                            string     fullName           = null;
                            if (iteratorVariable14 != null)
                            {
                                iteratorVariable15 = this.GetHelpInfo(iteratorVariable14, !iteratorVariable3);
                                fullName           = iteratorVariable14.FullName;
                            }
                            else
                            {
                                IScriptCommandInfo info2 = iteratorVariable13 as IScriptCommandInfo;
                                if (info2 != null)
                                {
                                    fullName           = iteratorVariable13.Name;
                                    iteratorVariable15 = this.GetHelpInfo(info2, !iteratorVariable3, searchOnlyContent);
                                }
                            }
                            if ((((iteratorVariable15 != null) && !iteratorVariable5.ContainsKey(fullName)) && (!iteratorVariable6.ContainsKey(fullName) && Match(iteratorVariable15, helpRequest, iteratorVariable13))) && (!searchOnlyContent || iteratorVariable15.MatchPatternInContent(pattern)))
                            {
                                iteratorVariable5.Add(fullName, null);
                                iteratorVariable4++;
                                yield return(iteratorVariable15);

                                if ((iteratorVariable4 >= helpRequest.MaxResults) && (helpRequest.MaxResults > 0))
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #10
0
        internal CommandInfo LookupCommandInfo(
            string commandName,
            CommandOrigin commandOrigin)
        {
            CommandInfo commandInfo    = (CommandInfo)null;
            string      commandName1   = commandName;
            Exception   innerException = (Exception)null;

            while (true)
            {
                CommandDiscovery.discoveryTracer.WriteLine("Looking up command: {0}", (object)commandName);
                if (!string.IsNullOrEmpty(commandName))
                {
                    CommandSearcher commandSearcher = new CommandSearcher(commandName, SearchResolutionOptions.AllowDuplicateCmdletNames, CommandTypes.All, this._context);
                    commandSearcher.CommandOrigin = commandOrigin;
                    try
                    {
                        if (!commandSearcher.MoveNext())
                        {
                            if (!commandName.Contains("-"))
                            {
                                CommandDiscovery.discoveryTracer.WriteLine("The command [{0}] was not found, trying again with get- prepended", (object)commandName);
                                commandName = "get" + (object)'-' + commandName;
                            }
                            else
                            {
                                goto label_13;
                            }
                        }
                        else
                        {
                            commandInfo = ((IEnumerator <CommandInfo>)commandSearcher).Current;
                            goto label_13;
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (PathTooLongException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (FileLoadException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (FormatException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                    catch (MetadataException ex)
                    {
                        innerException = (Exception)ex;
                        goto label_13;
                    }
                }
                else
                {
                    break;
                }
            }
            CommandDiscovery.discoveryTracer.TraceError("Command name empty or null");
label_13:
            if (commandInfo == null)
            {
                CommandDiscovery.discoveryTracer.TraceError("'{0}' is not recognized as a cmdlet, function, operable program or script file.", (object)commandName);
                CommandNotFoundException notFoundException = new CommandNotFoundException(commandName1, innerException, "CommandNotFoundException", new object[0]);
                CommandDiscovery.tracer.TraceException((Exception)notFoundException);
                throw notFoundException;
            }
            return(commandInfo);
        }
예제 #11
0
        internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent)
        {
            if (!searchOnlyContent)
            {
                string    target            = helpRequest.Target;
                string    pattern           = target;
                Hashtable iteratorVariable2 = new Hashtable(StringComparer.OrdinalIgnoreCase);
                if (!WildcardPattern.ContainsWildcardCharacters(target))
                {
                    pattern = pattern + "*";
                }
                WildcardPattern iteratorVariable3          = new WildcardPattern(pattern, WildcardOptions.IgnoreCase);
                IDictionary <string, AliasInfo> aliasTable = this._sessionState.Internal.GetAliasTable();
                foreach (string iteratorVariable5 in aliasTable.Keys)
                {
                    if (iteratorVariable3.IsMatch(iteratorVariable5))
                    {
                        HelpRequest iteratorVariable6 = helpRequest.Clone();
                        iteratorVariable6.Target = iteratorVariable5;
                        foreach (HelpInfo iteratorVariable7 in this.ExactMatchHelp(iteratorVariable6))
                        {
                            if (!Match(iteratorVariable7, helpRequest) || iteratorVariable2.ContainsKey(iteratorVariable5))
                            {
                                continue;
                            }
                            iteratorVariable2.Add(iteratorVariable5, null);
                            yield return(iteratorVariable7);
                        }
                    }
                }
                CommandSearcher iteratorVariable8 = new CommandSearcher(pattern, SearchResolutionOptions.ResolveAliasPatterns, CommandTypes.Alias, this._context);
                while (iteratorVariable8.MoveNext())
                {
                    CommandInfo current = iteratorVariable8.Current;
                    if (this._context.CurrentPipelineStopping)
                    {
                        goto Label_0423;
                    }
                    AliasInfo iteratorVariable10 = current as AliasInfo;
                    if (iteratorVariable10 != null)
                    {
                        string      name = iteratorVariable10.Name;
                        HelpRequest iteratorVariable12 = helpRequest.Clone();
                        iteratorVariable12.Target = name;
                        foreach (HelpInfo iteratorVariable13 in this.ExactMatchHelp(iteratorVariable12))
                        {
                            if (!Match(iteratorVariable13, helpRequest) || iteratorVariable2.ContainsKey(name))
                            {
                                continue;
                            }
                            iteratorVariable2.Add(name, null);
                            yield return(iteratorVariable13);
                        }
                    }
                }
                foreach (CommandInfo iteratorVariable14 in ModuleUtils.GetMatchingCommands(pattern, this._context, helpRequest.CommandOrigin, false))
                {
                    if (this._context.CurrentPipelineStopping)
                    {
                        break;
                    }
                    AliasInfo aliasInfo = iteratorVariable14 as AliasInfo;
                    if (aliasInfo != null)
                    {
                        string   key      = aliasInfo.Name;
                        HelpInfo helpInfo = AliasHelpInfo.GetHelpInfo(aliasInfo);
                        if (!iteratorVariable2.ContainsKey(key))
                        {
                            iteratorVariable2.Add(key, null);
                            yield return(helpInfo);
                        }
                    }
                }
            }
Label_0423:
            yield break;
        }