Esempio n. 1
0
        static void Main(string[] args)
        {
            try
            {
                //Cross-Platform Environment Variables
                Environment.SetEnvironmentVariable("LOCALAPPDATA", "~/.config");
                Environment.SetEnvironmentVariable("MONO_XMLSERIALIZER_THS", "no");
                Environment.SetEnvironmentVariable("MONO_XMLSERIALIZER_DEBUG", "0");


                Console.TreatControlCAsInput = true;
                ConsoleShell.Start(RunspaceConfiguration.Create(), "PowerShell", "", new string[] { "-ImportSystemModules" });
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine(ex.Message);
                Console.WriteLine();
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine();
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Starts managed MSH.
        /// </summary>
        /// <param name="consoleFilePath">
        /// Deprecated: Console file used to create a runspace configuration to start MSH
        /// </param>
        /// <param name="args">
        /// Command line arguments to the managed MSH
        /// </param>
        /// <param name="argc">
        /// Length of the passed in argument array.
        /// </param>
        public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)] string[] args, int argc)
        {
            // Warm up some components concurrently on background threads.
            EarlyStartup.Init();

            // We need to read the settings file before we create the console host
            CommandLineParameterParser.EarlyParse(args);

#if !UNIX
            // NOTE: On Unix, logging has to be deferred until after command-line parsing
            // complete. On Windows, deferring the call is not needed.
            PSEtwLog.LogConsoleStartup();
#endif

            // Windows Vista and later support non-traditional UI fallback ie., a
            // user on an Arabic machine can choose either French or English(US) as
            // UI fallback language.
            // CLR does not support this (non-traditional) fallback mechanism.
            // The currentUICulture returned NativeCultureResolver supports this non
            // traditional fallback on Vista. So it is important to set currentUICulture
            // in the beginning before we do anything.
            Thread.CurrentThread.CurrentUICulture = NativeCultureResolver.UICulture;
            Thread.CurrentThread.CurrentCulture   = NativeCultureResolver.Culture;

#if DEBUG
            if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) && args[0].Equals("-isswait", StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Attach the debugger to continue...");
                while (!System.Diagnostics.Debugger.IsAttached)
                {
                    Thread.Sleep(100);
                }

                System.Diagnostics.Debugger.Break();
            }
#endif
            int exitCode = 0;
            try
            {
                var banner = string.Format(
                    CultureInfo.InvariantCulture,
                    ManagedEntranceStrings.ShellBannerNonWindowsPowerShell,
                    PSVersionInfo.GitCommitId);

                exitCode = ConsoleShell.Start(banner, ManagedEntranceStrings.UsageHelp, args);
            }
            catch (HostException e)
            {
                if (e.InnerException != null && e.InnerException.GetType() == typeof(Win32Exception))
                {
                    Win32Exception win32e = e.InnerException as Win32Exception;

                    // These exceptions are caused by killing conhost.exe
                    // 1236, network connection aborted by local system
                    // 0x6, invalid console handle
                    if (win32e.NativeErrorCode == 0x6 || win32e.NativeErrorCode == 1236)
                    {
                        return(exitCode);
                    }
                }

                System.Environment.FailFast(e.Message, e);
            }
            catch (Exception e)
            {
                System.Environment.FailFast(e.Message, e);
            }

            return(exitCode);
        }
Esempio n. 3
0
        public int Start(string consoleFilePath, string[] args)
        {
            int    num;
            string message;
            Guid   activityId = EtwActivity.GetActivityId();

            if (activityId == Guid.Empty)
            {
                EtwActivity.SetActivityId(EtwActivity.CreateActivityId());
            }
            PSEtwLog.LogOperationalInformation(PSEventId.Perftrack_ConsoleStartupStart, PSOpcode.WinStart, PSTask.PowershellConsoleStartup, PSKeyword.UseAlwaysOperational, new object[0]);
            WindowsErrorReporting.RegisterWindowsErrorReporting(false);
            try
            {
                Thread.CurrentThread.CurrentUICulture = NativeCultureResolver.UICulture;
                Thread.CurrentThread.CurrentCulture   = NativeCultureResolver.Culture;
                RunspaceConfigForSingleShell runspaceConfigForSingleShell = null;
                PSConsoleLoadException       pSConsoleLoadException       = null;
                if (!string.IsNullOrEmpty(consoleFilePath))
                {
                    runspaceConfigForSingleShell = RunspaceConfigForSingleShell.Create(consoleFilePath, out pSConsoleLoadException);
                }
                else
                {
                    ConsoleHost.DefaultInitialSessionState = InitialSessionState.CreateDefault2();
                    runspaceConfigForSingleShell           = null;
                    if (Process.GetCurrentProcess().MainWindowHandle != IntPtr.Zero)
                    {
                        ConsoleHost.DefaultInitialSessionState.WarmUpTabCompletionOnIdle = true;
                    }
                }
                int num1 = 0;
                try
                {
                    RunspaceConfigForSingleShell runspaceConfigForSingleShell1 = runspaceConfigForSingleShell;
                    string shellBanner = ManagedEntranceStrings.ShellBanner;
                    string shellHelp   = ManagedEntranceStrings.ShellHelp;
                    if (pSConsoleLoadException == null)
                    {
                        message = null;
                    }
                    else
                    {
                        message = pSConsoleLoadException.Message;
                    }
                    num1 = ConsoleShell.Start(runspaceConfigForSingleShell1, shellBanner, shellHelp, message, args);
                }
                catch (HostException hostException1)
                {
                    HostException hostException = hostException1;
                    if (hostException.InnerException != null && hostException.InnerException.GetType() == typeof(Win32Exception))
                    {
                        Win32Exception innerException = hostException.InnerException as Win32Exception;
                        if (innerException.NativeErrorCode == 6 || innerException.NativeErrorCode == 0x4d4)
                        {
                            num = num1;
                            return(num);
                        }
                    }
                    WindowsErrorReporting.FailFast(hostException);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    WindowsErrorReporting.FailFast(exception);
                }
                num = num1;
            }
            finally
            {
                WindowsErrorReporting.WaitForPendingReports();
            }
            return(num);
        }
Esempio n. 4
0
 public static int Start(RunspaceConfiguration configuration, string bannerText, string helpText, string[] args)
 {
     return(ConsoleShell.Start(configuration, bannerText, helpText, null, args));
 }