SHARE
TWEET

PowerShell StreamHost implementation in c#

a guest Oct 13th, 2012 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * A simple PowerShell StreamHost
  3.  *
  4.  * Copyright (c) 2012 Michael 'mihi' Schierl
  5.  *
  6.  * Used to run PowerShell where no Console is available (like in embedded consoles or via telnet)
  7.  * Note that the PowerShell experience will be limited (no dynamic prompts, no scrolling, etc.)
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Management.Automation;
  13. using System.Management.Automation.Host;
  14. using System.Management.Automation.Runspaces;
  15. using System.Threading;
  16.  
  17. namespace StreamHost
  18. {
  19.     class ConsoleUI : PSHostUserInterface
  20.     {
  21.         public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
  22.         {
  23.             return null;
  24.         }
  25.  
  26.         public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
  27.         {
  28.             return -1;
  29.         }
  30.  
  31.         public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
  32.         {
  33.             return null;
  34.         }
  35.  
  36.         public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
  37.         {
  38.             return null;
  39.         }
  40.  
  41.         public override PSHostRawUserInterface RawUI
  42.         {
  43.             get { return null; }
  44.         }
  45.  
  46.         public override string ReadLine()
  47.         {
  48.             return Console.In.ReadLine();
  49.         }
  50.  
  51.         public override System.Security.SecureString ReadLineAsSecureString()
  52.         {
  53.             return null;
  54.         }
  55.  
  56.         public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
  57.         {
  58.             Console.Out.Write(value);
  59.         }
  60.  
  61.         public override void Write(string value)
  62.         {
  63.             Console.Out.Write(value);
  64.         }
  65.  
  66.         public override void WriteDebugLine(string message)
  67.         {
  68.             Console.Error.WriteLine(message);
  69.         }
  70.  
  71.         public override void WriteErrorLine(string value)
  72.         {
  73.             Console.Error.WriteLine(value);
  74.         }
  75.  
  76.         public override void WriteLine(string value)
  77.         {
  78.             Console.Out.WriteLine(value);
  79.         }
  80.  
  81.         public override void WriteProgress(long sourceId, ProgressRecord record)
  82.         {
  83.         }
  84.  
  85.         public override void WriteVerboseLine(string message)
  86.         {
  87.             Console.Error.WriteLine(message);
  88.         }
  89.  
  90.         public override void WriteWarningLine(string message)
  91.         {
  92.             Console.Error.WriteLine(message);
  93.         }
  94.     }
  95.     class StreamHost : PSHost
  96.     {
  97.         ConsoleUI ui = new ConsoleUI();
  98.         public int? e = null;
  99.         Guid id = Guid.NewGuid();
  100.  
  101.         public override System.Globalization.CultureInfo CurrentCulture
  102.         {
  103.             get { return Thread.CurrentThread.CurrentCulture; }
  104.         }
  105.  
  106.         public override System.Globalization.CultureInfo CurrentUICulture
  107.         {
  108.             get { return Thread.CurrentThread.CurrentUICulture; }
  109.         }
  110.  
  111.         public override void EnterNestedPrompt()
  112.         {
  113.             throw new NotImplementedException();
  114.         }
  115.  
  116.         public override void ExitNestedPrompt()
  117.         {
  118.             throw new NotImplementedException();
  119.         }
  120.  
  121.         public override Guid InstanceId
  122.         {
  123.             get { return id; }
  124.         }
  125.  
  126.         public override string Name
  127.         {
  128.             get { return "StreamHost"; }
  129.         }
  130.  
  131.         public override void NotifyBeginApplication()
  132.         {
  133.         }
  134.  
  135.         public override void NotifyEndApplication()
  136.         {
  137.         }
  138.         public override void SetShouldExit(int exitCode)
  139.         {
  140.             e = exitCode;
  141.         }
  142.  
  143.         public override PSHostUserInterface UI
  144.         {
  145.             get { return ui; }
  146.         }
  147.  
  148.         public override Version Version
  149.         {
  150.             get { return new Version(1, 0); }
  151.         }
  152.     }
  153.     static class Program
  154.     {
  155.         static void Main(string[] args)
  156.         {
  157.             StreamHost h = new StreamHost();
  158.             Runspace r = RunspaceFactory.CreateRunspace(h);
  159.             r.Open();
  160.             PowerShell p = PowerShell.Create();
  161.             p.Runspace = r;
  162.             while (h.e == null)
  163.             {
  164.                 Console.Out.Write(r.CreatePipeline("prompt").Invoke()[0]);
  165.                 p.Commands.Clear();
  166.                 p.AddScript(Console.In.ReadLine()).AddCommand("out-default");
  167.                 p.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
  168.                 p.Invoke();
  169.             }
  170.             p.Dispose();
  171.             Environment.Exit(h.e.Value);
  172.         }
  173.     }
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top