0

I am trying to access a dataset inside a C# code. The dataset is output from PowerShell code which I am executing using PowerShell.Invoke() method. I have assigned the output from that call to a Collection<PSObject> as it was not allowing to me to assign the output to a DataSet object. Now I want to access the tables inside the dataset that was being returned by the PowerShell code. How do I extract the tables from this collection?

Sample Code

private DataSet ExecPowershell()
{
    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    using (RunspaceInvoke runspaceInvoker = new RunspaceInvoke(runspace))
    {
        runspaceInvoker.Invoke("Set-ExecutionPolicy RemoteSigned");
        using (PowerShell powershell = PowerShell.Create())
        {
            powershell.Runspace = runspace;
            string PSFilePath = "D:\\PS\\WebSitePS.ps1";
            powershell.Commands.AddScript(PSFilePath);
            Collection<PSObject> siteDetails = powershell.Invoke();
            foreach (PSObject siteDetail in siteDetails)
            {
                //What to do here? 
            }
        }
    }
    return null;
}
briantist
  • 45,546
  • 6
  • 82
  • 127
karun_r
  • 183
  • 1
  • 2
  • 14
  • Specifically, see [Keith Hill's answer](http://stackoverflow.com/a/2330855/3905079) in the linked duplicate. – briantist May 13 '16 at 18:35
  • From Keith's answer, I can get the object type. But how do I map the PSObject's values to the dummy object I create in C# ? – karun_r May 14 '16 at 00:52
  • `.BaseObject` is the actual object, not just its type. I understood your post to mean that the PowerShell code was returning a `[DataSet]` object, which was being wrapped in a `PSObject` (as always when you invoke PowerShell code). Is the PowerShell code actually just returning a `[PSObject]` with note properties? – briantist May 14 '16 at 20:35
  • No. The Powershell code is returning a DataSet with four tables in it when I run the PS code in the ISE. I am getting a PSObject from that code only when I invoke it from C# – karun_r May 17 '16 at 18:38

0 Answers0