[英]Return Powershell PSObject as DataTable in C#
I've written a Powershell script that reads a CSV file and returns a predetermined collection from the data. 我编写了一个Powershell脚本,它读取CSV文件并从数据中返回一个预定的集合。 Below is a sample output of said script. 以下是所述脚本的示例输出。
Count Name
------ ------
12 Rubies
3 Pearls
20 Emeralds
I am able to obtain the results in C# by storing it in a PSObject like so: 我可以通过将它存储在PSObject中来获得C#中的结果:
var shell = PowerShell.Create();
shell.Commands.AddScript("C:\\Scripts\\Powershell\\Get-MyData.ps1");
Collection<PSObject> results = shell.Invoke();
Now when I'm expecting a single object, I am able to obtain each value and assign them to variables like this: 现在,当我期待单个对象时,我能够获取每个值并将它们分配给这样的变量:
foreach (PSObject psObject in results)
{
localVariable = Convert.ToString(psObject.Properties["Name"].Value);
}
However, I am having trouble converting this solution to a dynamic one. 但是,我无法将此解决方案转换为动态解决方案。 That is to say, the number of rows is expected to vary. 也就是说,预计行数会有所不同。 So I've implemented this before when the source is a SQL database using a solution similar to the one posted here so I've assumed that datatables should be the way to go but I cannot get it to work in this scenario. 所以我之前已经实现了这个,当源是一个SQL数据库时,使用类似于这里发布的解决方案,所以我假设数据表应该是要走的路,但我不能让它在这种情况下工作。 Any ideas or suggestions? 任何想法或建议? Thanks! 谢谢!
Note: The Powershell script component here is compulsory as it includes other mechanisms to formulate the output so while I could just read from the CSV file using C#, this is simply not an option. 注意:这里的Powershell脚本组件是强制性的,因为它包含了其他制定输出的机制,所以当我只能使用C#从CSV文件中读取时,这根本不是一个选项。
What I understood from your question is that you want to have DataTable for further data process and you want to use it in your respective collection. 我从您的问题中理解的是,您希望将DataTable用于进一步的数据处理,并且您希望在各自的集合中使用它。
You can proceed like this: 你可以像这样继续:
DataTable dt=new DataTable();
dt.Colums.Add("Count");
dt.Colums.Add("Name");
foreach (PSObject psObject in results)
{
foreach(PSPropertyInfo prop in psObject.Properties)
{
var count=prop.Name;
var name=prop.Value;
//In other words generate output as you desire.
dt.Rows.Add(count,name);
}
}
Hope it's helpful for you. 希望它对你有所帮助。
since you are already using powershell to read the csv file, why not continue? 既然你已经使用PowerShell来读取csv文件,为什么不继续呢?
You could invoke a powershell command/script to iterate the results of your *.ps1 file before you start to manage it in C#. 在开始使用C#进行管理之前,您可以调用powershell命令/脚本来迭代* .ps1文件的结果。
It's quite easy to invoke a script in c#, and you don't have to create the script as a file. 在c#中调用脚本非常容易,您不必将脚本创建为文件。 You can just send the script text directly and invoke it. 您可以直接发送脚本文本并调用它。
maybe you can even combine the 2 similar to: 也许你甚至可以将2类似于:
var shell = PowerShell.Create();
shell.Commands.AddScript("C:\\Scripts\\Powershell\\Get-MyData.ps1 | foreach {do-something $_}");
Collection<PSObject> results = shell.Invoke();
Notice that i added a pipe after your script results are returned. 请注意,我在返回脚本结果后添加了一个管道。 Remember that the 'AddScript' is very much like sending a bunch of text to powershell.exe. 请记住,'AddScript'非常类似于向powershell.exe发送一堆文本。 I've pushed entire script files from embedded resources to this method before. 我之前已将整个脚本文件从嵌入式资源推送到此方法。
Use Linq: 使用Linq:
IEnumerable<dynamic> dynamicResults = from item in results
select new { Name = item.Name, Count = item.Count };
Then set the myDataGrid.ItemsSource = dynamicResults; 然后设置myDataGrid.ItemsSource = dynamicResults;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.