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 );
}
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54

1 Answers1

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

boxdog
  • 7,894
  • 2
  • 18
  • 27
Malte
  • 111
  • 10