简体   繁体   English

Powershell CSV 变量到 Out-GridView

[英]Powershell CSV Variable to Out-GridView

This is driving me nuts.这让我发疯了。 I want to save CSV text to a string variable - NOT a file!我想将 CSV 文本保存到字符串variable - 而不是文件! I want to then output that (CSV) text to the Out-GridView using the commas as column separators.然后我想 output 使用逗号作为列分隔符将 (CSV) 文本发送到Out-GridView

How do I do this?我该怎么做呢? I have looked high and low, and I can find nothing - NOTHING - on how to do this without saving the text to CSV file first then importing it using Import-Csv .我看了高低,我什么也找不到 - 没有 - 关于如何在先将文本保存到CSV文件然后使用Import-Csv导入它的情况下执行此操作。 I don't want to save a file , I just want to display my CSV formatted multiline string object (with NewLine characters) in the Out-GridView .我不想保存文件,我只想在Out-GridView中显示我的 CSV 格式的多行字符串 object (带有新行字符)。

Any clues?有什么线索吗?

Example:例子:

$rows += "COL1,COL2,COL3`n"    # CSV header row AS TEXT
$rows += "val1,val2,val3`n"    # CSV data row AS TEXT

$rows | Out-GridView           # does not work! No columns :( - just a single column per row

Pipe $rows to ConvertFrom-Csv : Pipe $rowsConvertFrom-Csv

$rows += "COL1,COL2,COL3`n"
$rows += "val1,val2,val3`n"
$rows | ConvertFrom-Csv | Out-GridView

What you would normally want to do when creating a CSV like string instead of += to a string variable is to use Here-Strings :在创建 CSV 之类的字符串而不是+=string变量时,您通常想要做的是使用Here-Strings

@'
COL1,COL2,COL3
val1,val2,val3
'@ | ConvertFrom-Csv | Out-GridView

Use this:用这个:

$rows += "COL1,COL2,COL3`n"
$rows += "val1,val2,val3`n"

$csv = $rows | ConvertTo-Csv -Delimiter ","
$csv | Out-GridView

Et viola!等等中提琴!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM