Exemple #1
0
 internal Collection <string> GetSearchPaths()
 {
     if (this._searchPaths == null)
     {
         this._searchPaths = new Collection <string>();
         RunspaceConfigForSingleShell runspaceConfiguration = this.ExecutionContext.RunspaceConfiguration as RunspaceConfigForSingleShell;
         if (runspaceConfiguration != null)
         {
             MshConsoleInfo consoleInfo = runspaceConfiguration.ConsoleInfo;
             if ((consoleInfo == null) || (consoleInfo.ExternalPSSnapIns == null))
             {
                 return(this._searchPaths);
             }
             foreach (PSSnapInInfo info2 in consoleInfo.ExternalPSSnapIns)
             {
                 this._searchPaths.Add(info2.ApplicationBase);
             }
         }
         if (this.ExecutionContext.Modules != null)
         {
             foreach (PSModuleInfo info3 in this.ExecutionContext.Modules.ModuleTable.Values)
             {
                 this._searchPaths.Add(info3.ModuleBase);
             }
         }
     }
     return(this._searchPaths);
 }
Exemple #2
0
 internal Collection <string> GetSearchPaths()
 {
     if (this._searchPaths != null)
     {
         return(this._searchPaths);
     }
     this._searchPaths = new Collection <string>();
     if (this.ExecutionContext.RunspaceConfiguration is RunspaceConfigForSingleShell runspaceConfiguration)
     {
         MshConsoleInfo consoleInfo = runspaceConfiguration.ConsoleInfo;
         if (consoleInfo == null || consoleInfo.ExternalPSSnapIns == null)
         {
             return(this._searchPaths);
         }
         foreach (PSSnapInInfo externalPsSnapIn in consoleInfo.ExternalPSSnapIns)
         {
             this._searchPaths.Add(externalPsSnapIn.ApplicationBase);
         }
     }
     if (this.ExecutionContext.Modules != null)
     {
         foreach (PSModuleInfo psModuleInfo in this.ExecutionContext.Modules.ModuleTable.Values)
         {
             this._searchPaths.Add(psModuleInfo.ModuleBase);
         }
     }
     return(this._searchPaths);
 }
        /// <summary>
        /// Gets the search paths for external snapins/modules that are currently loaded.
        /// If the current shell is single-shell based, then the returned
        /// search path contains all the directories of currently active PSSnapIns/modules.
        /// </summary>
        /// <returns>a collection of strings representing locations</returns>
        internal Collection <string> GetSearchPaths()
        {
            // return the cache if already present.
            if (null != _searchPaths)
            {
                return(_searchPaths);
            }
            _searchPaths = new Collection <String>();

            RunspaceConfigForSingleShell runspace = this.ExecutionContext.RunspaceConfiguration as RunspaceConfigForSingleShell;

            if (null != runspace)
            {
                // SingleShell case. Check active snapins...
                MshConsoleInfo currentConsole = runspace.ConsoleInfo;

                if ((null == currentConsole) || (null == currentConsole.ExternalPSSnapIns))
                {
                    return(_searchPaths);
                }

                foreach (PSSnapInInfo snapin in currentConsole.ExternalPSSnapIns)
                {
                    _searchPaths.Add(snapin.ApplicationBase);
                }
            }

            // add loaded modules paths to the search path
            if (null != ExecutionContext.Modules)
            {
                foreach (PSModuleInfo loadedModule in ExecutionContext.Modules.ModuleTable.Values)
                {
                    if (!_searchPaths.Contains(loadedModule.ModuleBase))
                    {
                        _searchPaths.Add(loadedModule.ModuleBase);
                    }
                }
            }

            return(_searchPaths);
        }
 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
                }
            }
        }