Esempio n. 1
0
        internal Collection <PSSnapInInfo> GetPSSnapIn(string pattern, bool searchRegistry)
        {
            bool flag = WildcardPattern.ContainsWildcardCharacters(pattern);

            if (!flag)
            {
                PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(pattern);
            }
            Collection <PSSnapInInfo> collection  = searchRegistry ? PSSnapInReader.ReadAll() : this.PSSnapIns;
            Collection <PSSnapInInfo> collection2 = new Collection <PSSnapInInfo>();

            if (collection != null)
            {
                if (!flag)
                {
                    foreach (PSSnapInInfo info in collection)
                    {
                        if (string.Equals(info.Name, pattern, StringComparison.OrdinalIgnoreCase))
                        {
                            collection2.Add(info);
                        }
                    }
                    return(collection2);
                }
                WildcardPattern pattern2 = new WildcardPattern(pattern, WildcardOptions.IgnoreCase);
                foreach (PSSnapInInfo info2 in collection)
                {
                    if (pattern2.IsMatch(info2.Name))
                    {
                        collection2.Add(info2);
                    }
                }
            }
            return(collection2);
        }
        /// <summary>
        /// Searches for mshsnapin in either current console or registry as determined
        /// by <paramref name="searchRegistry"/>.
        /// </summary>
        /// <param name="pattern">
        /// Id/WildcardPattern of the mshsnapin to search for. This can contain wildcard characters as
        /// represented by WildCardPattern.
        /// </param>
        /// <param name="searchRegistry">
        /// A boolean which determines whether to search in the current console or registry.
        /// </param>
        /// <returns>A collection of mshsnapininfo objects.</returns>
        /// <exception cref="PSArgumentException">
        /// 1.Unable to read registry entries for mshsnapins.
        /// 2.Pattern specified is not valid. If pattern doesnt contain
        /// wildcard characters, this function checks for the validity
        /// of the mshsnapin name.
        /// </exception>
        /// <exception cref="System.Security.SecurityException">
        /// Caller doesn't have permission to read keys.
        /// </exception>
        internal Collection <PSSnapInInfo> GetPSSnapIn(string pattern, bool searchRegistry)
        {
            // We want to improve the search speed by noting whether we want
            // to perform wildcard search.
            bool doWildCardSearch = WildcardPattern.ContainsWildcardCharacters(pattern);

            if (!doWildCardSearch)
            {
                // Verify PSSnapInID..
                // This will throw if it not a valid name
                PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(pattern);
            }

            // Build the list to search..If searchRegistry is true get all mshsnapins available
            // from the registry, otherwise get mshsnapins from the current console.
            Collection <PSSnapInInfo> listToSearch = searchRegistry ?
                                                     PSSnapInReader.ReadAll() : PSSnapIns;

            // Create a list to return..
            Collection <PSSnapInInfo> listToReturn = new Collection <PSSnapInInfo>();

            // If there is nothing to search..
            if (listToSearch == null)
            {
                return(listToReturn);
            }

            if (!doWildCardSearch)
            {
                // We are not doing wildcard search..
                foreach (PSSnapInInfo mshSnapIn in listToSearch)
                {
                    if (string.Equals(mshSnapIn.Name, pattern, StringComparison.OrdinalIgnoreCase))
                    {
                        listToReturn.Add(mshSnapIn);
                    }
                }
            }
            else
            {
                WildcardPattern matcher = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase);
                // We are doing WildCard search
                foreach (PSSnapInInfo mshSnapIn in listToSearch)
                {
                    if (matcher.IsMatch(mshSnapIn.Name))
                    {
                        listToReturn.Add(mshSnapIn);
                    }
                }
            }

            // return whatever we found..may be 0..
            return(listToReturn);
        }
