Exemple #1
0
        /// <exception cref="PSSnapInException">
        /// One or more default mshsnapins cannot be loaded because the
        /// registry is not populated correctly.
        /// </exception>
        internal static RunspaceConfigForSingleShell CreateDefaultConfiguration()
        {
            s_mshsnapinTracer.WriteLine("Creating default runspace configuration.");

            MshConsoleInfo consoleInfo = MshConsoleInfo.CreateDefaultConfiguration();

            // This should not happen. If there is a failure in creating consoleInfo,
            // an exception should have been thrown up.
            if (consoleInfo != null)
            {
                RunspaceConfigForSingleShell rspcfg  = new RunspaceConfigForSingleShell(consoleInfo);
                PSConsoleLoadException       warning = null;

                rspcfg.LoadConsole(out warning);

                if (warning != null)
                {
                    s_mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", warning.Message);
                }

                return(rspcfg);
            }

            s_mshsnapinTracer.WriteLine("Default runspace configuration created.");

            return(null);
        }
        /// <exception cref="PSSnapInException">
        /// One or more default mshsnapins cannot be loaded because the
        /// registry is not populated correctly.
        /// </exception>
        /// <exception cref="PSArgumentNullException">
        /// fileName is null.
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// fileName does not specify proper file extension.
        /// </exception>
        /// <exception cref="XmlException">
        /// Unable to load/parse the file specified by fileName.
        /// </exception>
        internal new static RunspaceConfigForSingleShell Create(string consoleFile, out PSConsoleLoadException warning)
        {
            PSConsoleLoadException warning1 = null;

            s_mshsnapinTracer.WriteLine("Creating MshConsoleInfo. consoleFile={0}", consoleFile);

            MshConsoleInfo consoleInfo = MshConsoleInfo.CreateFromConsoleFile(consoleFile, out warning1);

            if (warning1 != null)
            {
                s_mshsnapinTracer.TraceWarning("There was a warning while creating MshConsoleInfo: {0}", warning1.Message);
            }

            // At this time, consoleInfo should not be null. 
            // Otherwise, an exception should have been thrown up. 
            if (consoleInfo != null)
            {
                RunspaceConfigForSingleShell rspcfg = new RunspaceConfigForSingleShell(consoleInfo);
                PSConsoleLoadException warning2 = null;

                rspcfg.LoadConsole(out warning2);

                if (warning2 != null)
                {
                    s_mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", warning2.Message);
                }

                warning = CombinePSConsoleLoadException(warning1, warning2);

                return rspcfg;
            }

            warning = null;
            return null;
        }
Exemple #3
0
        internal static RunspaceConfigForSingleShell Create(string consoleFile, out PSConsoleLoadException warning)
        {
            PSConsoleLoadException cle = null;

            _mshsnapinTracer.WriteLine("Creating MshConsoleInfo. consoleFile={0}", new object[] { consoleFile });
            MshConsoleInfo consoleInfo = MshConsoleInfo.CreateFromConsoleFile(consoleFile, out cle);

            if (cle != null)
            {
                _mshsnapinTracer.TraceWarning("There was a warning while creating MshConsoleInfo: {0}", new object[] { cle.Message });
            }
            if (consoleInfo != null)
            {
                RunspaceConfigForSingleShell shell      = new RunspaceConfigForSingleShell(consoleInfo);
                PSConsoleLoadException       exception2 = null;
                shell.LoadConsole(out exception2);
                if (exception2 != null)
                {
                    _mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", new object[] { exception2.Message });
                }
                warning = CombinePSConsoleLoadException(cle, exception2);
                return(shell);
            }
            warning = null;
            return(null);
        }
 private static PSConsoleLoadException CombinePSConsoleLoadException(
     PSConsoleLoadException e1,
     PSConsoleLoadException e2)
 {
     using (RunspaceConfigForSingleShell.tracer.TraceMethod())
     {
         if ((e1 == null || e1.PSSnapInExceptions.Count == 0) && (e2 == null || e2.PSSnapInExceptions.Count == 0))
         {
             return((PSConsoleLoadException)null);
         }
         if (e1 == null || e1.PSSnapInExceptions.Count == 0)
         {
             return(e2);
         }
         if (e2 == null || e2.PSSnapInExceptions.Count == 0)
         {
             return(e1);
         }
         foreach (PSSnapInException psSnapInException in e2.PSSnapInExceptions)
         {
             e1.PSSnapInExceptions.Add(psSnapInException);
         }
         return(e1);
     }
 }
 internal static RunspaceConfigForSingleShell Create(
     string consoleFile,
     out PSConsoleLoadException warning)
 {
     using (RunspaceConfigForSingleShell.tracer.TraceMethod())
     {
         PSConsoleLoadException cle = (PSConsoleLoadException)null;
         RunspaceConfigForSingleShell._mshsnapinTracer.WriteLine("Creating MshConsoleInfo. consoleFile={0}", (object)consoleFile);
         MshConsoleInfo fromConsoleFile = MshConsoleInfo.CreateFromConsoleFile(consoleFile, out cle);
         if (cle != null)
         {
             RunspaceConfigForSingleShell._mshsnapinTracer.TraceWarning("There was a warning while creating MshConsoleInfo: {0}", (object)cle.Message);
         }
         if (fromConsoleFile != null)
         {
             RunspaceConfigForSingleShell configForSingleShell = new RunspaceConfigForSingleShell(fromConsoleFile);
             PSConsoleLoadException       warning1             = (PSConsoleLoadException)null;
             configForSingleShell.LoadConsole(out warning1);
             if (warning1 != null)
             {
                 RunspaceConfigForSingleShell._mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", (object)warning1.Message);
             }
             warning = RunspaceConfigForSingleShell.CombinePSConsoleLoadException(cle, warning1);
             return(configForSingleShell);
         }
         warning = (PSConsoleLoadException)null;
         return((RunspaceConfigForSingleShell)null);
     }
 }
