예제 #1
0
        /// <summary>
        /// The principal constructor that most hosts will use when creating
        /// an instance of the automation engine. It allows you to pass in an
        /// instance of PSHost that provides the host-specific I/O routines, etc.
        /// </summary>
        internal AutomationEngine(PSHost hostInterface, InitialSessionState iss)
        {
#if !UNIX
            // Update the env variable PATHEXT to contain .CPL
            var pathext = Environment.GetEnvironmentVariable("PATHEXT");

            if (string.IsNullOrEmpty(pathext))
            {
                Environment.SetEnvironmentVariable("PATHEXT", ".CPL");
            }
            else if (!(pathext.EndsWith(";.CPL", StringComparison.OrdinalIgnoreCase) ||
                       pathext.StartsWith(".CPL;", StringComparison.OrdinalIgnoreCase) ||
                       pathext.Contains(";.CPL;", StringComparison.OrdinalIgnoreCase) ||
                       pathext.Equals(".CPL", StringComparison.OrdinalIgnoreCase)))
            {
                // Fast skip if we already added the extention as ";.CPL".
                // Fast skip if user already added the extention.
                pathext += pathext[pathext.Length - 1] == ';' ? ".CPL" : ";.CPL";
                Environment.SetEnvironmentVariable("PATHEXT", pathext);
            }
#endif

            Context = new ExecutionContext(this, hostInterface, iss);

            EngineParser     = new Language.Parser();
            CommandDiscovery = new CommandDiscovery(Context);

            // Load the iss, resetting everything to it's defaults...
            iss.Bind(Context, updateOnly: false, module: null, noClobber: false, local: false, setLocation: true);
        }
예제 #2
0
        /// <summary>
        /// The principal constructor that most hosts will use when creating
        /// an instance of the automation engine. It allows you to pass in an
        /// instance of PSHost that provides the host-specific I/O routines, etc.
        /// </summary>
        internal AutomationEngine(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss)
        {
#if !CORECLR// There is no control panel items in CSS
            // Update the env variable PathEXT to contain .CPL
            var pathext = Environment.GetEnvironmentVariable("PathEXT");
            pathext = pathext ?? string.Empty;
            bool cplExist = false;
            if (pathext != string.Empty)
            {
                string[] entries = pathext.Split(Utils.Separators.Semicolon);
                foreach (string entry in entries)
                {
                    string ext = entry.Trim();
                    if (ext.Equals(".CPL", StringComparison.OrdinalIgnoreCase))
                    {
                        cplExist = true;
                        break;
                    }
                }
            }
            if (!cplExist)
            {
                pathext = (pathext == string.Empty) ? ".CPL" :
                          pathext.EndsWith(";", StringComparison.OrdinalIgnoreCase)
                    ? (pathext + ".CPL") : (pathext + ";.CPL");
                Environment.SetEnvironmentVariable("PathEXT", pathext);
            }
#endif
            if (runspaceConfiguration != null)
            {
                Context = new ExecutionContext(this, hostInterface, runspaceConfiguration);
            }
            else
            {
                Context = new ExecutionContext(this, hostInterface, iss);
            }

            EngineParser     = new Language.Parser();
            CommandDiscovery = new CommandDiscovery(Context);

            // Initialize providers before loading types so that any ScriptBlocks in the
            // types.ps1xml file can be parsed.

            // Bind the execution context with RunspaceConfiguration.
            // This has the side effect of initializing cmdlet cache and providers from runspace configuration.
            if (runspaceConfiguration != null)
            {
                runspaceConfiguration.Bind(Context);
            }
            else
            {
                // Load the iss, resetting everything to it's defaults...
                iss.Bind(Context, /*updateOnly*/ false);
            }

            InitialSessionState.SetSessionStateDrive(Context, true);

            InitialSessionState.CreateQuestionVariable(Context);
        }
예제 #3
0
        /// <summary>
        /// The principal constructor that most hosts will use when creating
        /// an instance of the automation engine. It allows you to pass in an
        /// instance of PSHost that provides the host-specific I/O routines, etc.
        /// </summary>
        internal AutomationEngine(PSHost hostInterface, InitialSessionState iss)
        {
            Context = new ExecutionContext(this, hostInterface, iss);

            EngineParser     = new Language.Parser();
            CommandDiscovery = new CommandDiscovery(Context);

            // Load the iss, resetting everything to it's defaults...
            iss.Bind(Context, /*updateOnly*/ false);

            InitialSessionState.SetSessionStateDrive(Context, true);
        }
