Exemple #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, 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);
        }
        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);
        }
 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);
     }
 }