Exemple #6
0
        /// <exception cref="PSSnapInException">
        /// One or more default mshsnapins cannot be loaded because the
        /// registry is not populated correctly.
        /// </exception>
        /// <exception cref="PSArgumentNullException">
        /// fileName is null.
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// fileName does not specify proper file extension.
        /// </exception>
        /// <exception cref="XmlException">
        /// Unable to load/parse the file specified by fileName.
        /// </exception>
        internal new static RunspaceConfigForSingleShell Create(string consoleFile, out PSConsoleLoadException warning)
        {
            PSConsoleLoadException warning1 = null;

            s_mshsnapinTracer.WriteLine("Creating MshConsoleInfo. consoleFile={0}", consoleFile);

            MshConsoleInfo consoleInfo = MshConsoleInfo.CreateFromConsoleFile(consoleFile, out warning1);

            if (warning1 != null)
            {
                s_mshsnapinTracer.TraceWarning("There was a warning while creating MshConsoleInfo: {0}", warning1.Message);
            }

            // At this time, consoleInfo should not be null.
            // Otherwise, an exception should have been thrown up.
            if (consoleInfo != null)
            {
                RunspaceConfigForSingleShell rspcfg   = new RunspaceConfigForSingleShell(consoleInfo);
                PSConsoleLoadException       warning2 = null;

                rspcfg.LoadConsole(out warning2);

                if (warning2 != null)
                {
                    s_mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", warning2.Message);
                }

                warning = CombinePSConsoleLoadException(warning1, warning2);

                return(rspcfg);
            }

            warning = null;
            return(null);
        }
Exemple #7
0
 public static RunspaceConfiguration Create(
     string consoleFilePath,
     out PSConsoleLoadException warnings)
 {
     using (RunspaceConfiguration.tracer.TraceMethod())
         return((RunspaceConfiguration)RunspaceConfigForSingleShell.Create(consoleFilePath, out warnings));
 }
 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 #9
0
        private void LoadConsole(out PSConsoleLoadException warning)
        {
            if (_consoleInfo == null)
            {
                warning = null;
                return;
            }

            LoadPSSnapIns(_consoleInfo.PSSnapIns, out warning);
        }
Exemple #10
0
 internal static MshConsoleInfo CreateFromConsoleFile(string fileName, out PSConsoleLoadException cle)
 {
     _mshsnapinTracer.WriteLine("Creating console info from file {0}", new object[] { fileName });
     MshConsoleInfo info = CreateDefaultConfiguration();
     string fullPath = Path.GetFullPath(fileName);
     info.fileName = fullPath;
     info.Load(fullPath, out cle);
     _mshsnapinTracer.WriteLine("Console info created successfully", new object[0]);
     return info;
 }
Exemple #11
0
 private void LoadConsole(out PSConsoleLoadException warning)
 {
     if (this._consoleInfo == null)
     {
         warning = null;
     }
     else
     {
         this.LoadPSSnapIns(this._consoleInfo.PSSnapIns, out warning);
     }
 }
