Custom PSHostUserInterface ignored by Runspace

Background

I am writing an application that programmatically executes PowerShell scripts. This application has a custom implementation PSHostthat allows scripts to display logging reports. Currently, the behavior that I see is that some requests are properly redirected to my custom PSHost, while others are ignored.

Things get even weirder when I started checking out the variable $Hostin my scripts, which seems to suggest that my custom PSHostis not even used.

Code

I have code that runs PowerShell in a .NET application:

var state = InitialSessionState.CreateDefault();
state.AuthorizationManager = new AuthorizationManager("dummy"); // Disable execution policy

var host = new CustomPsHost(new CustomPsHostUI());

using (var runspace = RunspaceFactory.CreateRunspace(host, state))
{
    runspace.Open();

    using (var powershell = PowerShell.Create())
    {
        powershell.Runspace = runspace;

        var command = new Command(filepath);

        powershell.Invoke(command);
    }
}

CustomPsHost , , PSHostUserInterface:

public class CustomPsHost : PSHost
{
    private readonly PSHostUserInterface _hostUserInterface;

    public CustomPsHost(PSHostUserInterface hostUserInterface)
    {
        _hostUserInterface = hostUserInterface;
    }

    public override PSHostUserInterface UI
    {
        get { return _hostUserInterface; }
    }

    // Methods omitted for brevity
}

CustomPsHostUI :

public class CustomPsHostUI : PSHostUserInterface
{
    public override void Write(string value) { Debug.WriteLine(value); }
    public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value){ Debug.WriteLine(value); }
    public override void WriteLine(string value) { Debug.WriteLine(value); }
    public override void WriteErrorLine(string value) { Debug.WriteLinevalue); }
    public override void WriteDebugLine(string message) { Debug.WriteLine(message); }
    public override void WriteProgress(long sourceId, ProgressRecord record) {}
    public override void WriteVerboseLine(string message) { Debug.WriteLine(message); }

    // Other methods omitted for brevity
}

PowerShell script, :

Write-Warning "This gets outputted to my CustomPSHostUI"
Write-Host "This does not get outputted to the CustomPSHostUI"

Write-Warning $Host.GetType().FullName # Says System.Management.Automation.Internal.Host.InternalHost
Write-Warning $Host.UI.GetType().FullName # Says System.Management.Automation.Internal.Host.InternalHostUserInterface

CustomPSHostUI?

+4
2

PSHostRawUserInterface.

Write-Host Write (ConsoleColor, ConsoleColor, ). PowerShell ui .

. ps1 Write-Host :

powershell.AddCommand("Write-Host").AddParameter("Testing...")

script, PowerShell . . $ , .

, $host . PowerShell , . , .

+6

, PSHostUserInterface PSHostRawUserInterface , WriteErrorLine() Write-Error, , PSHostUserInterface,

https://msdn.microsoft.com/en-us/library/ee706570%28v=vs.85%29.aspx .Invoke(), :

powershell.AddCommand("out-default");
powershell.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output, PipelineResultTypes.Output);

powershell.Invoke() // you had this already

, , -, . , (, , PSHost ), :

http://mshforfun.blogspot.com/2006/07/why-there-is-out-default-cmdlet.html

https://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.command.mergemyresults%28v=vs.85%29.aspx

, , , CMD, , , . ( 0,0, , .) :

class Whatever : PSHostRawUserInterface
{
    public override Size BufferSize
    {
        get { return new Size(300, 5000); }
        set { }
    }

    ...
}

, Console.BufferWidth Console.BufferHeight.

: ErrorRecord , , WriteErrorLine , PowerShell.Streams.Error.DataAdding ItemAdded . - , - , .

+1

All Articles