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 } } }