我知道如何通过PSObjects将散列表,数组等从Powershell返回到c#。为了使用它,我需要从Powershell脚本返回一个对象 - 而不仅仅是列出输出。
但是如何从Powershell中的列表中获取可以从Powershell脚本返回的结构?
想想这个简单的场景(例如目的):
Get-ChildItem C:\Test
我得到类似这样的输出:
PS C:\test> Get-ChildItem
Directory: C:\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 03.06.2013 14:59 6 test1.txt
-a--- 03.06.2013 14:59 5 test2.txt
现在我想获取每个文件的Name和Length属性,并将它作为某种对象从powershell脚本返回,我可以使用C#进行处理。
我可以从C#处理的一个例子是:
$a = New-Object psobject -Property @{
Name = "test1.txt"
Age = 6
}
$b = New-Object psobject -Property @{
Name = "test2.txt"
Age = 5
}
$myarray = @()
$myarray += $a
$myarray += $b
Return $myarray
如何从Get-ChildItem(或提供列表的类似内容)获取对象数组或类似内容?
请注意,这与Get-ChildItem无关,它仅用作输出列表的示例。
答案 0 :(得分:5)
这样的事情应该这样做:
$arr = Get-ChildItem | Select-Object Name,Length
return $arr
如果由于某种原因它不起作用,请尝试将数组嵌套在一个数组元素中(使用逗号运算符)
return ,$arr