Esempio n. 1
0
        internal PSSnapInInfo AddPSSnapIn(string mshSnapInID)
        {
            if (string.IsNullOrEmpty(mshSnapInID))
            {
                PSTraceSource.NewArgumentNullException("mshSnapInID");
            }
            if (IsDefaultPSSnapIn(mshSnapInID, this.defaultPSSnapIns))
            {
                _mshsnapinTracer.TraceError("MshSnapin {0} can't be added since it is a default mshsnapin", new object[] { mshSnapInID });
                throw PSTraceSource.NewArgumentException("mshSnapInID", "ConsoleInfoErrorStrings", "CannotLoadDefault", new object[0]);
            }
            if (this.IsActiveExternalPSSnapIn(mshSnapInID))
            {
                _mshsnapinTracer.TraceError("MshSnapin {0} is already loaded.", new object[] { mshSnapInID });
                throw PSTraceSource.NewArgumentException("mshSnapInID", "ConsoleInfoErrorStrings", "PSSnapInAlreadyExists", new object[] { mshSnapInID });
            }
            PSSnapInInfo item = PSSnapInReader.Read(this.MajorVersion, mshSnapInID);

            if (!Utils.IsPSVersionSupported(item.PSVersion.ToString()))
            {
                _mshsnapinTracer.TraceError("MshSnapin {0} and current monad engine's versions don't match.", new object[] { mshSnapInID });
                throw PSTraceSource.NewArgumentException("mshSnapInID", "ConsoleInfoErrorStrings", "AddPSSnapInBadMonadVersion", new object[] { item.PSVersion.ToString(), this.psVersion.ToString() });
            }
            this.externalPSSnapIns.Add(item);
            _mshsnapinTracer.WriteLine("MshSnapin {0} successfully added to consoleinfo list.", new object[] { mshSnapInID });
            this.isDirty = true;
            return(item);
        }
Esempio n. 2
0
 internal PSSnapInInfo AddPSSnapIn(string mshSnapInID)
 {
     using (MshConsoleInfo.tracer.TraceMethod())
     {
         if (string.IsNullOrEmpty(mshSnapInID))
         {
             MshConsoleInfo.tracer.NewArgumentNullException(nameof(mshSnapInID));
         }
         if (this.IsDefaultPSSnapIn(mshSnapInID))
         {
             MshConsoleInfo._mshsnapinTracer.TraceError("MshSnapin {0} can't be added since it is a default mshsnapin", (object)mshSnapInID);
             throw MshConsoleInfo.tracer.NewArgumentException(nameof(mshSnapInID), "ConsoleInfoErrorStrings", "CannotLoadDefault");
         }
         if (this.IsActiveExternalPSSnapIn(mshSnapInID))
         {
             MshConsoleInfo._mshsnapinTracer.TraceError("MshSnapin {0} is already loaded.", (object)mshSnapInID);
             throw MshConsoleInfo.tracer.NewArgumentException(nameof(mshSnapInID), "ConsoleInfoErrorStrings", "PSSnapInAlreadyExists", (object)mshSnapInID);
         }
         PSSnapInInfo psSnapInInfo = PSSnapInReader.Read(this.MajorVersion, mshSnapInID);
         if (!Utils.IsVersionSupported(psSnapInInfo.PSVersion.ToString()))
         {
             MshConsoleInfo._mshsnapinTracer.TraceError("MshSnapin {0} and current monad engine's versions don't match.", (object)mshSnapInID);
             throw MshConsoleInfo.tracer.NewArgumentException(nameof(mshSnapInID), "ConsoleInfoErrorStrings", "AddPSSnapInBadMonadVersion", (object)psSnapInInfo.PSVersion.ToString(), (object)this.psVersion.ToString());
         }
         this.externalPSSnapIns.Add(psSnapInInfo);
         MshConsoleInfo._mshsnapinTracer.WriteLine("MshSnapin {0} successfully added to consoleinfo list.", (object)mshSnapInID);
         this.isDirty = true;
         return(psSnapInInfo);
     }
 }
        /// <summary>
        /// Adds a mshsnapin specified by <paramref name="mshSnapInID"/> to the current list of
        /// mshsnapins. If the mshsnapin is successfully added, IsDirty property is set to true.
        /// </summary>
        /// <param name="mshSnapInID">ID of the mshsnapin which needs to be added.</param>
        /// <returns>A <see cref="PSSnapInInfo"/> object corresponding to mshSnapInID.</returns>
        /// <remarks>PSSnapIn information must be present in the registry for this call to succeed.</remarks>
        /// <exception cref="PSArgumentNullException">
        /// mshSnapInID is empty or null.
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// PSSnapIn is already loaded.
        /// No PSSnapIn with given id found.
        /// PSSnapIn cannot be loaded.
        /// </exception>
        /// <exception cref="System.Security.SecurityException">
        /// Caller doesn't have permission to read keys.
        /// </exception>
        internal PSSnapInInfo AddPSSnapIn(string mshSnapInID)
        {
            if (string.IsNullOrEmpty(mshSnapInID))
            {
                PSTraceSource.NewArgumentNullException("mshSnapInID");
            }

            // Check whether the mshsnapin is already present in defaultmshsnapins/externalMshSnapins
            if (IsDefaultPSSnapIn(mshSnapInID, _defaultPSSnapIns))
            {
                s_mshsnapinTracer.TraceError("MshSnapin {0} can't be added since it is a default mshsnapin", mshSnapInID);

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

            if (IsActiveExternalPSSnapIn(mshSnapInID))
            {
                s_mshsnapinTracer.TraceError("MshSnapin {0} is already loaded.", mshSnapInID);

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

            // Check whether the mshsnapin is present in the registry.
            PSSnapInInfo newPSSnapIn = PSSnapInReader.Read(this.MajorVersion, mshSnapInID);

            if (!Utils.IsPSVersionSupported(newPSSnapIn.PSVersion.ToString()))
            {
                s_mshsnapinTracer.TraceError("MshSnapin {0} and current monad engine's versions don't match.", mshSnapInID);

                throw PSTraceSource.NewArgumentException("mshSnapInID",
                                                         ConsoleInfoErrorStrings.AddPSSnapInBadMonadVersion,
                                                         newPSSnapIn.PSVersion.ToString(),
                                                         PSVersion.ToString());
            }

            // new mshsnapin will never be null
            //if this is a valid new mshsnapin,add this to external mshsnapins
            _externalPSSnapIns.Add(newPSSnapIn);
            s_mshsnapinTracer.WriteLine("MshSnapin {0} successfully added to consoleinfo list.", mshSnapInID);
            //Set IsDirty to true
            IsDirty = true;

            return(newPSSnapIn);
        }
        /// <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);
                }
            }
        }
Esempio n. 5
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);
                 }
             }
         }
     }
 }