【问题标题】:powershell : get-date.adddays incorrect?powershell:get-date.adddays 不正确?
【发布时间】:2010-10-04 14:01:48
【问题描述】:

我要做的是创建一个 PowerShell 脚本,它获取目录中的文件夹列表,该列表由正则表达式过滤,筛选出具有 nnnnnnx31 或 nnnnnnddd 的文件夹名称,其中 n = 前 6 个字符的 alpha 字符和最后 3 个是静态字符串 x31 的任一数字。接下来,它会筛选文件是否存在 90 天,这些文件将被复制到不同的目录。

当我尝试运行时:

get-childitem | where {$_.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$_.lastwritetime -lt (get-date.adddays(-90)}

我得到错误:

You must provide a value expression on the right-hand side of the -lt operator
At line: 1 char: 96
+ get-childitem | where {$_.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$_.lastwritetime -lt  <<<< get-date.adddays(-90)}

我也尝试了以下方法,但没有成功:

get-childitem | where {$_.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$_.lastwritetime -lt (get-date | foreach-object {$_.adddays(-90)}) }

有什么想法吗?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    你需要做(get-date).AddDays(-90)

    【讨论】:

      【解决方案2】:

      您可以使用一个 where-object 而不是两个:

      get-childitem | where {$_.name -match "^\d{6}([a-zA-Z]{3}|x31)$" -AND $_.lastwritetime -lt (get-date).adddays(-90)}
      

      【讨论】:

      • 赞成合并 where 子句。简洁和性能改进。
      【解决方案3】:

      试试这个 - get-childitem | where {$_.name -match "^\d{6}([a-zA-Z]{3}|x31)$"} | where {$_.lastwritetime -lt (get-date).adddays(-90)}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 2012-05-19
        • 1970-01-01
        • 2010-09-24
        • 1970-01-01
        • 2013-05-21
        • 2022-08-05
        相关资源
        最近更新 更多