简体   繁体   English

Powershell 脚本导出-Csv 怪异

[英]Powershell script Export-Csv weirdness

I have created a small script to extract all lines of a specified string for a folder and its subfolder.我创建了一个小脚本来提取文件夹及其子文件夹的指定字符串的所有行。 It behaves as expected untill I want to export the lines to excel (csv).在我想将这些行导出到 excel (csv) 之前,它的行为与预期一致。 At that point it only exports two lines (at least it is the same two lines every single time).那时它只导出两行(至少每次都是相同的两行)。 In reallity it should have exportet around 40 lines.实际上,它应该有大约 40 行的 exportet。 The working script looks like this:工作脚本如下所示:

[string]$pathSource = "D:\Projects\test"
[string]$pathTarget = "D:\Projects\test\searchpattern-sql.csv"

[string[]]$excludeFileTypes = "*.dll","*.pdb","*.gif","*.dat","*.bin","*.msi","*.epi4","*.png","*.tlog","*.gif","*.cache","*.csproj","*.config","*.user","*.txt"

[string]$searchPattern = "sqlCommand"

Get-ChildItem $pathSource -Exclude $excludeFileTypes  -Recurse | Select-String $searchPattern | Sort-Object Path | Select-Object Path, LineNumber, Line

Adding a new pipe with the export cmdlet ( | Export-Csv -path $pathTarget) somehow ruins the search.使用导出 cmdlet (| Export-Csv -path $pathTarget) 添加新的 pipe 会以某种方式破坏搜索。 What am I doing wrong?我究竟做错了什么?

Just test this:只需测试一下:

[string]$pathSource = "D:\Projects\test"
[string]$pathTarget = "D:\Projects\test\searchpattern-sql.csv"

[string[]]$excludeFileTypes = "*.dll","*.pdb","*.gif","*.dat","*.bin","*.msi","*.epi4","*.png","*.tlog","*.gif","*.cache","*.csproj","*.config","*.user","*.txt"

[string]$searchPattern = "sqlCommand"

$a = Get-ChildItem $pathSource -Exclude $excludeFileTypes  -Recurse | Select-String $searchPattern | Sort-Object Path | Select-Object Path, LineNumber, Line
$a | Export-Csv yourfile.csv -NoTypeInformation

The $a var just contains the array of lines that I export. $a var 只包含我导出的行数组。

Personally I've found problems using Export-CSV .就个人而言,我发现使用Export-CSV存在问题。 What I've found working fine is pipe to ConvrtTo-CSV and then to Set-Content or output to file using redirection.我发现工作正常的是 pipe 到ConvrtTo-CSV ,然后到Set-Content或 output 到文件使用重定向。

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

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