Exemple #12
0
        internal static MshConsoleInfo CreateFromConsoleFile(string fileName, out PSConsoleLoadException cle)
        {
            _mshsnapinTracer.WriteLine("Creating console info from file {0}", new object[] { fileName });
            MshConsoleInfo info     = CreateDefaultConfiguration();
            string         fullPath = Path.GetFullPath(fileName);

            info.fileName = fullPath;
            info.Load(fullPath, out cle);
            _mshsnapinTracer.WriteLine("Console info created successfully", new object[0]);
            return(info);
        }
Exemple #13
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);
        }
 internal static MshConsoleInfo CreateFromConsoleFile(
     string fileName,
     out PSConsoleLoadException cle)
 {
     using (MshConsoleInfo.tracer.TraceMethod())
     {
         MshConsoleInfo._mshsnapinTracer.WriteLine("Creating console info from file {0}", (object)fileName);
         MshConsoleInfo defaultConfiguration = MshConsoleInfo.CreateDefaultConfiguration();
         string         fullPath             = Path.GetFullPath(fileName);
         defaultConfiguration.fileName = fullPath;
         defaultConfiguration.Load(fullPath, out cle);
         MshConsoleInfo._mshsnapinTracer.WriteLine("Console info created successfully", new object[0]);
         return(defaultConfiguration);
     }
 }
Exemple #15
0
        private void LoadPSSnapIns(Collection <PSSnapInInfo> mshsnapinInfos, out PSConsoleLoadException warning)
        {
            warning = null;

            Collection <PSSnapInException> mshsnapinExceptions = new Collection <PSSnapInException>();

            bool partialSuccess = false;

            foreach (PSSnapInInfo mshsnapinInfo in mshsnapinInfos)
            {
                try
                {
                    LoadPSSnapIn(mshsnapinInfo);
                    partialSuccess = true;
                }
                catch (PSSnapInException e)
                {
                    if (!mshsnapinInfo.IsDefault)
                    {
                        _consoleInfo.RemovePSSnapIn(mshsnapinInfo.Name);
                    }
                    else
                    {
                        throw;
                    }

                    mshsnapinExceptions.Add(e);
                }
            }

            if (partialSuccess)
            {
                string errors;
                UpdateAll(out errors);

                if (!String.IsNullOrEmpty(errors))
                {
                    s_mshsnapinTracer.TraceWarning(errors);
                    mshsnapinExceptions.Add(new PSSnapInException(null, errors, true));
                }
            }

            if (mshsnapinExceptions.Count > 0)
            {
                warning = new PSConsoleLoadException(_consoleInfo, mshsnapinExceptions);
                s_mshsnapinTracer.TraceWarning(warning.Message);
            }
        }
        /// <summary>
        /// Constructs a <see cref="System.Management.Automation.Runspaces.MshConsoleInfo"/> object from a
        /// Monad console file.
        /// </summary>
        /// <param name="fileName">
        /// Monad console file name. If the filename is not absolute path. Then absolute path is
        /// constructed by using Path.GetFullPath() API.
        /// </param>
        /// <param name="cle">
        /// PSConsoleLoadException occurred while loading this console file. This object
        /// also contains specific PSSnapInExceptions that occurred while loading.
        /// </param>
        /// <exception cref="PSSnapInException">
        /// One or more default mshsnapins cannot be loaded because the
        /// registry is not populated correctly.
        /// </exception>
        /// <exception cref="PSArgumentNullException">
        /// fileName is null.
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// 1. fileName does not specify proper file extension.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// fileName 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 fileName.
        /// </exception>
        internal static MshConsoleInfo CreateFromConsoleFile(string fileName, out PSConsoleLoadException cle)
        {
            s_mshsnapinTracer.WriteLine("Creating console info from file {0}", fileName);

            // Construct default mshsnapins
            MshConsoleInfo consoleInfo = CreateDefaultConfiguration();

            // Check whether the filename specified is an absolute path.
            string absolutePath = Path.GetFullPath(fileName);

            consoleInfo.Filename = absolutePath;

            // Construct externalPSSnapIns by loading file.
            consoleInfo.Load(absolutePath, out cle);

            s_mshsnapinTracer.WriteLine("Console info created successfully");

            return(consoleInfo);
        }
