簡體   English   中英

Powershell 腳本導出-Csv 怪異

[英]Powershell script Export-Csv weirdness

我創建了一個小腳本來提取文件夾及其子文件夾的指定字符串的所有行。 在我想將這些行導出到 excel (csv) 之前,它的行為與預期一致。 那時它只導出兩行(至少每次都是相同的兩行)。 實際上,它應該有大約 40 行的 exportet。 工作腳本如下所示:

[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) 添加新的 pipe 會以某種方式破壞搜索。 我究竟做錯了什么?

只需測試一下:

[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 只包含我導出的行數組。

就個人而言,我發現使用Export-CSV存在問題。 我發現工作正常的是 pipe 到ConvrtTo-CSV ,然后到Set-Content或 output 到文件使用重定向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM