我创建了一个小脚本来提取文件夹及其子文件夹的指定字符串的所有行。它的行为与预期一致,直到我想将行导出到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
使用export cmdlet添加新管道(| Export-Csv -path $ pathTarget)会以某种方式破坏搜索。我做错了什么?
答案 0 :(得分:1)
试试这个:
[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
$ a var只包含我导出的行数组。
答案 1 :(得分:0)
我个人发现使用Export-CSV
时出现问题。我发现工作正常的是管道到ConvrtTo-CSV
然后到Set-Content
或使用重定向输出到文件。