我正在尝试导入CSV文件,然后按列排序。不能使它工作。
$csvPath = 'path/to/usage_new.csv'
$usage = Get-Content -Path $csvPath | Select-Object -Skip 2 | Sort-Object "Percentage Used" | Out-String | ConvertFrom-Csv
foreach ($usage1 in $usage) {Write-Host Number $usage1.Phone used $usage1."Percentage Used"%}
这只是基本的东西,但是Sort-Object不起作用?
导入并打印到屏幕OK
任何建议都很好!
谢谢
最佳答案
Get-Content将文本行读取为字符串,字符串没有称为“已使用百分比”的属性,它们只有一个长度。
在进行排序之前,您需要从CSV转换,因此传入数据具有从列标题构建的“已使用百分比”属性:
Import-Csv -Path $csvPath |
Select-Object -Skip 2 |
Sort-Object -Property 'Percentage Used' |
Select-Object Phone, @{Name='Used'; Expression={"{0}%" -f $_.'Percentage Used'}}
关于powershell - PowerShell导入和排序CSV文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49333501/