1

Hi I am trying to create some sort of hybrid using power shell and c# as managing tool, so simple script in powershell that checks repo status

$git_status = git status
Write-Output $git_status

Now using Pipeline and Run

using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(powershellScript);
        pipeline.Commands.Add("Out-String");
        Collection<PSObject> results = pipeline.Invoke();
        runspace.Close();

        StringBuilder output = new StringBuilder();
        foreach (PSObject obj in results)
        {
            output.AppendLine(obj.ToString());
        }

        return output.ToString();
}

But I am getting empty result, when I run script in IDE power shell result looks like for example:

Write-Host "$git_status" On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: DeviceDatabase/Platforms.xml

What can i do to get that response back to c# procedure?

1
  • As far as I understand it, powershell and git do not play together well. PS operates with records, and git outputs raw byte stream. Since you anyway write code, you'd probably better just read gits output and then handle it with C# Commented Jan 17, 2018 at 16:15

1 Answer 1

1

Write-Host will only print output to terminal, but it will not pass it to stdout. If you want to get the output - use Write-Output.

Write-Host is there to build UX: change output text color, etc. For anything else use Write-Output if you want to have your scripts as tools that can be combined to solve larger problems.

You can find a better explanation here. I'll use example code from the link to expand a little:

function Receive-Output 
{
    process
    {
        # catch input from pipe and print it in green
        Write-Host $_ -ForegroundColor Green
    }
}

# this will print green text, because it was passed to Receive-Output
Write-Output "this is a test" | Receive-Output

# this will print white text, because it was not passed to Receive-Output
# there's a good chance "this is a test" will already be printed by the time Receive-Output gets called
Write-Host "this is a test" | Receive-Output

Edit: As git status prints a colorful text we can safely assume it's using Write-Host in PowerShell. Based on how Posh-Git reads git status into an object I've compiled a little example code that should solve your problem:

> $status = (git -c color.status=false status)
> Write-Output $status

This is the output I get (Windows 10, PowerShell Core 6.0): the output I get

Sign up to request clarification or add additional context in comments.

2 Comments

I've changed that and i think problem lays elsewhere as Git is separate process lunched by power shell script its output is only displayed by not contained by power shell, I think it should be dumped to some sort of var and the outputted , but that's just a theory I need to check.
@WojciechSzabowicz , please see if updated answer helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.