Exemplo n.º 1
0
        private ScriptInfo GetNextBuiltinScript()
        {
            ScriptInfo scriptInfo = (ScriptInfo)null;

            if ((this.commandResolutionOptions & SearchResolutionOptions.CommandNameIsPattern) != SearchResolutionOptions.None)
            {
                if (this.matchingScript == null)
                {
                    Collection <string> collection       = new Collection <string>();
                    WildcardPattern     wildcardPattern1 = new WildcardPattern(this.commandName, WildcardOptions.IgnoreCase);
                    WildcardPattern     wildcardPattern2 = new WildcardPattern(this.commandName + ".ps1", WildcardOptions.IgnoreCase);
                    foreach (string key in this._context.CommandDiscovery.ScriptCache.Keys)
                    {
                        if (wildcardPattern1.IsMatch(key) || wildcardPattern2.IsMatch(key))
                        {
                            collection.Add(key);
                        }
                    }
                    this.matchingScript = collection.GetEnumerator();
                }
                if (!this.matchingScript.MoveNext())
                {
                    this.currentState   = CommandSearcher.SearchState.BuiltinScriptResolution;
                    this.matchingScript = (IEnumerator <string>)null;
                }
                else
                {
                    scriptInfo = this._context.CommandDiscovery.GetScriptInfo(this.matchingScript.Current);
                }
            }
            else
            {
                this.currentState = CommandSearcher.SearchState.BuiltinScriptResolution;
                scriptInfo        = this._context.CommandDiscovery.GetScriptInfo(this.commandName) ?? this._context.CommandDiscovery.GetScriptInfo(this.commandName + ".ps1");
            }
            if (scriptInfo != null)
            {
                CommandDiscovery.discoveryTracer.WriteLine("Script found: {0}", (object)scriptInfo.Name);
            }
            return(scriptInfo);
        }
Exemplo n.º 2
0
        private CommandInfo GetNextAlias()
        {
            CommandInfo commandInfo = (CommandInfo)null;

            if ((this.commandResolutionOptions & SearchResolutionOptions.ResolveAliasPatterns) != SearchResolutionOptions.None)
            {
                if (this.matchingAlias == null)
                {
                    Collection <AliasInfo> collection      = new Collection <AliasInfo>();
                    WildcardPattern        wildcardPattern = new WildcardPattern(this.commandName, WildcardOptions.IgnoreCase);
                    foreach (KeyValuePair <string, AliasInfo> keyValuePair in (IEnumerable <KeyValuePair <string, AliasInfo> >) this._context.EngineSessionState.GetAliasTable())
                    {
                        if (wildcardPattern.IsMatch(keyValuePair.Key))
                        {
                            collection.Add(keyValuePair.Value);
                        }
                    }
                    this.matchingAlias = collection.GetEnumerator();
                }
                if (!this.matchingAlias.MoveNext())
                {
                    this.currentState  = CommandSearcher.SearchState.AliasResolution;
                    this.matchingAlias = (IEnumerator <AliasInfo>)null;
                }
                else
                {
                    commandInfo = (CommandInfo)this.matchingAlias.Current;
                }
            }
            else
            {
                this.currentState = CommandSearcher.SearchState.AliasResolution;
                commandInfo       = (CommandInfo)this._context.EngineSessionState.GetAlias(this.commandName);
            }
            if (commandInfo != null)
            {
                CommandDiscovery.discoveryTracer.WriteLine("Alias found: {0}  {1}", (object)commandInfo.Name, (object)commandInfo.Definition);
            }
            return(commandInfo);
        }
Exemplo n.º 3
0
 public void Reset()
 {
     if (this._commandOrigin == CommandOrigin.Runspace)
     {
         if (this._context.EngineSessionState.Applications.Count == 0)
         {
             this.commandTypes &= ~CommandTypes.Application;
         }
         if (this._context.EngineSessionState.Scripts.Count == 0)
         {
             this.commandTypes &= ~CommandTypes.ExternalScript;
         }
     }
     if (this.pathSearcher != null)
     {
         this.pathSearcher.Reset();
     }
     this._currentMatch  = (CommandInfo)null;
     this.currentState   = CommandSearcher.SearchState.Reset;
     this.matchingAlias  = (IEnumerator <AliasInfo>)null;
     this.matchingCmdlet = (IEnumerator <CmdletInfo>)null;
     this.matchingScript = (IEnumerator <string>)null;
 }
