Esempio n. 1
0
        public virtual Collection <PSObject> RunPowerShellTest(params string[] scripts)
        {
            Collection <PSObject> output = null;

            for (int i = 0; i < scripts.Length; ++i)
            {
                Console.WriteLine(scripts[i]);
                powershell.AddScript(scripts[i]);
            }
            try
            {
                output = powershell.Invoke();

                if (powershell.Streams.Error.Count > 0)
                {
                    throw new RuntimeException(ErrorIsNotEmptyException);
                }

                return(output);
            }
            catch (Exception psException)
            {
                powershell.LogPowerShellException(psException, this.TestContext);
                throw;
            }
            finally
            {
                powershell.LogPowerShellResults(output, this.TestContext);
                powershell.Streams.Error.Clear();
            }
        }
        /// <summary>
        /// Logs a PowerShell exception thrown from PowerShell.Invoke, parsing the inner
        /// PowerShell error record if available
        /// </summary>
        /// <param name="runtimeException">The exception to parse</param>
        public static void LogPowerShellException(
            this System.Management.Automation.PowerShell powershell,
            Exception runtimeException,
            XunitTracingInterceptor xunitLogger)
        {
            if (xunitLogger != null)
            {
                xunitLogger.Information(string.Format("Caught Exception: {0}", runtimeException));
                xunitLogger.Information(string.Format("Message: {0}", runtimeException.Message));
            }

            IContainsErrorRecord recordContainer = runtimeException as IContainsErrorRecord;

            if (recordContainer != null)
            {
                ErrorRecord record = recordContainer.ErrorRecord;

                if (xunitLogger != null)
                {
                    xunitLogger.Information(FormatErrorRecord(record));
                }
            }

            if (runtimeException.InnerException != null)
            {
                powershell.LogPowerShellException(runtimeException.InnerException, xunitLogger);
            }
        }
        /// <summary>
        /// Logs a PowerShell exception thrown from PowerShell.Invoke, parsing the inner
        /// PowerShell error record if available
        /// </summary>
        /// <param name="runtimeException">The exception to parse</param>
        public static void LogPowerShellException(
            this System.Management.Automation.PowerShell powershell,
            Exception runtimeException,
            XunitTracingInterceptor xunitLogger)
        {
            if (xunitLogger != null)
            {
                xunitLogger.Information(string.Format("Caught Exception: {0}", runtimeException));
                xunitLogger.Information(string.Format("Message: {0}", runtimeException.Message));
            }

            IContainsErrorRecord recordContainer = runtimeException as IContainsErrorRecord;

            if (recordContainer != null)
            {
                ErrorRecord record = recordContainer.ErrorRecord;

                if (xunitLogger != null)
                {
                    xunitLogger.Information(string.Format(
                                                "PowerShell Error Record: {0}\nException:{1}\nDetails:{2}\nScript Stack Trace: {3}\n: Target: {4}\n",
                                                record,
                                                record.Exception,
                                                record.ErrorDetails,
                                                record.ScriptStackTrace,
                                                record.TargetObject));
                }
            }

            if (runtimeException.InnerException != null)
            {
                powershell.LogPowerShellException(runtimeException.InnerException, xunitLogger);
            }
        }
Esempio n. 4
0
        private Collection <PSObject> ExecuteShellTest(
            System.Management.Automation.PowerShell powershell,
            IEnumerable <string> setupScripts,
            IEnumerable <string> scripts)
        {
            SetupPowerShellModules(powershell, setupScripts);

            Collection <PSObject> output = null;

            foreach (var script in scripts)
            {
                if (TracingInterceptor != null)
                {
                    TracingInterceptor.Information(script);
                }
                powershell.AddScript(script);
            }
            try
            {
                powershell.Runspace.Events.Subscribers.Clear();
                powershell.Streams.Error.Clear();
                output = powershell.Invoke();

                if (powershell.Streams.Error.Count > 0)
                {
                    var sb = new StringBuilder();

                    sb.AppendLine("Test failed due to a non-empty error stream, check the error stream in the test log for more details.");
                    sb.AppendLine(string.Format("{0} total Errors", powershell.Streams.Error.Count));
                    foreach (var error in powershell.Streams.Error)
                    {
                        sb.AppendLine(error.Exception.ToString());
                    }

                    throw new RuntimeException(sb.ToString());
                }

                return(output);
            }
            catch (Exception psException)
            {
                powershell.LogPowerShellException(psException, TracingInterceptor);
                throw;
            }
            finally
            {
                powershell.LogPowerShellResults(output, TracingInterceptor);
                powershell.Streams.Error.Clear();
            }
        }
        /// <summary>
        /// Logs a PowerShell exception thrown from PowerShell.Invoke, parsing the inner
        /// PowerShell error record if available
        /// </summary>
        /// <param name="runtimeException">The exception to parse</param>
        public static void LogPowerShellException(this System.Management.Automation.PowerShell powershell, Exception runtimeException, TestContext context)
        {
            context.WriteLine("Caught Exception: {0}\n", runtimeException);
            context.WriteLine("Message: {0}\n", runtimeException.Message);
            IContainsErrorRecord recordContainer = runtimeException as IContainsErrorRecord;

            if (recordContainer != null)
            {
                ErrorRecord record = recordContainer.ErrorRecord;
                context.WriteLine("PowerShell Error Record: {0}\nException:{1}\nDetails:{2}\nScript Stack Trace: {3}\n: Target: {4}\n", record, record.Exception, record.ErrorDetails, record.ScriptStackTrace, record.TargetObject);
            }

            if (runtimeException.InnerException != null)
            {
                powershell.LogPowerShellException(runtimeException.InnerException, context);
            }
        }