0

I have below PowerShell cmnd which runs fine in PowerShell and I am also able to call and execute it from a unit test in C# as described below. But my problem is in PowerShell window I write $rec.Categories.Count and then I get result. How can I write this in PowerShell script which I call in C# and get the value in there to test?

In PowerShell window:

$inv = Get-Inventory -Project 'TestDemo'
$inv.Categories.Count
4

Same script I have in C# and I am calling it by below method and it runs successfully but I don't know the way to get "4" in my result variable.

[Fact]
        public void VerifyGetInventoriesOfaProject()
        {
            string path = Path.Combine( Root, @"TestData\GetInventory.ps1" );
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript( path );
            var results = pipeline.Invoke();
            runspace.Close();
           // Assert.Equal( 5, results );
}

1 Answer 1

1

If you just search c# get output from powershell the first result is a stackoverflow post about this with a pretty clear and easy answer ...

Get Powershell command's output when invoked through code

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

Comments

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.