简体   繁体   English

Powershell CSV 可变发行

[英]Powershell CSV Variable Issue

I am having a problem with passing a variable into a CSV. I need to pass an email for a spreadsheet showing all skills.我在将变量传递到 CSV 时遇到问题。我需要将 email 传递给显示所有技能的电子表格。 It's the same email for each skill.每个技能都是一样的email。 I just want the $email to populate my csv. It does not pass and only shows the $email instead of the test@test.com in the column.我只是想让 $email 填充我的 csv。它没有通过,只显示 $email 而不是列中的 test@test.com。

I am new at powershell so any guidance is greatly appreciated.我是 powershell 的新人,非常感谢任何指导。 Thank you.谢谢。

------------here is my script------------------------- ----------这是我的脚本------------------------

Add-Content -Path C:\temp\test.csv -Value '"User Name","Skill Name","Level"' Add-Content -Path C:\temp\test.csv -Value '"User Name","Skill Name","Level"'

$Email = "test@test.com" $Email = "test@test.com"

$agent = @( $代理=@(

} }

'"$Email","T1","4"' '"$Email","T1","4"'

'"$Email","T2","6"' '"$Email","T2","6"'

'"$Email","T3","7"' '"$Email","T3","7"'

'"$Email","Training","1"' '"$Email","培训","1"'

'"$Email","Supervisor","8"' '"$Email","主管","8"'

) )

$agent | $代理| foreach { Add-Content -Path C:\temp\temp.csv -Value $_ } foreach { Add-Content -Path C:\temp\temp.csv -Value $_ }

Because you are new to Powershell, I'm showing you two alternative ways of framing the problem.因为您是 Powershell 的新手,所以我将向您展示两种构建问题的替代方法。 These might help you get used to some of the features of powershell.这些可能会帮助您习惯 powershell 的一些功能。

$Email = 'test@test.com'

$mytext = @"
"User Name","Skill Name","Level"
"$Email","T1","4"
"$Email","T2","6"
"$Email","T3","7"
"$Email","Training","1"
"$Email","Supervisor","8"
"@


$mytext | Out-file Mycsv.csv

Here, I just set up the Email variable, then create one big here string with the header and the five data records in it.在这里,我只是设置了 Email 变量,然后用 header 和其中的五个数据记录创建一个大的 here 字符串。 Because I used double quotes on the here string, the variable $Email will be detected inside of it.因为我在此处的字符串上使用了双引号,所以会在其中检测到变量 $Email。 A here string with single quotes would not have behaved correctly.带有单引号的此处字符串不会正确运行。

Then, I pass $mytext through a pipeline one line at a time, and Out-file collects all this into a file.然后,我一次一行地通过管道传递 $mytext,然后 Out-file 将所有这些收集到一个文件中。

Here's the second approach:这是第二种方法:

$Email = 'test@test.com'

$myarray = @(
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "T1"; "Level" = 4}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "T2"; "Level" = 6}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "T3"; "Level" = 7}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "Training"; "Level" = 1}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "Supervisor"; "Level" = 8}
)

$myarray | Export-Csv myothercsv.csv

Here, I set up the variable Email, then create an array of custom objects, each with the same named properties.在这里,我设置了变量 Email,然后创建了一个自定义对象数组,每个对象都具有相同的命名属性。

Then I pass the array through a pipeline to Export-Csv which converts everything to Csv format.然后我通过管道将数组传递给 Export-Csv,它将所有内容转换为 Csv 格式。 It's worth noting that Export-Csv V5 throws in a line that says #TYPE in it.值得注意的是,Export-Csv V5 抛出了一行,其中写着#TYPE。 This is not hard to eliminate, using the notype parameter if desired.这并不难消除,如果需要,可以使用 notype 参数。 It's also worth noting that the double quotes in the output file were all added in by Export-csv, and weren't copies of the double quotes in the script.还值得注意的是,output 文件中的双引号都是由 Export-csv 添加的,而不是脚本中双引号的副本。

Edit .编辑 Pipelines are a surprisingly easy and flexible way of getting things done in powershell. For this reason, cmdlets like Out-File and Export-Csv are built to work well with pipelines supplying a stream of input.管道是在 powershell 中完成任务的一种非常简单和灵活的方式。因此,像 Out-File 和 Export-Csv 这样的 cmdlet 可以很好地与提供 stream 输入的管道一起工作。 A lot of loop control, initialization, and finalization busy work is being handled behind the scenes by PS.大量的循环控制、初始化、终结等繁琐的工作,都是由PS在幕后处理的。

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

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