Exemplo n.º 4
0
        private CommandInfo ProcessQualifiedFileSystemState()
        {
            try
            {
                this.setupPathSearcher();
            }
            catch (ArgumentException ex)
            {
                this.currentState = CommandSearcher.SearchState.NoMoreMatches;
                throw;
            }
            catch (PathTooLongException ex)
            {
                this.currentState = CommandSearcher.SearchState.NoMoreMatches;
                throw;
            }
            CommandInfo commandInfo = (CommandInfo)null;

            this.currentState = CommandSearcher.SearchState.PathSearch;
            if (this.canDoPathLookup)
            {
                try
                {
                    for (; commandInfo == null; commandInfo = this.GetInfoFromPath(((IEnumerator <string>) this.pathSearcher).Current))
                    {
                        if (!this.pathSearcher.MoveNext())
                        {
                            break;
                        }
                    }
                }
                catch (InvalidOperationException ex)
                {
                }
            }
            return(commandInfo);
        }
Exemplo n.º 5
0
        private CommandInfo GetNextFunction()
        {
            CommandInfo commandInfo = (CommandInfo)null;

            if ((this.commandResolutionOptions & SearchResolutionOptions.ResolveFunctionPatterns) != SearchResolutionOptions.None)
            {
                if (this.matchingFunctionEnumerator == null)
                {
                    Collection <CommandInfo> collection      = new Collection <CommandInfo>();
                    WildcardPattern          wildcardPattern = new WildcardPattern(this.commandName, WildcardOptions.IgnoreCase);
                    foreach (DictionaryEntry dictionaryEntry in this._context.EngineSessionState.GetFunctionTable())
                    {
                        if (wildcardPattern.IsMatch((string)dictionaryEntry.Key))
                        {
                            collection.Add((CommandInfo)dictionaryEntry.Value);
                        }
                    }
                    this.matchingFunctionEnumerator = collection.GetEnumerator();
                }
                if (!this.matchingFunctionEnumerator.MoveNext())
                {
                    this.currentState = CommandSearcher.SearchState.FunctionResolution;
                    this.matchingFunctionEnumerator = (IEnumerator <CommandInfo>)null;
                }
                else
                {
                    commandInfo = this.matchingFunctionEnumerator.Current;
                }
            }
            else
            {
                this.currentState = CommandSearcher.SearchState.FunctionResolution;
                commandInfo       = this.GetFunction(this.commandName);
            }
            return(commandInfo);
        }
Exemplo n.º 6
0
        public bool MoveNext()
        {
            bool flag1 = false;

            this._currentMatch = (CommandInfo)null;
            if (this.currentState == CommandSearcher.SearchState.Reset)
            {
                this._currentMatch = this.ProcessResetState();
                if (this._currentMatch != null && SessionState.IsVisible(this._commandOrigin, this._currentMatch))
                {
                    flag1 = true;
                    goto label_48;
                }
                else
                {
                    this.currentState = CommandSearcher.SearchState.AliasResolution;
                }
            }
            if (this.currentState == CommandSearcher.SearchState.AliasResolution)
            {
                this._currentMatch = this.ProcessAliasState();
                if (this._currentMatch != null)
                {
                    flag1 = true;
                    goto label_48;
                }
                else
                {
                    this.currentState = CommandSearcher.SearchState.FunctionResolution;
                }
            }
            if (this.currentState == CommandSearcher.SearchState.FunctionResolution)
            {
                this._currentMatch = this.ProcessFunctionState();
                if (this._currentMatch != null)
                {
                    flag1 = true;
                    goto label_48;
                }
                else
                {
                    this.currentState = CommandSearcher.SearchState.CmdletResolution;
                }
            }
            if (this.currentState == CommandSearcher.SearchState.CmdletResolution)
            {
                this._currentMatch = this.ProcessCmdletState();
                if (this._currentMatch != null)
                {
                    flag1 = true;
                    goto label_48;
                }
                else
                {
                    this.currentState = CommandSearcher.SearchState.BuiltinScriptResolution;
                }
            }
            if (this.currentState == CommandSearcher.SearchState.BuiltinScriptResolution)
            {
                if ((this.commandTypes & (CommandTypes.ExternalScript | CommandTypes.Application)) == (CommandTypes)0)
                {
                    flag1 = false;
                    goto label_48;
                }
                else
                {
                    if (this._commandOrigin == CommandOrigin.Runspace && this.commandName.IndexOfAny(this._pathSeparators) >= 0)
                    {
                        bool flag2 = false;
                        if (this._context.EngineSessionState.Applications.Count == 1 && this._context.EngineSessionState.Applications[0].Equals("*", StringComparison.OrdinalIgnoreCase) || this._context.EngineSessionState.Scripts.Count == 1 && this._context.EngineSessionState.Scripts[0].Equals("*", StringComparison.OrdinalIgnoreCase))
                        {
                            flag2 = true;
                        }
                        else
                        {
                            foreach (string application in this._context.EngineSessionState.Applications)
                            {
                                if (this.checkPath(application, this.commandName))
                                {
                                    flag2 = true;
                                    break;
                                }
                            }
                            if (!flag2)
                            {
                                foreach (string script in this._context.EngineSessionState.Scripts)
                                {
                                    if (this.checkPath(script, this.commandName))
                                    {
                                        flag2 = true;
                                        break;
                                    }
                                }
                            }
                        }
                        if (!flag2)
                        {
                            flag1 = false;
                            goto label_48;
                        }
                    }
                    this.currentState  = CommandSearcher.SearchState.PowerShellPathResolution;
                    this._currentMatch = this.ProcessBuiltinScriptState();
                    if (this._currentMatch != null)
                    {
                        this.currentState = CommandSearcher.SearchState.QualifiedFileSystemPath;
                        flag1             = true;
                        goto label_48;
                    }
                }
            }
            if (this.currentState == CommandSearcher.SearchState.PowerShellPathResolution)
            {
                this.currentState  = CommandSearcher.SearchState.QualifiedFileSystemPath;
                this._currentMatch = this.ProcessPathResolutionState();
                if (this._currentMatch != null)
                {
                    flag1 = true;
                    goto label_48;
                }
            }
            if (this.currentState == CommandSearcher.SearchState.QualifiedFileSystemPath || this.currentState == CommandSearcher.SearchState.PathSearch)
            {
                this._currentMatch = this.ProcessQualifiedFileSystemState();
                if (this._currentMatch != null)
                {
                    flag1 = true;
                    goto label_48;
                }
            }
            if (this.currentState == CommandSearcher.SearchState.PathSearch)
            {
                this.currentState  = CommandSearcher.SearchState.PowerShellRelativePath;
                this._currentMatch = this.ProcessPathSearchState();
                if (this._currentMatch != null)
                {
                    flag1 = true;
                }
            }
label_48:
            return(flag1);
        }
