Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * A simple PowerShell StreamHost
- *
- * Copyright (c) 2012 Michael 'mihi' Schierl
- *
- * Used to run PowerShell where no Console is available (like in embedded consoles or via telnet)
- * Note that the PowerShell experience will be limited (no dynamic prompts, no scrolling, etc.)
- */
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Management.Automation;
- using System.Management.Automation.Host;
- using System.Management.Automation.Runspaces;
- using System.Threading;
- namespace StreamHost
- {
- class ConsoleUI : PSHostUserInterface
- {
- public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
- {
- return null;
- }
- public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
- {
- return -1;
- }
- public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
- {
- return null;
- }
- public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
- {
- return null;
- }
- public override PSHostRawUserInterface RawUI
- {
- get { return null; }
- }
- public override string ReadLine()
- {
- return Console.In.ReadLine();
- }
- public override System.Security.SecureString ReadLineAsSecureString()
- {
- return null;
- }
- public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
- {
- Console.Out.Write(value);
- }
- public override void Write(string value)
- {
- Console.Out.Write(value);
- }
- public override void WriteDebugLine(string message)
- {
- Console.Error.WriteLine(message);
- }
- public override void WriteErrorLine(string value)
- {
- Console.Error.WriteLine(value);
- }
- public override void WriteLine(string value)
- {
- Console.Out.WriteLine(value);
- }
- public override void WriteProgress(long sourceId, ProgressRecord record)
- {
- }
- public override void WriteVerboseLine(string message)
- {
- Console.Error.WriteLine(message);
- }
- public override void WriteWarningLine(string message)
- {
- Console.Error.WriteLine(message);
- }
- }
- class StreamHost : PSHost
- {
- ConsoleUI ui = new ConsoleUI();
- public int? e = null;
- Guid id = Guid.NewGuid();
- public override System.Globalization.CultureInfo CurrentCulture
- {
- get { return Thread.CurrentThread.CurrentCulture; }
- }
- public override System.Globalization.CultureInfo CurrentUICulture
- {
- get { return Thread.CurrentThread.CurrentUICulture; }
- }
- public override void EnterNestedPrompt()
- {
- throw new NotImplementedException();
- }
- public override void ExitNestedPrompt()
- {
- throw new NotImplementedException();
- }
- public override Guid InstanceId
- {
- get { return id; }
- }
- public override string Name
- {
- get { return "StreamHost"; }
- }
- public override void NotifyBeginApplication()
- {
- }
- public override void NotifyEndApplication()
- {
- }
- public override void SetShouldExit(int exitCode)
- {
- e = exitCode;
- }
- public override PSHostUserInterface UI
- {
- get { return ui; }
- }
- public override Version Version
- {
- get { return new Version(1, 0); }
- }
- }
- static class Program
- {
- static void Main(string[] args)
- {
- StreamHost h = new StreamHost();
- Runspace r = RunspaceFactory.CreateRunspace(h);
- r.Open();
- PowerShell p = PowerShell.Create();
- p.Runspace = r;
- while (h.e == null)
- {
- Console.Out.Write(r.CreatePipeline("prompt").Invoke()[0]);
- p.Commands.Clear();
- p.AddScript(Console.In.ReadLine()).AddCommand("out-default");
- p.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
- p.Invoke();
- }
- p.Dispose();
- Environment.Exit(h.e.Value);
- }
- }
- }
Add Comment
Please, Sign In to add comment