Operator '-gt' is not working in this PowerShell

前端 未结 2 1033
傲寒
傲寒 2021-01-21 07:55

I have a PowerShell script which reads a CSV file and filters out data as per my requirement, but the catch is that it ONLY works on my Windows 8.1 system and nowhere else.

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-21 08:13

    It's not working the way you expect because what you're doing is a string comparison, not a date comparison.

    What you have in your CSV file is a date string. When using comparison operators, PowerShell will base the type of comparison on the object type on the left hand side, and if necessary coerce the right-hand side to the same type in order to perform the operation. Start with a datetime object, and put it on the left hand side, and PowerShell will coerce the string on the right-hand sided to a datetime for you:

    $date1 = (get-date).AddDays(-1)
    
    echo $date1
    
    Import-Csv C:\avaya\2014.04.csv |
     Where-Object { $_.Party1Name -eq "User_Name" -and $date1 -lt $_.'Call Start' } |
     Export-Csv -Path "result.csv" -NoTypeInformation
    

提交回复
热议问题