Esempio n. 3
0
        protected internal Collection <PSSnapInInfo> GetSnapIns(string pattern)
        {
            if (this.Runspace != null)
            {
                if (pattern != null)
                {
                    return(this.Runspace.ConsoleInfo.GetPSSnapIn(pattern, this._shouldGetAll));
                }
                return(this.Runspace.ConsoleInfo.PSSnapIns);
            }
            WildcardPattern pattern2 = null;

            if (!string.IsNullOrEmpty(pattern))
            {
                if (!WildcardPattern.ContainsWildcardCharacters(pattern))
                {
                    PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(pattern);
                }
                pattern2 = new WildcardPattern(pattern, WildcardOptions.IgnoreCase);
            }
            Collection <PSSnapInInfo> collection = new Collection <PSSnapInInfo>();

            if (this._shouldGetAll)
            {
                foreach (PSSnapInInfo info in PSSnapInReader.ReadAll())
                {
                    if ((pattern2 == null) || pattern2.IsMatch(info.Name))
                    {
                        collection.Add(info);
                    }
                }
                return(collection);
            }
            List <CmdletInfo> cmdlets = base.InvokeCommand.GetCmdlets();
            Dictionary <PSSnapInInfo, bool> dictionary = new Dictionary <PSSnapInInfo, bool>();

            foreach (CmdletInfo info2 in cmdlets)
            {
                PSSnapInInfo pSSnapIn = info2.PSSnapIn;
                if ((pSSnapIn != null) && !dictionary.ContainsKey(pSSnapIn))
                {
                    dictionary.Add(pSSnapIn, true);
                }
            }
            foreach (PSSnapInInfo info4 in dictionary.Keys)
            {
                if ((pattern2 == null) || pattern2.IsMatch(info4.Name))
                {
                    collection.Add(info4);
                }
            }
            return(collection);
        }
