Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET PowerShell
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!
Posted 25-May-13 17:46pm
Edited 27-May-13 20:55pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I was able to figure this out, the test script file contains 2 lines...
$result = Get-Process
$result
 
            Runspace runSpace = RunspaceFactory.CreateRunspace();
            runSpace.Open();
            string filePath = Server.MapPath("Scripts/TestScript.txt");
            StreamReader reader = File.OpenText(filePath);
            string powerShellCode = reader.ReadToEnd();
            reader.Close();
            var shell = PowerShell.Create();
            shell.Commands.AddScript(powerShellCode);
            Collection<psobject> results = shell.Invoke();
            
            DataTable tempTable = new DataTable();
            tempTable.Columns.Add("Id");
            tempTable.Columns.Add("Processname");
 
            foreach (PSObject psObject in results)
            {
                DataRow row = tempTable.NewRow();
                row["Id"] = psObject.Properties["Id"].Value.ToString();
                row["Processname"] = psObject.Properties["Processname"].Value.ToString();
                tempTable.Rows.Add(row);
            }
            
            GridView1.DataSource = tempTable;
            GridView1.DataBind();
            runSpace.Close();</psobject>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 426
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,472
1 Sergey Alexandrovich Kryukov 5,404
2 Maciej Los 3,434
3 Peter Leow 3,259
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 30 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100