private Collection <PSSnapInInfo> Load(
     string path,
     out PSConsoleLoadException cle)
 {
     using (MshConsoleInfo.tracer.TraceMethod())
     {
         cle = (PSConsoleLoadException)null;
         MshConsoleInfo._mshsnapinTracer.WriteLine("Load mshsnapins from console file {0}", (object)path);
         if (string.IsNullOrEmpty(path))
         {
             throw MshConsoleInfo.tracer.NewArgumentNullException(nameof(path));
         }
         if (!Path.IsPathRooted(path))
         {
             MshConsoleInfo._mshsnapinTracer.TraceError("Console file {0} needs to be a absolute path.", (object)path);
             throw MshConsoleInfo.tracer.NewArgumentException(nameof(path), "ConsoleInfoErrorStrings", "PathNotAbsolute", (object)path);
         }
         if (!path.EndsWith(".psc1", StringComparison.OrdinalIgnoreCase))
         {
             MshConsoleInfo._mshsnapinTracer.TraceError("Console file {0} needs to have {1} extension.", (object)path, (object)".psc1");
             throw MshConsoleInfo.tracer.NewArgumentException(nameof(path), "ConsoleInfoErrorStrings", "BadConsoleExtension", (object)"");
         }
         PSConsoleFileElement fromFile = PSConsoleFileElement.CreateFromFile(path);
         if (!Utils.IsVersionSupported(fromFile.MonadVersion))
         {
             MshConsoleInfo._mshsnapinTracer.TraceError("Console version {0} is not supported in current monad session.", (object)fromFile.MonadVersion);
             throw MshConsoleInfo.tracer.NewArgumentException("PSVersion", "ConsoleInfoErrorStrings", "BadMonadVersion", (object)fromFile.MonadVersion, (object)this.psVersion.ToString());
         }
         Collection <PSSnapInException> exceptions = new Collection <PSSnapInException>();
         foreach (string psSnapIn in fromFile.PSSnapIns)
         {
             try
             {
                 this.AddPSSnapIn(psSnapIn);
             }
             catch (PSArgumentException ex)
             {
                 PSSnapInException psSnapInException = new PSSnapInException(psSnapIn, ex.Message, (Exception)ex);
                 MshConsoleInfo._mshsnapinTracer.TraceException((Exception)psSnapInException);
                 exceptions.Add(psSnapInException);
             }
             catch (SecurityException ex)
             {
                 string            message           = ResourceManagerCache.FormatResourceString("ConsoleInfoErrorStrings", "PSSnapInReadError");
                 PSSnapInException psSnapInException = new PSSnapInException(psSnapIn, message, (Exception)ex);
                 MshConsoleInfo._mshsnapinTracer.TraceException((Exception)psSnapInException);
                 exceptions.Add(psSnapInException);
             }
         }
         if (exceptions.Count > 0)
         {
             cle = new PSConsoleLoadException(this, exceptions);
         }
         this.isDirty = false;
         return(this.externalPSSnapIns);
     }
 }
