且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

运算符“-gt"在此 PowerShell 中不起作用

更新时间:2023-11-13 15:47:40

-gt 正常工作.问题是您试图将字符串值与 -gt 进行比较.要比较日期,它们需要是 DateTime-objects.导入 CSV 文件时,所有值都将作为字符串导入.您需要将日期转换回 DateTime 对象.例如:

-gt is working as it should. The problem is that you're trying to compare string-values with -gt. To compare dates, they need to be DateTime-objects. When you import a CSV file, all values are imported as strings. You need to convert the date back to a DateTime-object. Ex:

$date1=(get-date).AddDays(-1)

echo $date1

Import-Csv C:\avaya\2014.04.csv | Where-Object { $_.Party1Name -eq "User_Name" -and ($_.'Call Start' -as [datetime]) -gt $date1 } | Export-Csv -Path "result.csv" -NoTypeInformation