使用 Test-Path -NewerThan 检查文件日期

Using Test-Path -NewerThan to check date of files

我有以下方法来验证远程文件夹中是否存在文件,我需要检查文件的日期必须是当前日期。

如果我执行下面的语句它似乎工作

$Exs = Test-Path  '\srvcld\Homeware\Applications\Processed\Soc*' -NewerThan (Get-Date -UFormat "%d/%m/%Y")

但是当它 运行 在整个脚本中 returns 出现以下错误:

Test-Path : Cannot bind parameter 'NewerThan'. Cannot convert value "02/23/2018" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

您是否知道另一种可用于验证当前日期的文件是否存在的方法?

这可能对你有帮助

Get-ChildItem -Path "<path>" | Where-Object {$_.LastWriteTime -ge (Get-Date).Date}

不要格式化日期。这会将其转换为 [String],并在将其转换回 [DateTime] 时依赖于您的文化。

相反,保留为 [DateTime] 对象并将其传递给 Test-PathThat's the type it's expecting anyway:

$Exs = Test-Path  '\srvcld\Homeware\Applications\Processed\Soc*' -NewerThan (Get-Date).Date

要添加到 ,这会给出当前日期而不是时间。 运行 如果您想查看类型,请查看以下内容。


$date1 = Get-Date -UFormat "%d/%m/%Y"
$date2 = (Get-Date).Date

$date1.GetType()     # returns String
$date2.GetType()     # returns DateTime