Exemple #17
0
 private static PSConsoleLoadException CombinePSConsoleLoadException(PSConsoleLoadException e1, PSConsoleLoadException e2)
 {
     if (((e1 == null) || (e1.PSSnapInExceptions.Count == 0)) && ((e2 == null) || (e2.PSSnapInExceptions.Count == 0)))
     {
         return(null);
     }
     if ((e1 == null) || (e1.PSSnapInExceptions.Count == 0))
     {
         return(e2);
     }
     if ((e2 != null) && (e2.PSSnapInExceptions.Count != 0))
     {
         foreach (PSSnapInException exception in e2.PSSnapInExceptions)
         {
             e1.PSSnapInExceptions.Add(exception);
         }
     }
     return(e1);
 }
Exemple #18
0
        internal static RunspaceConfigForSingleShell CreateDefaultConfiguration()
        {
            _mshsnapinTracer.WriteLine("Creating default runspace configuration.", new object[0]);
            MshConsoleInfo consoleInfo = MshConsoleInfo.CreateDefaultConfiguration();

            if (consoleInfo != null)
            {
                RunspaceConfigForSingleShell shell   = new RunspaceConfigForSingleShell(consoleInfo);
                PSConsoleLoadException       warning = null;
                shell.LoadConsole(out warning);
                if (warning != null)
                {
                    _mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", new object[] { warning.Message });
                }
                return(shell);
            }
            _mshsnapinTracer.WriteLine("Default runspace configuration created.", new object[0]);
            return(null);
        }
 internal static RunspaceConfigForSingleShell CreateDefaultConfiguration()
 {
     using (RunspaceConfigForSingleShell.tracer.TraceMethod())
     {
         RunspaceConfigForSingleShell._mshsnapinTracer.WriteLine("Creating default runspace configuration.", new object[0]);
         MshConsoleInfo defaultConfiguration = MshConsoleInfo.CreateDefaultConfiguration();
         if (defaultConfiguration != null)
         {
             RunspaceConfigForSingleShell configForSingleShell = new RunspaceConfigForSingleShell(defaultConfiguration);
             PSConsoleLoadException       warning = (PSConsoleLoadException)null;
             configForSingleShell.LoadConsole(out warning);
             if (warning != null)
             {
                 RunspaceConfigForSingleShell._mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", (object)warning.Message);
             }
             return(configForSingleShell);
         }
         RunspaceConfigForSingleShell._mshsnapinTracer.WriteLine("Default runspace configuration created.", new object[0]);
         return((RunspaceConfigForSingleShell)null);
     }
 }
Exemple #20
0
        private void LoadPSSnapIns(Collection <PSSnapInInfo> mshsnapinInfos, out PSConsoleLoadException warning)
        {
            warning = null;
            Collection <PSSnapInException> exceptions = new Collection <PSSnapInException>();
            bool flag = false;

            foreach (PSSnapInInfo info in mshsnapinInfos)
            {
                try
                {
                    this.LoadPSSnapIn(info);
                    flag = true;
                }
                catch (PSSnapInException exception)
                {
                    if (info.IsDefault)
                    {
                        throw;
                    }
                    this._consoleInfo.RemovePSSnapIn(info.Name);
                    exceptions.Add(exception);
                }
            }
            if (flag)
            {
                string str;
                this.UpdateAll(out str);
                if (!string.IsNullOrEmpty(str))
                {
                    _mshsnapinTracer.TraceWarning(str, new object[0]);
                    exceptions.Add(new PSSnapInException(null, str, true));
                }
            }
            if (exceptions.Count > 0)
            {
                warning = new PSConsoleLoadException(this._consoleInfo, exceptions);
                _mshsnapinTracer.TraceWarning(warning.Message, new object[0]);
            }
        }
