本文介绍了检索PowerShell数组并放入ASP.NET C#数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿伙计们,我可以通过ASP.NET C#页面运行PowerShell脚本,并将单值PS变量写入标签。但是,我无法弄清楚如何在ASP.NET C#中将PS阵列转换或转换为多维数组。 这是我到目前为止所拥有的。如果MyVar1是PS中的数组,我如何正确检索数据,将其转换为多维数组,以便我可以绑定到gridview? 我得到无法隐式地将类型''object''转换为''string [*,*]''。存在显式转换(你是否错过了演员?)尝试 string [,] arrMultiD = Results; 这是运行和获取PS的代码结果。 Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); var shell = PowerShell.Create(); string filePath = Server.MapPath( 脚本/ 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(); 谢谢!解决方案 我能算出来,测试脚本文件包含2行... result = Get-Process 结果 Runspace runSpace = RunspaceFactory.CreateRunspace(); runSpace.Open(); string filePath = Server.MapPath( 脚本/ 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 > 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! 解决方案 I was able to figure this out, the test script file contains 2 lines...result = Get-ProcessresultRunspace 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> 这篇关于检索PowerShell数组并放入ASP.NET C#数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 12:02