예제 #4
0
        internal AutomationEngine(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss)
        {
            string str  = Environment.GetEnvironmentVariable("PathEXT") ?? string.Empty;
            bool   flag = false;

            if (str != string.Empty)
            {
                foreach (string str2 in str.Split(new char[] { ';' }))
                {
                    if (str2.Trim().Equals(".CPL", StringComparison.OrdinalIgnoreCase))
                    {
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag)
            {
                str = (str == string.Empty) ? ".CPL" : (str.EndsWith(";", StringComparison.OrdinalIgnoreCase) ? (str + ".CPL") : (str + ";.CPL"));
                Environment.SetEnvironmentVariable("PathEXT", str);
            }
            if (runspaceConfiguration != null)
            {
                this._context = new ExecutionContext(this, hostInterface, runspaceConfiguration);
            }
            else
            {
                this._context = new ExecutionContext(this, hostInterface, iss);
            }
            this.EngineNewParser  = new Parser();
            this.commandDiscovery = new System.Management.Automation.CommandDiscovery(this._context);
            if (runspaceConfiguration != null)
            {
                runspaceConfiguration.Bind(this._context);
            }
            else
            {
                iss.Bind(this._context, false);
            }
            InitialSessionState.SetSessionStateDrive(this._context, true);
            InitialSessionState.CreateQuestionVariable(this._context);
        }
예제 #5
0
        /// <summary>
        /// The principal constructor that most hosts will use when creating
        /// an instance of the automation engine. It allows you to pass in an
        /// instance of PSHost that provides the host-specific I/O routines, etc.
        /// </summary>
        internal AutomationEngine(PSHost hostInterface, InitialSessionState iss)
        {
#if !UNIX
            // Update the env variable PathEXT to contain .CPL
            var pathext = Environment.GetEnvironmentVariable("PathEXT");
            pathext = pathext ?? string.Empty;
            bool cplExist = false;
            if (pathext != string.Empty)
            {
                string[] entries = pathext.Split(Utils.Separators.Semicolon);
                foreach (string entry in entries)
                {
                    string ext = entry.Trim();
                    if (ext.Equals(".CPL", StringComparison.OrdinalIgnoreCase))
                    {
                        cplExist = true;
                        break;
                    }
                }
            }

            if (!cplExist)
            {
                pathext = (pathext == string.Empty) ? ".CPL" :
                          pathext.EndsWith(";", StringComparison.OrdinalIgnoreCase)
                    ? (pathext + ".CPL") : (pathext + ";.CPL");
                Environment.SetEnvironmentVariable("PathEXT", pathext);
            }
#endif

            Context = new ExecutionContext(this, hostInterface, iss);

            EngineParser     = new Language.Parser();
            CommandDiscovery = new CommandDiscovery(Context);

            // Load the iss, resetting everything to it's defaults...
            iss.Bind(Context, /*updateOnly*/ false);

            InitialSessionState.SetSessionStateDrive(Context, true);
        }
        /// <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);
                }
            }
        }
예제 #7
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);
                 }
             }
         }
     }
 }
예제 #8
0
 internal AutomationEngine(
     PSHost hostInterface,
     RunspaceConfiguration runspaceConfiguration,
     InitialSessionState iss)
 {
     this._context         = runspaceConfiguration == null ? new ExecutionContext(this, hostInterface, iss) : new ExecutionContext(this, hostInterface, runspaceConfiguration);
     this.EngineParser     = new Parser();
     this.commandDiscovery = new CommandDiscovery(this._context);
     if (runspaceConfiguration != null)
     {
         runspaceConfiguration.Bind(this._context);
     }
     else
     {
         iss.Bind(this._context, false);
     }
     try
     {
         bool flag1 = true;
         if (this._context.EngineSessionState.ProviderCount > 0)
         {
             if (this._context.EngineSessionState.CurrentDrive == (PSDriveInfo)null)
             {
                 bool flag2 = false;
                 try
                 {
                     Collection <PSDriveInfo> drives = this._context.EngineSessionState.GetSingleProvider(this._context.ProviderNames.FileSystem).Drives;
                     if (drives != null)
                     {
                         if (drives.Count > 0)
                         {
                             this._context.EngineSessionState.CurrentDrive = drives[0];
                             flag2 = true;
                         }
                     }
                 }
                 catch (ProviderNotFoundException ex)
                 {
                 }
                 if (!flag2)
                 {
                     Collection <PSDriveInfo> collection = this._context.EngineSessionState.Drives((string)null);
                     if (collection != null && collection.Count > 0)
                     {
                         this._context.EngineSessionState.CurrentDrive = collection[0];
                     }
                     else
                     {
                         this._context.ReportEngineStartupError((Exception) new ItemNotFoundException(Environment.CurrentDirectory, "PathNotFound"));
                         flag1 = false;
                     }
                 }
             }
             if (flag1)
             {
                 this._context.EngineSessionState.SetLocation(Environment.CurrentDirectory, new CmdletProviderContext(this._context));
             }
         }
         this._context.EngineSessionState.SetVariableAtScope((PSVariable) new QuestionMarkVariable(this._context), "global", true, CommandOrigin.Internal);
     }
     catch (Exception ex)
     {
         CommandProcessorBase.CheckForSevereException(ex);
     }
 }