private void ReportException(Exception e)
{
if (e == null)
{
return;
}
var icer = e as IContainsErrorRecord;
object error = icer != null
? icer.ErrorRecord
: new ErrorRecord(e, "Host.ReportException", ErrorCategory.NotSpecified, null);
using (var powerShell = PowerShell.Create())
{
powerShell.Runspace = _runspace;
powerShell.AddScript("$input").AddCommand("out-string");
// Do not merge errors, this function will swallow errors.
var inputCollection = new PSDataCollection<object> {error};
inputCollection.Complete();
var result = powerShell.Invoke(inputCollection);
if (result.Count > 0)
{
var str = result[0].BaseObject as string;
if (!string.IsNullOrEmpty(str))
{
// Remove \r\n, which is added by the Out-String cmdlet.
_psRemoteHost.UI.WriteErrorLine(str.Substring(0, str.Length - 2));
}
}
}
}