mehrandvd
mehrandvd

Reputation: 9116

Write-Host -ForegroundColor Green equivalent in PSCmdlet in C#

I'm writing a Cmdlet in C#.

At some points I want to have a colored output like PowerShell command:

Write-Host "Message" -ForegroundColor Green

My C# code is like this:

public class MyCmdlet : PSCmdlet
{
    protected override void ProcessRecord()
    {
        // Something like: WriteHost("Message", Color.Green);
        // ...
    }
}

but the only options are Write-Warning, Write-Error, Write-Object, ...

So my question is:

Question: How can I put some lot on the output with colors?

Upvotes: 3

Views: 2164

Answers (2)

user4003407
user4003407

Reputation: 22132

You can use the Host property of PSCmdlet class to interact with host:

Add-Type -TypeDefinition @‘
    using System;
    using System.Management.Automation;
    [Cmdlet(VerbsDiagnostic.Test, "HostUIWrite")]
    public class TestHostUIWriteCmdlet : PSCmdlet {
        protected override void ProcessRecord() {
            Host.UI.WriteLine(ConsoleColor.Green, Host.UI.RawUI.BackgroundColor, "SomeText");
        }
    }
’@ -PassThru|Select-Object -ExpandProperty Assembly|Import-Module
Test-HostUIWrite

Also you could use Write-Host cmdlet from your code:

Add-Type -TypeDefinition @‘
    using System;
    using System.Management.Automation;
    using Microsoft.PowerShell.Commands;
    [Cmdlet(VerbsDiagnostic.Test, "WriteHost")]
    public class TestWriteHostCmdlet : PSCmdlet {
        protected override void ProcessRecord() {
            using(PowerShell ps = PowerShell.Create(RunspaceMode.CurrentRunspace)) {
                ps.
                AddCommand(new CmdletInfo("Write-Host", typeof(WriteHostCommand))).
                AddParameter("Object", "SomeText").
                AddParameter("ForegroundColor", ConsoleColor.Yellow).
                Invoke();
            }
        }
    }
’@ -ReferencedAssemblies Microsoft.PowerShell.Commands.Utility -PassThru|Select-Object -ExpandProperty Assembly|Import-Module
Test-WriteHost

If you are targeting PowerShell v5, then you can use the WriteInformation method:

Add-Type -TypeDefinition @‘
    using System;
    using System.Management.Automation;
    [Cmdlet(VerbsDiagnostic.Test, "WriteInformation")]
    public class TestWriteInformationCmdlet : PSCmdlet {
        protected override void ProcessRecord() {
            WriteInformation(new HostInformationMessage { Message="SomeText",
                                                          ForegroundColor=ConsoleColor.Red,
                                                          BackgroundColor=Host.UI.RawUI.BackgroundColor,
                                                          NoNewLine=false
                                                        }, new[] { "PSHOST" });
        }
    }
’@ -PassThru|Select-Object -ExpandProperty Assembly|Import-Module
Test-WriteInformation

Upvotes: 8

Richard
Richard

Reputation: 109130

The System.Console class has properties to set the colour:

Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Red;

(Not a great combination of colours :-).)

The existing value is the current colour: save to restore.


Important: Other than Write-Host the other cmdlets do not assume they are writing to something that has colours – they could be redirected to a file. Additionally the possible values for Write-Host's ForegroundColor and BackgroundColor are affected by which PSH host is in use. For example using the console properties you can modify how each colour is shown, but that is not possible in ISE.

Have you considered just invoking Microsoft.PowerShell.Commands.WriteHostCommand?

Upvotes: 1

Related Questions