Esempio n. 4
0
        protected override void ProcessRecord()
        {
            Collection <PSSnapInInfo> searchList = null;

            foreach (string str in this._pssnapins)
            {
                Exception           innerException = null;
                Collection <string> snapInList     = new Collection <string>();
                try
                {
                    if (WildcardPattern.ContainsWildcardCharacters(str))
                    {
                        if (searchList == null)
                        {
                            searchList = PSSnapInReader.ReadAll(PSVersionInfo.RegistryVersion1Key);
                        }
                        snapInList = base.SearchListForPattern(searchList, str);
                        if (snapInList.Count != 0)
                        {
                            goto Label_0088;
                        }
                        if (this._passThru)
                        {
                            base.WriteNonTerminatingError(str, "NoPSSnapInsFound", PSTraceSource.NewArgumentException(str, "MshSnapInCmdletResources", "NoPSSnapInsFound", new object[] { str }), ErrorCategory.InvalidArgument);
                        }
                        continue;
                    }
                    snapInList.Add(str);
Label_0088:
                    this.AddPSSnapIns(snapInList);
                }
                catch (PSArgumentException exception2)
                {
                    innerException = exception2;
                }
                catch (SecurityException exception3)
                {
                    innerException = exception3;
                }
                if (innerException != null)
                {
                    base.WriteNonTerminatingError(str, "AddPSSnapInRead", innerException, ErrorCategory.InvalidArgument);
                }
            }
        }
        /// <summary>
        /// Constructs PSSnapInfo objects as requested by the user and writes them to the
        /// output buffer.
        /// </summary>
        protected override void BeginProcessing()
        {
            if (_pssnapins != null)
            {
                foreach (string psSnapIn in _pssnapins)
                {
                    Exception exception = null;

                    try
                    {
                        Collection <PSSnapInInfo> psSnapInInfoList = GetSnapIns(psSnapIn);

                        // psSnapInInfoList wont be null..
                        Diagnostics.Assert(psSnapInInfoList != null, "ConsoleInfo.GetPSSnapIn returned null");
                        if (psSnapInInfoList.Count == 0)
                        {
                            WriteNonTerminatingError(psSnapIn, "NoPSSnapInsFound",
                                                     PSTraceSource.NewArgumentException(psSnapIn,
                                                                                        MshSnapInCmdletResources.NoPSSnapInsFound, psSnapIn),
                                                     ErrorCategory.InvalidArgument);

                            continue;
                        }

                        foreach (PSSnapInInfo pssnapinInfo in psSnapInInfoList)
                        {
                            // Load the pssnapin info properties that are localizable and redirected in the registry
                            pssnapinInfo.LoadIndirectResources(ResourceReader);
                            WriteObject(pssnapinInfo);
                        }
                    }
                    catch (System.Security.SecurityException se)
                    {
                        exception = se;
                    }
                    catch (PSArgumentException ae)
                    {
                        exception = ae;
                    }

                    if (exception != null)
                    {
                        WriteNonTerminatingError(psSnapIn, "GetPSSnapInRead", exception, ErrorCategory.InvalidArgument);
                    }
                }
            }
            else if (ShouldGetAll)
            {
                Exception exception = null;

                try
                {
                    Collection <PSSnapInInfo> psSnapInInfoList = PSSnapInReader.ReadAll();
                    foreach (PSSnapInInfo pssnapinInfo in psSnapInInfoList)
                    {
                        // Load the pssnapin info properties that are localizable and redirected in the registry
                        pssnapinInfo.LoadIndirectResources(ResourceReader);
                        WriteObject(pssnapinInfo);
                    }
                }
                catch (System.Security.SecurityException se)
                {
                    exception = se;
                }
                catch (PSArgumentException ae)
                {
                    exception = ae;
                }

                if (exception != null)
                {
                    WriteNonTerminatingError(this, "GetPSSnapInRead", exception, ErrorCategory.InvalidArgument);
                }
            }
            else
            {
                // this should never throw..
                Collection <PSSnapInInfo> psSnapInInfoList = GetSnapIns(null);
                foreach (PSSnapInInfo pssnapinInfo in psSnapInInfoList)
                {
                    // Load the pssnapin info properties that are localizable and redirected in the registry
                    pssnapinInfo.LoadIndirectResources(ResourceReader);
                    WriteObject(pssnapinInfo);
                }
            }
        }
        /// <summary>
        /// Adds pssnapins to console file and loads the pssnapin dlls into
        /// the current monad runtime.
        /// </summary>
        /// <remarks>
        /// The new pssnapin information is not stored in the console file until
        /// the file is saved.
        /// </remarks>
        protected override void ProcessRecord()
        {
            // Cache for the information stored in the registry
            // update the cache the first time a wildcard is found..
            Collection <PSSnapInInfo> listToSearch = null;

            foreach (string pattern in _pssnapins)
            {
                Exception           exception = null;
                Collection <string> listToAdd = new Collection <string>();

                try
                {
                    // check whether there are any wildcard characters
                    bool doWildCardSearch = WildcardPattern.ContainsWildcardCharacters(pattern);
                    if (doWildCardSearch)
                    {
                        // wildcard found in the pattern
                        // Get all the possible candidates for current monad version
                        if (listToSearch == null)
                        {
                            // cache snapin registry information...

                            // For 3.0 PowerShell, we still use "1" as the registry version key for
                            // Snapin and Custom shell lookup/discovery.
                            // For 3.0 PowerShell, we use "3" as the registry version key only for Engine
                            // related data like ApplicationBase etc.
                            listToSearch = PSSnapInReader.ReadAll(PSVersionInfo.RegistryVersion1Key);
                        }

                        listToAdd = SearchListForPattern(listToSearch, pattern);

                        // listToAdd wont be null..
                        Diagnostics.Assert(listToAdd != null, "Pattern matching returned null");
                        if (listToAdd.Count == 0)
                        {
                            if (_passThru)
                            {
                                // passThru is specified and we have nothing to add...
                                WriteNonTerminatingError(pattern, "NoPSSnapInsFound",
                                                         PSTraceSource.NewArgumentException(pattern,
                                                                                            MshSnapInCmdletResources.NoPSSnapInsFound, pattern),
                                                         ErrorCategory.InvalidArgument);
                            }

                            continue;
                        }
                    }
                    else
                    {
                        listToAdd.Add(pattern);
                    }

                    // now add all the snapins for this pattern...
                    AddPSSnapIns(listToAdd);
                }
                catch (PSArgumentException ae)
                {
                    exception = ae;
                }
                catch (System.Security.SecurityException se)
                {
                    exception = se;
                }

                if (exception != null)
                {
                    WriteNonTerminatingError(pattern,
                                             "AddPSSnapInRead",
                                             exception,
                                             ErrorCategory.InvalidArgument);
                }
            }
        }
        /// <summary>
        /// Routine to get the list of loaded snapins...
        /// </summary>
        /// <returns></returns>
        protected internal Collection <PSSnapInInfo> GetSnapIns(string pattern)
        {
            // If RunspaceConfiguration is not null, then return the list that it has
            if (Runspace != null)
            {
                if (pattern != null)
                {
                    return(Runspace.ConsoleInfo.GetPSSnapIn(pattern, _shouldGetAll));
                }
                else
                {
                    return(Runspace.ConsoleInfo.PSSnapIns);
                }
            }

            WildcardPattern matcher = null;

            if (!String.IsNullOrEmpty(pattern))
            {
                bool doWildCardSearch = WildcardPattern.ContainsWildcardCharacters(pattern);

                if (!doWildCardSearch)
                {
                    // Verify PSSnapInID..
                    // This will throw if it not a valid name
                    PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(pattern);
                }
                matcher = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase);
            }

            Collection <PSSnapInInfo> snapins = new Collection <PSSnapInInfo>();

            if (_shouldGetAll)
            {
                foreach (PSSnapInInfo snapinKey in PSSnapInReader.ReadAll())
                {
                    if (matcher == null || matcher.IsMatch(snapinKey.Name))
                    {
                        snapins.Add(snapinKey);
                    }
                }
            }
            else
            {
                // Otherwise, just scan through the list of cmdlets and rebuild the table.
                List <CmdletInfo> cmdlets = InvokeCommand.GetCmdlets();
                Dictionary <PSSnapInInfo, bool> snapinTable = new Dictionary <PSSnapInInfo, bool>();
                foreach (CmdletInfo cmdlet in cmdlets)
                {
                    PSSnapInInfo snapin = cmdlet.PSSnapIn;
                    if (snapin != null && !snapinTable.ContainsKey(snapin))
                    {
                        snapinTable.Add(snapin, true);
                    }
                }

                foreach (PSSnapInInfo snapinKey in snapinTable.Keys)
                {
                    if (matcher == null || matcher.IsMatch(snapinKey.Name))
                    {
                        snapins.Add(snapinKey);
                    }
                }
            }
            return(snapins);
        }
