public static void WriteEntry(this IPsUserInterface logger, PsStreamEntry entry)
        {
            switch (entry.Type)
            {
            case PsStreamEntryType.Information:
                logger.WriteMessage(entry.Message);
                break;

            case PsStreamEntryType.Warning:
                logger.WriteWarning(entry.Message);
                break;

            case PsStreamEntryType.Error:
                logger.WriteError(entry.Message, null);
                break;

            case PsStreamEntryType.Verbose:
                logger.WriteVerbose(entry.Message);
                break;

            case PsStreamEntryType.Debug:
                logger.WriteDebug(entry.Message);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #2
0
        public PsRunner(IPsUserInterface psUserInterface)
        {
            Logger        = psUserInterface;
            DiscardOutput = false;

            Host = new SimplePsHost(psUserInterface);
            var rs = RunspaceFactory.CreateRunspace(Host);

            PsInstance          = System.Management.Automation.PowerShell.Create();
            PsInstance.Runspace = rs;
            PsInstance.InvocationStateChanged += (s, e) => InvocationStateChanged?.Invoke(this, e);
            rs.Open();

            PsInstance.Streams.Debug.DataAdded += delegate(object s, DataAddedEventArgs e)
            {
                Logger.WriteDebug(PsInstance.Streams.Debug[e.Index].Message);
            };
            PsInstance.Streams.Error.DataAdded += delegate(object s, DataAddedEventArgs e)
            {
                var er = PsInstance.Streams.Error[e.Index];
                Logger.WriteError(er.Exception.GetExceptionMessage(), er.TargetObject);
            };
            PsInstance.Streams.Progress.DataAdded += delegate(object s, DataAddedEventArgs e)
            {
                var pr = PsInstance.Streams.Progress[e.Index];
                Logger.ReportProgress(pr.ActivityId, pr);
            };
            PsInstance.Streams.Verbose.DataAdded += delegate(object s, DataAddedEventArgs e)
            {
                Logger.WriteVerbose(PsInstance.Streams.Verbose[e.Index].Message);
            };
            PsInstance.Streams.Warning.DataAdded += delegate(object s, DataAddedEventArgs e)
            {
                Logger.WriteWarning(PsInstance.Streams.Warning[e.Index].Message);
            };
        }