获得"System.Collections.Generic.List`1 [System.String]".屏幕上的数据正常时,在CSV文件导出中 [英] Getting "System.Collections.Generic.List`1[System.String]" in CSV File export when data is okay on screen

查看:649
本文介绍了获得"System.Collections.Generic.List`1 [System.String]".屏幕上的数据正常时,在CSV文件导出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PowerShell的新手,正在尝试从Hyper-V中获取VM名称及其相关IP地址的列表.

我在屏幕上可以很好地获取信息,但是当我尝试导出到csv时,得到的IP地址都是每行"System.Collections.Generic.List`1 [System.String]".

对于"joins"或"ConvertTo-CSV"有一些建议,但我不理解这些语法.

任何人都可以帮忙吗?

这是我使用的语法...

Get-VM | Select -ExpandProperty VirtualNetworkAdapters | select name, IPV4Addresses | Export-Csv -Path "c:\Temp\VMIPs.csv"

解决方案

如果使用Export-CsvConvertTo-Csv格式导出为CSV的对象的属性值包含集合(数组的值,这些值通过它们的.ToString()方法进行了字符串化,这会导致无用的表示,就像数组值的.IPV4Addresses属性一样.

通过ConvertTo-Csv cmdlet(与Export-Csv类似,但返回 CSV数据而不是将其保存到文件中)进行演示:

PS> [pscustomobject] @{ col1 = 1; col2 = 2, 3 } | ConvertTo-Csv
"col1","col2"
"1","System.Object[]"

也就是说,存储在.col2属性中的数组2, 3被无意地字符串化为System.Object[],这是在常规PowerShell数组上调用.ToString()时得到的结果.其他.NET集合类型(例如您的情况下的[System.Collections.Generic.List[string]])类似地进行字符串化;即按其类型名称.


假设您要在单个 CSV列中表示数组值属性的所有值,要解决此问题,您必须确定有意义的字符串表示形式整个集合,并使用带有计算出的属性的 Select-Object实施它 :

例如,您可以使用-join运算符创建以空格分隔的元素列表:

PS> [pscustomobject] @{ col1 = 1; col2 = 2, 3 } | 
      Select-Object col1, @{ n='col2'; e={ $_.col2 -join ' ' } } |
        ConvertTo-Csv
"col1","col2"
"1","2 3"

请注意如何将数组2, 3转换为字符串'2 3'.

I am new to PowerShell and trying to get a list of VM names and their associated IP Addresses from within Hyper-V.

I am getting the information fine on the screen but when I try to export to csv all I get for the IP Addresses is "System.Collections.Generic.List`1[System.String]" on each line.

There are suggestions about "joins" or "ConvertTo-CSV" but I don't understand the syntax for these.

Can anyone help?

This is the syntax I am using...

Get-VM | Select -ExpandProperty VirtualNetworkAdapters | select name, IPV4Addresses | Export-Csv -Path "c:\Temp\VMIPs.csv"

解决方案

If an object you export as CSV with Export-Csv or ConvertTo-Csv has property values that contain a collection (array) of values, these values are stringified via their .ToString() method, which results in an unhelpful representation, as in the case of your array-valued .IPV4Addresses property.

To demonstrate this with the ConvertTo-Csv cmdlet (which works analogously to Export-Csv, but returns the CSV data instead of saving it to a file):

PS> [pscustomobject] @{ col1 = 1; col2 = 2, 3 } | ConvertTo-Csv
"col1","col2"
"1","System.Object[]"

That is, the array 2, 3 stored in the .col2 property was unhelpfully stringified as System.Object[], which is what you get when you call .ToString() on a regular PowerShell array; other .NET collection types - such as [System.Collections.Generic.List[string]] in your case - stringify analogously; that is, by their type name.


Assuming you want to represent all values of an array-valued property in a single CSV column, to fix this problem you must decide on a meaningful string representation for the collection as a whole and implement it using Select-Object with a calculated property:

E.g., you can use the -join operator to create a space-separated list of the elements:

PS> [pscustomobject] @{ col1 = 1; col2 = 2, 3 } | 
      Select-Object col1, @{ n='col2'; e={ $_.col2 -join ' ' } } |
        ConvertTo-Csv
"col1","col2"
"1","2 3"

Note how array 2, 3 was turned into string '2 3'.

这篇关于获得"System.Collections.Generic.List`1 [System.String]".屏幕上的数据正常时,在CSV文件导出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