我正在将此 CSV 文件导入 powershell,但它没有导入标题,也没有导入所有列。它只得到第一列,但认为没有标题。我意识到第二行是空的,但这就是软件使用我需要处理的数据生成 CSV 文件的方式。
CSV 文件:
#,Labels,Machine Name,System Description,IP,User Logged,Disk Free C
,,,,,,
6,"computers-PRO,Desktop,Office 2010,OS Windows 7,Update Adobe Reader",PRO-MEDASST,91-0-928,172.10.201.111,CHR\\jeniferc,6.3
7,Update Adobe Reader,RED,empty,172.10.201.5,,9.5
8,Update Adobe Reader,SIER,empty,172.10.201.5,,6.8
Powershell 代码:
$computerscsv = import-csv -Path "C:\C Drives Less than 10GB free.csv" #import the csv file
$computerscsv | Format-Table
pause
Powershell 输出:
WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing
headers.
H1
--
6
7
8
如有任何帮助,我们将不胜感激。
最佳答案
编辑:此问题已在 the PowerShell GitHub repository 上报告.
这似乎是命令中的错误。 CSV 文件中的 header 不能以 #
开头。请注意,将第一个 header 转换为 "#"
而不是 #
也不能解决问题。
解决方法:
$data = Get-Content "C:\C Drives Less than 10GB free.csv"
$data[0] = 'Num' + $data[0]
$data | ConvertFrom-Csv
或者,对于更通用的解决方案:
$data = Get-Content "C:\C Drives Less than 10GB free.csv"
if ($data[0][0] -eq '#') {
$data[0] = 'Num' + $data[0]
}
elseif ($data[0].Substring(0,2) -eq '"#') {
$data[0] = '"Num' + $data[0].Substring(1)
}
$data | ConvertFrom-Csv
我的猜测是 Import-Csv
和 ConvertFrom-Csv
将以 #
开头的第一行误认为是 (现在有些弃用)类型信息行:
PS C:\> Get-ChildItem C:\Windows\ | Select-Object Name, LastWriteTime -First 1 | ConvertTo-Csv -NoTypeInformation:$false
#TYPE Selected.System.IO.DirectoryInfo
"Name","LastWriteTime"
"addins","9/15/2018 3:33:54 AM"
关于powershell - 为什么 Powershell Import-Csv 无法在此 CSV 文件上正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56673905/