Powershell 脚本 Export-Csv 怪异
我创建了一个小脚本来提取文件夹及其子文件夹的指定字符串的所有行。它的行为符合预期,直到我想将行导出到 Excel (csv)。此时它只导出两行(至少每次都是相同的两行)。实际上它应该有 40 行左右的导出。工作脚本如下所示:
[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
使用导出 cmdlet ( | Export-Csv -path $pathTarget) 添加新管道会以某种方式破坏搜索。我做错了什么?
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). 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. 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. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需测试一下:
$a var 仅包含我导出的行数组。
Just test this :
The $a var just contains the array of lines that I export.
就我个人而言,我发现使用
Export-CSV
存在问题。我发现工作正常的是通过管道传输到ConvrtTo-CSV
,然后传输到Set-Content
或使用重定向输出到文件。Personally I've found problems using
Export-CSV
. What I've found working fine is pipe toConvrtTo-CSV
and then toSet-Content
or output to file using redirection.