Exemplo n.º 7
0
        private CmdletInfo GetNextCmdlet()
        {
            CmdletInfo result = (CmdletInfo)null;

            if (this.matchingCmdlet == null)
            {
                Collection <CmdletInfo> matchingCmdlets;
                if ((this.commandResolutionOptions & SearchResolutionOptions.CommandNameIsPattern) != SearchResolutionOptions.None)
                {
                    matchingCmdlets = new Collection <CmdletInfo>();
                    PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(this.commandName);
                    if (instance == null)
                    {
                        return(result);
                    }
                    WildcardPattern wildcardPattern = new WildcardPattern(instance.ShortName, WildcardOptions.IgnoreCase);
                    Dictionary <string, List <CmdletInfo> > cmdletCache = this._context.EngineSessionState.CmdletCache;
                    while (true)
                    {
                        lock (cmdletCache)
                        {
                            foreach (List <CmdletInfo> cmdletInfoList in cmdletCache.Values)
                            {
                                foreach (CmdletInfo cmdletInfo in cmdletInfoList)
                                {
                                    if (wildcardPattern.IsMatch(cmdletInfo.Name) && (string.IsNullOrEmpty(instance.PSSnapInName) || instance.PSSnapInName.Equals(cmdletInfo.ModuleName, StringComparison.OrdinalIgnoreCase)))
                                    {
                                        matchingCmdlets.Add(cmdletInfo);
                                    }
                                }
                            }
                        }
                        if (cmdletCache != this._context.TopLevelSessionState.CmdletCache)
                        {
                            cmdletCache = this._context.TopLevelSessionState.CmdletCache;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    matchingCmdlets = this._context.CommandDiscovery.GetCmdletInfo(this.commandName);
                    if (matchingCmdlets.Count > 1)
                    {
                        if ((this.commandResolutionOptions & SearchResolutionOptions.ReturnFirstDuplicateCmdletName) != SearchResolutionOptions.None)
                        {
                            this.matchingCmdlet = matchingCmdlets.GetEnumerator();
                            while (this.matchingCmdlet.MoveNext())
                            {
                                if (result == null)
                                {
                                    result = this.matchingCmdlet.Current;
                                }
                            }
                            return(this.traceResult(result));
                        }
                        if ((this.commandResolutionOptions & SearchResolutionOptions.AllowDuplicateCmdletNames) == SearchResolutionOptions.None)
                        {
                            throw this.NewAmbiguousCmdletName(this.commandName, matchingCmdlets);
                        }
                    }
                }
                this.matchingCmdlet = matchingCmdlets.GetEnumerator();
            }
            if (!this.matchingCmdlet.MoveNext())
            {
                this.currentState   = CommandSearcher.SearchState.CmdletResolution;
                this.matchingCmdlet = (IEnumerator <CmdletInfo>)null;
            }
            else
            {
                result = this.matchingCmdlet.Current;
            }
            return(this.traceResult(result));
        }