Exemple #21
0
        private static PSConsoleLoadException CombinePSConsoleLoadException(PSConsoleLoadException e1, PSConsoleLoadException e2)
        {
            if ((e1 == null || e1.PSSnapInExceptions.Count == 0) && (e2 == null || e2.PSSnapInExceptions.Count == 0))
            {
                return(null);
            }

            if (e1 == null || e1.PSSnapInExceptions.Count == 0)
            {
                return(e2);
            }

            if (e2 == null || e2.PSSnapInExceptions.Count == 0)
            {
                return(e1);
            }

            foreach (PSSnapInException sile in e2.PSSnapInExceptions)
            {
                e1.PSSnapInExceptions.Add(sile);
            }

            return(e1);
        }
        private void LoadPSSnapIns(Collection<PSSnapInInfo> mshsnapinInfos, out PSConsoleLoadException warning)
        {
            warning = null;

            Collection<PSSnapInException> mshsnapinExceptions = new Collection<PSSnapInException>();

            bool partialSuccess = false;

            foreach (PSSnapInInfo mshsnapinInfo in mshsnapinInfos)
            {
                try
                {
                    LoadPSSnapIn(mshsnapinInfo);
                    partialSuccess = true;
                }
                catch (PSSnapInException e)
                {
                    if (!mshsnapinInfo.IsDefault)
                    {
                        _consoleInfo.RemovePSSnapIn(mshsnapinInfo.Name);
                    }
                    else
                    {
                        throw;
                    }

                    mshsnapinExceptions.Add(e);
                }
            }

            if (partialSuccess)
            {
                string errors;
                UpdateAll(out errors);

                if (!String.IsNullOrEmpty(errors))
                {
                    s_mshsnapinTracer.TraceWarning(errors);
                    mshsnapinExceptions.Add(new PSSnapInException(null, errors, true));
                }
            }

            if (mshsnapinExceptions.Count > 0)
            {
                warning = new PSConsoleLoadException(_consoleInfo, mshsnapinExceptions);
                s_mshsnapinTracer.TraceWarning(warning.Message);
            }
        }
        private void LoadConsole(out PSConsoleLoadException warning)
        {
            if (_consoleInfo == null)
            {
                warning = null;
                return;
            }

            LoadPSSnapIns(_consoleInfo.PSSnapIns, out warning);
        }
Exemple #24
0
 public static InitialSessionState CreateFrom(string[] snapInPathCollection, out PSConsoleLoadException warnings)
 {
     warnings = null;
     return new InitialSessionState();
 }
Exemple #25
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;
 }
Exemple #26
0
 public static InitialSessionState Create(string[] snapInNameCollection, out PSConsoleLoadException warning)
 {
     warning = null;
     return(new InitialSessionState());
 }
 private static PSConsoleLoadException CombinePSConsoleLoadException(PSConsoleLoadException e1, PSConsoleLoadException e2)
 {
     if (((e1 == null) || (e1.PSSnapInExceptions.Count == 0)) && ((e2 == null) || (e2.PSSnapInExceptions.Count == 0)))
     {
         return null;
     }
     if ((e1 == null) || (e1.PSSnapInExceptions.Count == 0))
     {
         return e2;
     }
     if ((e2 != null) && (e2.PSSnapInExceptions.Count != 0))
     {
         foreach (PSSnapInException exception in e2.PSSnapInExceptions)
         {
             e1.PSSnapInExceptions.Add(exception);
         }
     }
     return e1;
 }
 private void LoadConsole(out PSConsoleLoadException warning)
 {
     if (this._consoleInfo == null)
     {
         warning = null;
     }
     else
     {
         this.LoadPSSnapIns(this._consoleInfo.PSSnapIns, out warning);
     }
 }
Exemple #29
0
        /// <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;
        }
Exemple #30
0
 public static RunspaceConfiguration Create(string consoleFilePath, out PSConsoleLoadException warnings)
 {
     warnings = null;
     return(null);
 }
 /// <summary>
 /// Create one single shell runspace configuration object from console file
 /// </summary>
 /// <param name="consoleFilePath"></param>
 /// <param name="warnings"></param>
 /// <returns></returns>
 public static RunspaceConfiguration Create(string consoleFilePath, out PSConsoleLoadException warnings)
 {
     return RunspaceConfigForSingleShell.Create(consoleFilePath, out warnings);
 }
        private static PSConsoleLoadException CombinePSConsoleLoadException(PSConsoleLoadException e1, PSConsoleLoadException e2)
        {
            if ((e1 == null || e1.PSSnapInExceptions.Count == 0) && (e2 == null || e2.PSSnapInExceptions.Count == 0))
                return null;

            if (e1 == null || e1.PSSnapInExceptions.Count == 0)
                return e2;

            if (e2 == null || e2.PSSnapInExceptions.Count == 0)
                return e1;

            foreach (PSSnapInException sile in e2.PSSnapInExceptions)
            {
                e1.PSSnapInExceptions.Add(sile);
            }

            return e1;
        }
 internal static RunspaceConfigForSingleShell Create(string consoleFile, out PSConsoleLoadException warning)
 {
     PSConsoleLoadException cle = null;
     _mshsnapinTracer.WriteLine("Creating MshConsoleInfo. consoleFile={0}", new object[] { consoleFile });
     MshConsoleInfo consoleInfo = MshConsoleInfo.CreateFromConsoleFile(consoleFile, out cle);
     if (cle != null)
     {
         _mshsnapinTracer.TraceWarning("There was a warning while creating MshConsoleInfo: {0}", new object[] { cle.Message });
     }
     if (consoleInfo != null)
     {
         RunspaceConfigForSingleShell shell = new RunspaceConfigForSingleShell(consoleInfo);
         PSConsoleLoadException exception2 = null;
         shell.LoadConsole(out exception2);
         if (exception2 != null)
         {
             _mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", new object[] { exception2.Message });
         }
         warning = CombinePSConsoleLoadException(cle, exception2);
         return shell;
     }
     warning = null;
     return null;
 }
