问题描述
我有一个 PowerShell 脚本,它可以根据我的要求读取 CSV 文件并过滤掉数据,但问题是它只能在我的 Windows 8.1 系统上运行,而不能在其他任何地方运行.-gt
运算符,准确地说,如果我将 -gt
更改为 lt
, le代码>等
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. The -gt
operator to be precise, the script runs on other systems if I change the -gt
to lt
, le
, etc.
下面是脚本.
$date1 = Date ("{0:MM/dd/yyyy}" -f (get-date).AddDays(-1)) -format G
echo $date1
Import-Csv C:\avaya\2014.04.csv | Where-Object { $_.Party1Name -eq "User_Name" -and $_.'Call Start' -gt ( $date1 ) } | Export-Csv -Path "result.csv" -NoTypeInformation
推荐答案
-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
这篇关于运算符“-gt"在此 PowerShell 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!