Esempio n. 8
0
 protected override void BeginProcessing()
 {
     if (this._pssnapins != null)
     {
         foreach (string str in this._pssnapins)
         {
             Exception innerException = null;
             try
             {
                 Collection <PSSnapInInfo> snapIns = base.GetSnapIns(str);
                 if (snapIns.Count == 0)
                 {
                     base.WriteNonTerminatingError(str, "NoPSSnapInsFound", PSTraceSource.NewArgumentException(str, "MshSnapInCmdletResources", "NoPSSnapInsFound", new object[] { str }), ErrorCategory.InvalidArgument);
                     continue;
                 }
                 foreach (PSSnapInInfo info in snapIns)
                 {
                     info.LoadIndirectResources(base.ResourceReader);
                     base.WriteObject(info);
                 }
             }
             catch (SecurityException exception2)
             {
                 innerException = exception2;
             }
             catch (PSArgumentException exception3)
             {
                 innerException = exception3;
             }
             if (innerException != null)
             {
                 base.WriteNonTerminatingError(str, "GetPSSnapInRead", innerException, ErrorCategory.InvalidArgument);
             }
         }
     }
     else if (base.ShouldGetAll)
     {
         Exception exception4 = null;
         try
         {
             foreach (PSSnapInInfo info2 in PSSnapInReader.ReadAll())
             {
                 info2.LoadIndirectResources(base.ResourceReader);
                 base.WriteObject(info2);
             }
         }
         catch (SecurityException exception5)
         {
             exception4 = exception5;
         }
         catch (PSArgumentException exception6)
         {
             exception4 = exception6;
         }
         if (exception4 != null)
         {
             base.WriteNonTerminatingError(this, "GetPSSnapInRead", exception4, ErrorCategory.InvalidArgument);
         }
     }
     else
     {
         foreach (PSSnapInInfo info3 in base.GetSnapIns(null))
         {
             info3.LoadIndirectResources(base.ResourceReader);
             base.WriteObject(info3);
         }
     }
 }