Exemple #34
0
        /// <summary>
        /// Constructs a <see cref="System.Management.Automation.Runspaces.MshConsoleInfo"/> object from a
        /// Monad console file.
        /// </summary>
        /// <param name="fileName">
        /// Monad console file name. If the filename is not absolute path. Then absolute path is
        /// constructed by using Path.GetFullPath() API.
        /// </param>
        /// <param name="cle">
        /// PSConsoleLoadException occurred while loading this console file. This object
        /// also contains specific PSSnapInExceptions that occurred while loading.
        /// </param>
        /// <exception cref="PSSnapInException">
        /// One or more default mshsnapins cannot be loaded because the
        /// registry is not populated correctly.
        /// </exception>
        /// <exception cref="PSArgumentNullException">
        /// fileName is null.
        /// </exception>
        /// <exception cref="PSArgumentException">
        /// 1. fileName does not specify proper file extension.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// fileName 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 fileName.
        /// </exception>
        internal static MshConsoleInfo CreateFromConsoleFile(string fileName, out PSConsoleLoadException cle)
        {
            s_mshsnapinTracer.WriteLine("Creating console info from file {0}", fileName);

            // Construct default mshsnapins                
            MshConsoleInfo consoleInfo = CreateDefaultConfiguration();

            // Check whether the filename specified is an absolute path.
            string absolutePath = Path.GetFullPath(fileName);
            consoleInfo.Filename = absolutePath;

            // Construct externalPSSnapIns by loading file.
            consoleInfo.Load(absolutePath, out cle);

            s_mshsnapinTracer.WriteLine("Console info created successfully");

            return consoleInfo;
        }
Exemple #35
0
		public static RunspaceConfiguration Create (string consoleFilePath, out PSConsoleLoadException warnings)
		{
			warnings = null;
			return null;
		}
Exemple #36
0
 /// <summary>
 /// Create one single shell runspace configuration object from console file
 /// </summary>
 /// <param name="consoleFilePath"></param>
 /// <param name="warnings"></param>
 /// <returns></returns>
 public static RunspaceConfiguration Create(string consoleFilePath, out PSConsoleLoadException warnings)
 {
     return(RunspaceConfigForSingleShell.Create(consoleFilePath, out warnings));
 }
Exemple #37
0
 public static InitialSessionState CreateFrom(string[] snapInPathCollection, out PSConsoleLoadException warnings)
 {
     warnings = null;
     return(new InitialSessionState());
 }
        /// <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);
        }
 private void LoadPSSnapIns(Collection<PSSnapInInfo> mshsnapinInfos, out PSConsoleLoadException warning)
 {
     warning = null;
     Collection<PSSnapInException> exceptions = new Collection<PSSnapInException>();
     bool flag = false;
     foreach (PSSnapInInfo info in mshsnapinInfos)
     {
         try
         {
             this.LoadPSSnapIn(info);
             flag = true;
         }
         catch (PSSnapInException exception)
         {
             if (info.IsDefault)
             {
                 throw;
             }
             this._consoleInfo.RemovePSSnapIn(info.Name);
             exceptions.Add(exception);
         }
     }
     if (flag)
     {
         string str;
         this.UpdateAll(out str);
         if (!string.IsNullOrEmpty(str))
         {
             _mshsnapinTracer.TraceWarning(str, new object[0]);
             exceptions.Add(new PSSnapInException(null, str, true));
         }
     }
     if (exceptions.Count > 0)
     {
         warning = new PSConsoleLoadException(this._consoleInfo, exceptions);
         _mshsnapinTracer.TraceWarning(warning.Message, new object[0]);
     }
 }
Exemple #40
0
 public static InitialSessionState Create(string[] snapInNameCollection, out PSConsoleLoadException warning)
 {
     warning = null;
     return new InitialSessionState();
 }