Hey guys, I am able to run PowerShell scripts via an ASP.NET C# page and write single valued PS variables to a label. However, I can't figure out how to get or convert a PS array to a multidimensional array in ASP.NET C#.
Here is what I have so far. If "MyVar1" is an array in PS, how do I properly retrieve the data, convert it to a multidimensional array so I can bind into gridview?
I get "Cannot implicitly convert type 'object' to 'string[*,*]'. An explicit conversion exists (are you missing a cast?)" error when trying
string[,] arrMultiD = Results;
Here is the code to run and get the PS results.
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
var shell = PowerShell.Create();
string filePath = Server.MapPath("Scripts/MyScript.txt");
StreamReader reader = File.OpenText(filePath);
string powerShellCode = reader.ReadToEnd();
reader.Close();
shell.Commands.AddScript(powerShellCode);
var results = shell.Invoke();
var MyVar1 = shell.Runspace.SessionStateProxy.GetVariable("MyVar1");
runspace.Close();
Thanks!