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;
}