public PSRemoteUserInterface(InputOutputBuffers buffers)
 {
     _buffers = buffers;
     _buffers.InterceptInCommand(cmd =>
                                     {
                                         _rawUI.BufferSize = new Size(cmd.Columns, 25);
                                     });
 }
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();

            string listenAddress = null;
            string script = null;
            bool help = false;
            var options = new OptionSet
                        {
                            {"listen=", x => listenAddress = x },
                            {"script=", x => script = x },
                            {"h|?|help", x => help = x != null},
                        };

            options.Parse(args);
            if (listenAddress == null || help)
            {
                PrintUsage();
                return;
            }

            var buffers = new InputOutputBuffers();

            Log.Info("Initializing PowerShell");
            var powerShell = new PSWrapper(buffers, () => ExitEvent.Set());

            buffers.RegisterForInCommand((cmd, scope) => powerShell.TryExecute(cmd.TextLine));

            var config = new HttpSelfHostConfiguration(listenAddress);
            config.Services.Insert(typeof(IHttpControllerActivator), 0, new ControllerActivator(buffers));

            config.Routes.MapHttpRoute("session", "session", new { controller = "Session" });
            config.Routes.MapHttpRoute("content", "{contentFile}", new { controller = "Content" });

            var server = new HttpSelfHostServer(config);
            Log.InfoFormat("Staring HTTP listener at {0}", listenAddress);
            server.OpenAsync().Wait();

            if (script != null)
            {
                RunScript(script, powerShell);
            }
            else
            {
                StartInteractivePrompt(buffers);
            }
            Log.InfoFormat("System ready");
            ExitEvent.Wait();
            server.CloseAsync().Wait();

            powerShell.Dispose();
        }
        public PSWrapper(InputOutputBuffers buffers, Action exitCallback)
        {
            _buffers = buffers;
            _exitCallback = exitCallback;
            _psRemoteHost = new PSRemoteHost(buffers, this);

            var sessionState = InitialSessionState.CreateDefault();
            sessionState.Variables.Add(new SessionStateVariableEntry("cls_handler", new ClearHostHandler(buffers),"cls_handler"));

            _runspace = RunspaceFactory.CreateRunspace(_psRemoteHost, sessionState);
            _runspace.Open();

            using (var powerShell = PowerShell.Create())
            {
                powerShell.Runspace = _runspace;
                powerShell.AddScript("function Clear-Host() { $cls_handler.Clear() }");
                powerShell.Invoke();
            }
        }
 public PSRemoteHost(InputOutputBuffers buffers, IPSRemoteHostCallback callbacks)
 {
     _callbacks = callbacks;
     _psRemoteUserInterface = new PSRemoteUserInterface(buffers);
 }
 public ControllerActivator(InputOutputBuffers buffers)
 {
     _buffers = buffers;
 }
 private static void StartInteractivePrompt(InputOutputBuffers buffers)
 {
     Log.Info("Staring interactive prompt");
     buffers.QueueOutCommand(OutCommand.CreateReadLine(false, null));
 }