Exemplo n.º 1
0
        internal PSSnapInInfo RemovePSSnapIn(string mshSnapInID)
        {
            if (string.IsNullOrEmpty(mshSnapInID))
            {
                PSTraceSource.NewArgumentNullException("mshSnapInID");
            }
            PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(mshSnapInID);
            PSSnapInInfo info = null;

            foreach (PSSnapInInfo info2 in this.externalPSSnapIns)
            {
                if (string.Equals(mshSnapInID, info2.Name, StringComparison.OrdinalIgnoreCase))
                {
                    info = info2;
                    this.externalPSSnapIns.Remove(info2);
                    this.isDirty = true;
                    break;
                }
            }
            if (info != null)
            {
                return(info);
            }
            if (IsDefaultPSSnapIn(mshSnapInID, this.defaultPSSnapIns))
            {
                _mshsnapinTracer.WriteLine("MshSnapin {0} can't be removed since it is a default mshsnapin.", new object[] { mshSnapInID });
                throw PSTraceSource.NewArgumentException("mshSnapInID", "ConsoleInfoErrorStrings", "CannotRemoveDefault", new object[] { mshSnapInID });
            }
            throw PSTraceSource.NewArgumentException("mshSnapInID", "ConsoleInfoErrorStrings", "CannotRemovePSSnapIn", new object[] { mshSnapInID });
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 internal PSSnapInInfo RemovePSSnapIn(string mshSnapInID)
 {
     using (MshConsoleInfo.tracer.TraceMethod())
     {
         if (string.IsNullOrEmpty(mshSnapInID))
         {
             MshConsoleInfo.tracer.NewArgumentNullException(nameof(mshSnapInID));
         }
         PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(mshSnapInID);
         PSSnapInInfo psSnapInInfo = (PSSnapInInfo)null;
         foreach (PSSnapInInfo externalPsSnapIn in this.externalPSSnapIns)
         {
             if (string.Equals(mshSnapInID, externalPsSnapIn.Name, StringComparison.OrdinalIgnoreCase))
             {
                 psSnapInInfo = externalPsSnapIn;
                 this.externalPSSnapIns.Remove(externalPsSnapIn);
                 this.isDirty = true;
                 break;
             }
         }
         if (psSnapInInfo != null)
         {
             return(psSnapInInfo);
         }
         if (this.IsDefaultPSSnapIn(mshSnapInID))
         {
             MshConsoleInfo._mshsnapinTracer.WriteLine("MshSnapin {0} can't be removed since it is a default mshsnapin.", (object)mshSnapInID);
             throw MshConsoleInfo.tracer.NewArgumentException(nameof(mshSnapInID), "ConsoleInfoErrorStrings", "CannotRemoveDefault", (object)mshSnapInID);
         }
         throw MshConsoleInfo.tracer.NewArgumentException(nameof(mshSnapInID), "ConsoleInfoErrorStrings", "CannotRemovePSSnapIn", (object)mshSnapInID);
     }
 }
Exemplo n.º 4
0
        /// <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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Removes a mshsnapin specified by <paramref name="mshSnapInID"/> from the current
        /// list.
        /// </summary>
        /// <param name="mshSnapInID">ID of the mshsnapin which needs to be removed</param>
        /// <returns>PSSnapInInfo object for the mshsnapin that is removed.</returns>
        /// <remarks>MshSnapin is removed only from the console file. Registry entry
        /// is not touched.</remarks>
        /// <exception cref="PSArgumentNullException">
        /// mshSnapInID is null.
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// 1. mshSnapInID is either a default mshsnapin or not loaded.
        /// 2. mshSnapInId is not valid.
        /// </exception>
        internal PSSnapInInfo RemovePSSnapIn(string mshSnapInID)
        {
            if (string.IsNullOrEmpty(mshSnapInID))
            {
                PSTraceSource.NewArgumentNullException("mshSnapInID");
            }

            // Monad has specific restrictions on the mshsnapinid like
            // mshsnapinid should be A-Za-z0-9.-_ etc.
            PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(mshSnapInID);

            PSSnapInInfo removedPSSnapIn = null;

            // Check external mshsnapins
            foreach (PSSnapInInfo mshSnapIn in _externalPSSnapIns)
            {
                if (string.Equals(mshSnapInID, mshSnapIn.Name, System.StringComparison.OrdinalIgnoreCase))
                {
                    // We found the mshsnapin..remove from the list and break
                    removedPSSnapIn = mshSnapIn;
                    _externalPSSnapIns.Remove(mshSnapIn);

                    // The state of console file is changing..so set
                    // dirty flag.
                    IsDirty = true;
                    break;
                }
            }

            if (removedPSSnapIn == null)
            {
                if (IsDefaultPSSnapIn(mshSnapInID, _defaultPSSnapIns))
                {
                    s_mshsnapinTracer.WriteLine("MshSnapin {0} can't be removed since it is a default mshsnapin.", mshSnapInID);

                    throw PSTraceSource.NewArgumentException("mshSnapInID", ConsoleInfoErrorStrings.CannotRemoveDefault, mshSnapInID);
                }

                throw PSTraceSource.NewArgumentException("mshSnapInID", ConsoleInfoErrorStrings.CannotRemovePSSnapIn, mshSnapInID);
            }

            return(removedPSSnapIn);
        }
Exemplo n.º 7
0
 protected override void ProcessRecord()
 {
     foreach (string str in this._pssnapins)
     {
         Collection <PSSnapInInfo> snapIns = base.GetSnapIns(str);
         if (snapIns.Count == 0)
         {
             base.WriteNonTerminatingError(str, "NoPSSnapInsFound", PSTraceSource.NewArgumentException(str, "MshSnapInCmdletResources", "NoPSSnapInsFound", new object[] { str }), ErrorCategory.InvalidArgument);
         }
         else
         {
             foreach (PSSnapInInfo info in snapIns)
             {
                 if (base.ShouldProcess(info.Name))
                 {
                     Exception innerException = null;
                     if ((base.Runspace == null) && (base.Context.InitialSessionState != null))
                     {
                         try
                         {
                             PSSnapInException exception2;
                             PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(info.Name);
                             if (MshConsoleInfo.IsDefaultPSSnapIn(info.Name, base.Context.InitialSessionState.defaultSnapins))
                             {
                                 throw PSTraceSource.NewArgumentException(info.Name, "ConsoleInfoErrorStrings", "CannotRemoveDefault", new object[] { info.Name });
                             }
                             InitialSessionState state = InitialSessionState.Create();
                             state.ImportPSSnapIn(info, out exception2);
                             state.Unbind(base.Context);
                             base.Context.InitialSessionState.ImportedSnapins.Remove(info.Name);
                         }
                         catch (PSArgumentException exception3)
                         {
                             innerException = exception3;
                         }
                         if (innerException != null)
                         {
                             base.WriteNonTerminatingError(str, "RemovePSSnapIn", innerException, ErrorCategory.InvalidArgument);
                         }
                     }
                     else
                     {
                         try
                         {
                             PSSnapInException warning        = null;
                             PSSnapInInfo      sendToPipeline = base.Runspace.RemovePSSnapIn(info.Name, out warning);
                             if (warning != null)
                             {
                                 base.WriteNonTerminatingError(info.Name, "RemovePSSnapInRead", warning, ErrorCategory.InvalidData);
                             }
                             if (this._passThru)
                             {
                                 sendToPipeline.LoadIndirectResources(base.ResourceReader);
                                 base.WriteObject(sendToPipeline);
                             }
                         }
                         catch (PSArgumentException exception5)
                         {
                             innerException = exception5;
                         }
                         if (innerException != null)
                         {
                             base.WriteNonTerminatingError(str, "RemovePSSnapIn", innerException, ErrorCategory.InvalidArgument);
                         }
                     }
                 }
             }
         }
     }
 }
        /// <summary>
        /// Removes pssnapins from the current console file.
        /// </summary>
        /// <remarks>
        /// The pssnapin is not unloaded from the current engine. So all the cmdlets that are
        /// represented by this pssnapin will continue to work.
        /// </remarks>
        protected override void ProcessRecord()
        {
            foreach (string psSnapIn in _pssnapins)
            {
                Collection <PSSnapInInfo> snapIns = GetSnapIns(psSnapIn);

                // snapIns won't be null..
                Diagnostics.Assert(snapIns != null, "GetSnapIns() returned null");
                if (snapIns.Count == 0)
                {
                    WriteNonTerminatingError(psSnapIn, "NoPSSnapInsFound",
                                             PSTraceSource.NewArgumentException(psSnapIn,
                                                                                MshSnapInCmdletResources.NoPSSnapInsFound, psSnapIn),
                                             ErrorCategory.InvalidArgument);

                    continue;
                }

                foreach (PSSnapInInfo snapIn in snapIns)
                {
                    // confirm the operation first
                    // this is always false if WhatIf is set
                    if (ShouldProcess(snapIn.Name))
                    {
                        Exception exception = null;

                        if (this.Runspace == null && this.Context.InitialSessionState != null)
                        {
                            try
                            {
                                // Check if this snapin can be removed

                                // Monad has specific restrictions on the mshsnapinid like
                                // mshsnapinid should be A-Za-z0-9.-_ etc.
                                PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(snapIn.Name);

                                if (MshConsoleInfo.IsDefaultPSSnapIn(snapIn.Name, this.Context.InitialSessionState.defaultSnapins))
                                {
                                    throw PSTraceSource.NewArgumentException(snapIn.Name, ConsoleInfoErrorStrings.CannotRemoveDefault, snapIn.Name);
                                }

                                // Handle the initial session state case...
                                InitialSessionState iss = InitialSessionState.Create();
                                PSSnapInException   warning;

                                // Get the snapin information...
                                iss.ImportPSSnapIn(snapIn, out warning);
                                iss.Unbind(Context);
                                Context.InitialSessionState.ImportedSnapins.Remove(snapIn.Name);
                            }
                            catch (PSArgumentException ae)
                            {
                                exception = ae;
                            }

                            if (exception != null)
                            {
                                WriteNonTerminatingError(psSnapIn, "RemovePSSnapIn", exception, ErrorCategory.InvalidArgument);
                            }
                        }
                        else
                        {
                            try
                            {
                                PSSnapInException warning = null;

                                PSSnapInInfo psSnapInInfo = this.Runspace.RemovePSSnapIn(snapIn.Name, out warning);

                                if (warning != null)
                                {
                                    WriteNonTerminatingError(snapIn.Name, "RemovePSSnapInRead", warning, ErrorCategory.InvalidData);
                                }

                                if (_passThru)
                                {
                                    // Load the pssnapin info properties that are localizable and redirected in the registry
                                    psSnapInInfo.LoadIndirectResources(ResourceReader);
                                    WriteObject(psSnapInInfo);
                                }
                            }
                            catch (PSArgumentException ae)
                            {
                                exception = ae;
                            }

                            if (exception != null)
                            {
                                WriteNonTerminatingError(psSnapIn, "RemovePSSnapIn", exception, ErrorCategory.InvalidArgument);
                            }
                        }
                    } // ShouldContinue
                }
            }
        }
        /// <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);
        }
Exemplo n.º 10
0
 internal PSSnapInNameVersionPair(string PSSnapinName)
 {
     PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(PSSnapinName);
     this._PSSnapinName = PSSnapinName;
     this._version      = (Version)null;
 }