获取“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

查看:28
本文介绍了获取“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 |选择 -ExpandProperty VirtualNetworkAdapters |选择名称,IPV4Addresses |导出-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 } |转换为 CSVcol1",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 } |选择对象 col1,@{ n='col2';e={ $_.col2 -join ' ' } } |转换为 CSVcol1",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天全站免登陆