Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
HelpersHostCS.psm1318 linesDownload Raw Back to HelpersHostCS
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3$definition = @'4using System;5using System.Collections.Generic;6using System.Threading;7using System.Management.Automation;8using System.Management.Automation.Host;9using System.Globalization;10using System.Collections.ObjectModel;11using System.Security;12using System.Collections;13 14namespace TestHost15{16    public class TestHostRawUI : PSHostRawUserInterface17    {18        private ConsoleColor _backgroundColor = ConsoleColor.Black;19        public override ConsoleColor BackgroundColor20        {21            get { return _backgroundColor; }22            set { _backgroundColor = value; }23        }24        private ConsoleColor _foregroundColor = ConsoleColor.White;25        public override ConsoleColor ForegroundColor26        {27            get { return _foregroundColor; }28            set { _foregroundColor = value; }29        }30        private string _windowTitle = "title";31        public override string WindowTitle32        {33            get { return _windowTitle; }34            set { _windowTitle = value; }35        }36        private Size _bufferSize = new Size(10,10);37        public override Size BufferSize38        {39            get { return _bufferSize; }40            set { _bufferSize = value; }41        }42        private Coordinates _cursorPosition = new Coordinates(0, 0);43        public override Coordinates CursorPosition44        {45            get { return _cursorPosition; }46            set { _cursorPosition = value; }47        }48        private int _cursorSize = 10;49        public override int CursorSize50        {51            get { return _cursorSize; }52            set { _cursorSize = value; }53        }54        private bool _keyAvailable = false;55        public override bool KeyAvailable56        {57            get { return _keyAvailable; }58        }59        public override Size MaxPhysicalWindowSize60        {61            get { throw new NotImplementedException(); }62        }63        public override Size MaxWindowSize64        {65            get { throw new NotImplementedException(); }66        }67        private Coordinates _windowPosition = new Coordinates(0, 0);68        public override Coordinates WindowPosition69        {70            get { return _windowPosition; }71 72            set { _windowPosition = value; }73        }74        private Size _windowSize = new Size(80, 40);75        public override Size WindowSize76        {77            get { return _windowSize; }78            set { _windowSize = value; }79        }80        public override BufferCell[,] GetBufferContents(Rectangle rectangle) { throw new NotImplementedException(); }81        public override void FlushInputBuffer() { throw new NotImplementedException(); }82        public override KeyInfo ReadKey(ReadKeyOptions options) { throw new NotImplementedException(); }83        public override void SetBufferContents(Coordinates origin, BufferCell[,] contents) { throw new NotImplementedException(); }84        public override void SetBufferContents(Rectangle rectangle, BufferCell fill) { throw new NotImplementedException(); }85        public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill) { throw new NotImplementedException(); }86    }87 88    public class Streams89    {90        public ArrayList ConsoleOutput = new ArrayList();91        public ArrayList Input         = new ArrayList();92        public ArrayList Error         = new ArrayList();93        public ArrayList Verbose       = new ArrayList();94        public ArrayList Debug         = new ArrayList();95        public ArrayList Warning       = new ArrayList();96        public ArrayList Information   = new ArrayList();97        public ArrayList Progress      = new ArrayList();98        public ArrayList Prompt        = new ArrayList();99        public void Clear() {100            ConsoleOutput.Clear();101            Input.Clear();102            Error.Clear();103            Verbose.Clear();104            Debug.Clear();105            Warning.Clear();106            Information.Clear();107            Progress.Clear();108            Prompt.Clear();109        }110    }111    public class TestPSHostUserInterface : PSHostUserInterface112    {113        private PSHostRawUserInterface _rawui = new TestHostRawUI();114        public string ReadLineData = "This is readline data";115        public int PromptedChoice = 0;116        public string StringForSecureString = "TEST";117        public string UserNameForCredential = "Admin";118        public object promptResponse = "this is a prompt response";119 120        public Streams Streams = new Streams();121        public override PSHostRawUserInterface RawUI122        {123            get { return _rawui; }124        }125 126        public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)127        {128            if (descriptions == null || descriptions[0] == null)129            {130                throw new ArgumentException("descriptions");131            }132 133            string s = descriptions[0].Name;134            Streams.Prompt.Add(caption + ":" + message + ":" + s);135            Dictionary<string, PSObject> d = new Dictionary<string, PSObject>();136            d.Add(s, new PSObject(promptResponse));137            return d;138        }139 140        public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)141        {142            Streams.Prompt.Add(caption + ":" + message);143            return PromptedChoice;144        }145 146        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)147        {148            Streams.Prompt.Add("Credential:" + caption + ":" + message);149            SecureString ss = ReadLineAsSecureString();150            string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;151            return new PSCredential(userNameToUse, ss);152        }153 154        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)155        {156            Streams.Prompt.Add("Credential:" + caption + ":" + message);157            SecureString ss = ReadLineAsSecureString();158            string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;159            return new PSCredential(userNameToUse, ss);160        }161 162        public override string ReadLine()163        {164            return ReadLineData;165        }166 167        public override SecureString ReadLineAsSecureString()168        {169            SecureString ss = new SecureString();170            foreach(char c in StringForSecureString.ToCharArray()) { ss.AppendChar(c); }171            return ss;172        }173 174        // Cmdlets call 'Write' and 'WriteLine' methods implicitly.175        // To see difference between 'Write' and 'WriteLine' with and w/o colors in the debug output176        // we need use a meta information.177        // So we make a output string as:178        // <Foregraund color name> : <Background color name> : <'user value'> : <'NewLine' or 'NoNewLine'>179        //180        public override void Write(string value)181        {182            Streams.ConsoleOutput.Add("::"+value+":NoNewLine");183        }184 185        public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)186        {187            Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NoNewLine");188        }189 190        public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)191        {192            Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NewLine");193        }194 195        public override void WriteDebugLine(string message)196        {197            Streams.Debug.Add(message);198        }199 200        public override void WriteErrorLine(string value)201        {202            Streams.Error.Add(value);203        }204 205        public override void WriteLine(string value)206        {207            Streams.ConsoleOutput.Add("::"+value+":NewLine");208        }209 210        public override void WriteProgress(long sourceId, ProgressRecord record)211        {212            Streams.Progress.Add(record);213        }214 215        public override void WriteVerboseLine(string message)216        {217            Streams.Verbose.Add(message);218        }219 220        public override void WriteWarningLine(string message)221        {222            Streams.Warning.Add(message);223        }224 225        public override void WriteInformation(InformationRecord record)226        {227            HostInformationMessage hostOutput = record.MessageData as HostInformationMessage;228            if (hostOutput != null) {229                 string message = hostOutput.Message;230                 Streams.Information.Add(message);231            }232        }233    }234 235    public class TestHost : PSHost236    {237        private Guid _instanceId = Guid.NewGuid();238        private PSHostUserInterface _ui = new TestPSHostUserInterface();239        public string _hostName = "TEST HOST";240        public Version _version = new Version(6, 0);241        int promptLevel = 0;242        public override CultureInfo CurrentCulture243        {244            get { return Thread.CurrentThread.CurrentCulture; }245        }246 247        public override CultureInfo CurrentUICulture248        {249            get {  return Thread.CurrentThread.CurrentUICulture; }250        }251 252        public override Guid InstanceId253        {254            get { return _instanceId; }255        }256 257        public override string Name258        {259            get { return _hostName; }260        }261 262        public override PSHostUserInterface UI263        {264            get { return _ui; }265        }266 267        public override Version Version268        {269            get { return _version; }270        }271 272        public override void EnterNestedPrompt()273        {274            promptLevel++;275        }276 277        public override void ExitNestedPrompt()278        {279            promptLevel--;280        }281 282        public override void NotifyBeginApplication()283        {284            throw new NotImplementedException();285        }286 287        public override void NotifyEndApplication()288        {289            throw new NotImplementedException();290        }291 292        public override void SetShouldExit(int exitCode)293        {294            throw new NotImplementedException();295        }296    }297}298 299'@300## '301function New-TestHost302{303    If ($IsCoreCLR)304    {305        $references = @()306    }307    else308    {309        $references = "System.Management.Automation"310    }311 312    if ( ! ("TestHost.TestHost" -as "type" )) {313       $t = Add-Type -pass $definition -ref $references314    }315 316    [TestHost.TestHost]::New()317}318