我有以下CSV文件:
"Count";"Computername";"Files and paths"
"3";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
"1";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
"5";"Computer3";"\\SRV\TOTO$\HELLO.BAT"
"8";"Computer4";"\\192.168.8.18\TOTO\DNS.BAT"
"10";"Computer15";"%OSDRIVE%\Hello.exe"
"12";"Computer6";"\\SRV\SCRIPTS\REBOOT.BAT"
"88";"Computer7";"%OSDRIVE%\Winword.exe"
"154";"Computer2";"%OSDRIVE%\excel.exe"
我要保留“ Count”大于或等于8的所有行。
我尝试了以下命令:
Import-Csv -Path MyFile.csv -Delimiter ";" | Where-Object {$_.Count -ge 8}
但是它只向我返回8或88或18的行...但不是所有其他行都优于8的行(例如10、12、154 ...)。
为什么?
答案 0 :(得分:1)
Count
被读取为字符串,因此您需要将其更改为整数才能进行比较。
一种方法是将您的Where-Object
语句更改为
where {($_.Count -as [int]) -ge 8}
答案 1 :(得分:1)
除非嵌入类型信息(see documentation),否则每个值都将成为字符串。这将导致按字母顺序进行比较,而不是整数1。参见以下内容:
$csv = ConvertFrom-Csv @'
"Count";"Computername";"Files and paths"
"3";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
"1";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
"5";"Computer3";"\\SRV\TOTO$\HELLO.BAT"
"8";"Computer4";"\\192.168.8.18\TOTO\DNS.BAT"
"10";"Computer15";"%OSDRIVE%\Hello.exe"
"12";"Computer6";"\\SRV\SCRIPTS\REBOOT.BAT"
"88";"Computer7";"%OSDRIVE%\Winword.exe"
"154";"Computer2";"%OSDRIVE%\excel.exe"
'@ -Delimiter ';'
$csv | gm
所有属性都是字符串:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Computername NoteProperty string Computername=Computer1
Count NoteProperty string Count=3
Files and paths NoteProperty string Files and paths=%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1
最简单的解决方案是使用投射:
$csv | Where-Object {[int]$_.Count -ge 8}