Exemple #2
0
        private Collection <PSSnapInInfo> Load(string path, out PSConsoleLoadException cle)
        {
            cle = null;
            _mshsnapinTracer.WriteLine("Load mshsnapins from console file {0}", new object[] { path });
            if (string.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }
            if (!Path.IsPathRooted(path))
            {
                _mshsnapinTracer.TraceError("Console file {0} needs to be a absolute path.", new object[] { path });
                throw PSTraceSource.NewArgumentException("path", "ConsoleInfoErrorStrings", "PathNotAbsolute", new object[] { path });
            }
            if (!path.EndsWith(".psc1", StringComparison.OrdinalIgnoreCase))
            {
                _mshsnapinTracer.TraceError("Console file {0} needs to have {1} extension.", new object[] { path, ".psc1" });
                throw PSTraceSource.NewArgumentException("path", "ConsoleInfoErrorStrings", "BadConsoleExtension", new object[] { "" });
            }
            PSConsoleFileElement element = PSConsoleFileElement.CreateFromFile(path);

            if (!Utils.IsPSVersionSupported(element.MonadVersion))
            {
                _mshsnapinTracer.TraceError("Console version {0} is not supported in current monad session.", new object[] { element.MonadVersion });
                throw PSTraceSource.NewArgumentException("PSVersion", "ConsoleInfoErrorStrings", "BadMonadVersion", new object[] { element.MonadVersion, this.psVersion.ToString() });
            }
            Collection <PSSnapInException> exceptions = new Collection <PSSnapInException>();

            foreach (string str in element.PSSnapIns)
            {
                try
                {
                    this.AddPSSnapIn(str);
                }
                catch (PSArgumentException exception)
                {
                    PSSnapInException item = new PSSnapInException(str, exception.Message, exception);
                    exceptions.Add(item);
                }
                catch (SecurityException exception3)
                {
                    string            pSSnapInReadError = ConsoleInfoErrorStrings.PSSnapInReadError;
                    PSSnapInException exception4        = new PSSnapInException(str, pSSnapInReadError, exception3);
                    exceptions.Add(exception4);
                }
            }
            if (exceptions.Count > 0)
            {
                cle = new PSConsoleLoadException(this, exceptions);
            }
            this.isDirty = false;
            return(this.externalPSSnapIns);
        }
        /// <summary>
        /// Loads a Monad Console file specified by <paramref name="path"/>
        /// </summary>
        /// <param name="path">
        /// The absolute path from which the content is loaded.
        /// </param>
        /// <param name="cle">
        /// PSConsoleLoadException occurred while loading this console file. This object
        /// also contains specific PSSnapInExceptions that occurred while loading.
        /// </param>
        /// <returns>
        /// A list of <see cref="PSSnapInInfo"/> objects specified in the console file.
        /// </returns>
        /// <exception cref="PSArgumentNullException">
        /// Path is null.
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// 1. Path does not specify proper file extension.
        /// 2. PSSnapInId doesnt contain valid characters.
        /// 3. Path is not an Absolute Path.
        ///    Example of valid paths:"\\MyDir\\MyFile.txt" and "C:\\MyDir".
        /// </exception>
        /// <exception cref="ArgumentException">
        /// path contains one or more of the invalid characters defined in System.IO.Path.InvalidPathChars.
        /// </exception>
        /// <exception cref="XmlException">
        /// Unable to load/parse the file specified by path.
        /// </exception>
        private Collection <PSSnapInInfo> Load(string path, out PSConsoleLoadException cle)
        {
            // Initialize the out parameter..
            cle = null;

            s_mshsnapinTracer.WriteLine("Load mshsnapins from console file {0}", path);

            if (string.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            // Check whether the path is an absolute path
            if (!Path.IsPathRooted(path))
            {
                s_mshsnapinTracer.TraceError("Console file {0} needs to be a absolute path.", path);

                throw PSTraceSource.NewArgumentException("path", ConsoleInfoErrorStrings.PathNotAbsolute, path);
            }

            if (!path.EndsWith(StringLiterals.PowerShellConsoleFileExtension, StringComparison.OrdinalIgnoreCase))
            {
                s_mshsnapinTracer.TraceError("Console file {0} needs to have {1} extension.", path, StringLiterals.PowerShellConsoleFileExtension);

                throw PSTraceSource.NewArgumentException("path", ConsoleInfoErrorStrings.BadConsoleExtension);
            }

            PSConsoleFileElement consoleFileElement;

            // exceptions are thrown to the caller
            consoleFileElement = PSConsoleFileElement.CreateFromFile(path);

            // consoleFileElement will never be null..
            if (!Utils.IsPSVersionSupported(consoleFileElement.MonadVersion))
            {
                s_mshsnapinTracer.TraceError("Console version {0} is not supported in current monad session.", consoleFileElement.MonadVersion);

                throw PSTraceSource.NewArgumentException("PSVersion", ConsoleInfoErrorStrings.BadMonadVersion, consoleFileElement.MonadVersion,
                                                         PSVersion.ToString());
            }

            // Create a store for exceptions
            Collection <PSSnapInException> exceptions = new Collection <PSSnapInException>();

            foreach (string mshsnapin in consoleFileElement.PSSnapIns)
            {
                try
                {
                    this.AddPSSnapIn(mshsnapin);
                }
                catch (PSArgumentException ae)
                {
                    PSSnapInException sle = new PSSnapInException(mshsnapin, ae.Message, ae);

                    // Eat ArgumentException and continue..
                    exceptions.Add(sle);
                }
                catch (System.Security.SecurityException se)
                {
                    string            message = ConsoleInfoErrorStrings.PSSnapInReadError;
                    PSSnapInException sle     = new PSSnapInException(mshsnapin, message, se);
                    // Eat SecurityException and continue..

                    exceptions.Add(sle);
                }
            }

            // Before returning check whether there are any exceptions
            if (exceptions.Count > 0)
            {
                cle = new PSConsoleLoadException(this, exceptions);
            }

            // We are able to load console file and currently monad engine
            // can service this. So mark the isdirty flag.
            IsDirty = false;

            return(_externalPSSnapIns);
        }