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>
        /// Adds one or more snapins
        /// </summary>
        /// <param name="snapInList">List of snapin IDs</param>
        /// <remarks>
        /// This is a helper method and should not throw any
        /// exceptions. All exceptions are caught and displayed
        /// to the user using write* methods
        /// </remarks>
        private void AddPSSnapIns(Collection <string> snapInList)
        {
            if (snapInList == null)
            {
                // nothing to add
                return;
            }

            //BUGBUG TODO - brucepay - this is a workaround for not being able to dynamically update
            // the set of cmdlets in a runspace if there is no RunspaceConfiguration object.
            // This is a temporary fix to unblock remoting tests and need to be corrected/completed
            // before we can ship...

            // If there is no RunspaceConfig object, then
            // use an InitialSessionState object to gather and
            // bind the cmdlets from the snapins...
            if (Context.RunspaceConfiguration == null)
            {
                Collection <PSSnapInInfo> loadedSnapins = base.GetSnapIns(null);
                InitialSessionState       iss           = InitialSessionState.Create();
                bool isAtleastOneSnapinLoaded           = false;
                foreach (string snapIn in snapInList)
                {
                    if (InitialSessionState.IsEngineModule(snapIn))
                    {
                        WriteNonTerminatingError(snapIn, "LoadSystemSnapinAsModule",
                                                 PSTraceSource.NewArgumentException(snapIn,
                                                                                    MshSnapInCmdletResources.LoadSystemSnapinAsModule, snapIn),
                                                 ErrorCategory.InvalidArgument);
                    }
                    else
                    {
                        PSSnapInException warning;
                        try
                        {
                            // Read snapin data
                            PSSnapInInfo newPSSnapIn  = PSSnapInReader.Read(Utils.GetCurrentMajorVersion(), snapIn);
                            PSSnapInInfo psSnapInInfo = IsSnapInLoaded(loadedSnapins, newPSSnapIn);

                            // that means snapin is not already loaded ..so load the snapin
                            // now.
                            if (null == psSnapInInfo)
                            {
                                psSnapInInfo             = iss.ImportPSSnapIn(snapIn, out warning);
                                isAtleastOneSnapinLoaded = true;
                                Context.InitialSessionState.ImportedSnapins.Add(psSnapInInfo.Name, psSnapInInfo);
                            }
                            // Write psSnapInInfo object only if passthru is specified.
                            if (_passThru)
                            {
                                // Load the pssnapin info properties that are localizable and redirected in the registry
                                psSnapInInfo.LoadIndirectResources(ResourceReader);
                                WriteObject(psSnapInInfo);
                            }
                        }

                        catch (PSSnapInException pse)
                        {
                            WriteNonTerminatingError(snapIn, "AddPSSnapInRead", pse, ErrorCategory.InvalidData);
                        }
                    }
                }

                if (isAtleastOneSnapinLoaded)
                {
                    // Now update the session state with the new stuff...
                    iss.Bind(Context, /*updateOnly*/ true);
                }

                return;
            }

            foreach (string psSnapIn in snapInList)
            {
                Exception exception = null;

                try
                {
                    PSSnapInException warning = null;

                    PSSnapInInfo psSnapInInfo = this.Runspace.AddPSSnapIn(psSnapIn, out warning);

                    if (warning != null)
                    {
                        WriteNonTerminatingError(psSnapIn, "AddPSSnapInRead", warning, ErrorCategory.InvalidData);
                    }

                    // Write psSnapInInfo object only if passthru is specified.
                    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;
                }
                catch (PSSnapInException sle)
                {
                    exception = sle;
                }
                catch (System.Security.SecurityException se)
                {
                    exception = se;
                }

                if (exception != null)
                {
                    WriteNonTerminatingError(psSnapIn,
                                             "AddPSSnapInRead",
                                             exception,
                                             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
                }
            }
        }
示例#4
0
 private void AddPSSnapIns(Collection <string> snapInList)
 {
     if (snapInList != null)
     {
         if (base.Context.RunspaceConfiguration == null)
         {
             Collection <PSSnapInInfo> snapIns = base.GetSnapIns(null);
             InitialSessionState       state   = InitialSessionState.Create();
             bool flag = false;
             foreach (string str in snapInList)
             {
                 if (InitialSessionState.IsEngineModule(str))
                 {
                     base.WriteNonTerminatingError(str, "LoadSystemSnapinAsModule", PSTraceSource.NewArgumentException(str, "MshSnapInCmdletResources", "LoadSystemSnapinAsModule", new object[] { str }), ErrorCategory.InvalidArgument);
                 }
                 else
                 {
                     try
                     {
                         PSSnapInInfo psSnapInInfo = PSSnapInReader.Read(Utils.GetCurrentMajorVersion(), str);
                         PSSnapInInfo info2        = PSSnapInCommandBase.IsSnapInLoaded(snapIns, psSnapInInfo);
                         if (info2 == null)
                         {
                             PSSnapInException exception;
                             info2 = state.ImportPSSnapIn(str, out exception);
                             flag  = true;
                             base.Context.InitialSessionState.ImportedSnapins.Add(info2.Name, info2);
                         }
                         if (this._passThru)
                         {
                             info2.LoadIndirectResources(base.ResourceReader);
                             base.WriteObject(info2);
                         }
                     }
                     catch (PSSnapInException exception2)
                     {
                         base.WriteNonTerminatingError(str, "AddPSSnapInRead", exception2, ErrorCategory.InvalidData);
                     }
                 }
             }
             if (flag)
             {
                 state.Bind(base.Context, true);
             }
         }
         else
         {
             foreach (string str2 in snapInList)
             {
                 Exception innerException = null;
                 try
                 {
                     PSSnapInException warning        = null;
                     PSSnapInInfo      sendToPipeline = base.Runspace.AddPSSnapIn(str2, out warning);
                     if (warning != null)
                     {
                         base.WriteNonTerminatingError(str2, "AddPSSnapInRead", warning, ErrorCategory.InvalidData);
                     }
                     if (this._passThru)
                     {
                         sendToPipeline.LoadIndirectResources(base.ResourceReader);
                         base.WriteObject(sendToPipeline);
                     }
                 }
                 catch (PSArgumentException exception5)
                 {
                     innerException = exception5;
                 }
                 catch (PSSnapInException exception6)
                 {
                     innerException = exception6;
                 }
                 catch (SecurityException exception7)
                 {
                     innerException = exception7;
                 }
                 if (innerException != null)
                 {
                     base.WriteNonTerminatingError(str2, "AddPSSnapInRead", innerException, ErrorCategory.InvalidArgument);
                 }
             }
         }
     }
 }