【问题标题】:Powershell -recurese script failing to search all files in sub-directoryPowershell -recurese 脚本无法搜索子目录中的所有文件
【发布时间】:2015-01-29 22:36:48
【问题描述】:

我是使用 powershell 的新手,但遇到了障碍。我试图让我的脚本搜索所有新文件(前一天晚上)并将它们复制到测试文件夹。此脚本首先映射到所有 SQL BAK 文件所在的位置。接下来应该发生的事情是搜索前一天晚上的所有新 BAK 文件并将它们复制到测试文件夹中。但是,它只复制一个日期为几天前的文件。

 $bak_path = "\\nas2\sqlbackups"

 get-childitem -path $bak_path -Filter "*.bak" -recurse | 
     where-object { -not $_.PSIsContainer } | 
     sort-object -Property $_.CreationTime | 
     select-object -last 1 | copy-item -Destination I:\test

当我按如下方式运行此脚本时,它会返回所有 BAK 文件。这告诉我过滤器工作正常。我在同一位置有其他文件(*.log、*.rpt)我不想检索。

 $bak_path = "\\nas2\sqlbackups"
 get-childitem -path $bak_path -Filter "*.bak" -recurse

当我将此脚本更改为直接查看包含许多 SQL BAK 文件的文件夹时,它可以工作!

 $bak_path = "\\nas2\sqlbackups\SERVER123\Database\FULL"

get-childitem -path $bak_path -Filter "*.bak" | 
    where-object { -not $_.PSIsContainer } | 
    sort-object -Property $_.CreationTime | 
    select-object -last 1 | copy-item -Destination I:\test

但是,当我尝试使用 -recurse 选项时,我的脚本失败了。

我做错了什么?

【问题讨论】:

    标签: powershell-3.0


    【解决方案1】:

    你应该使用这个:

    sort-object -Property CreationTime
    

    代替:

    sort-object -Property $_.CreationTime
    

    比较:

    get-childitem -path $bak_path -Filter "*.bak" -File | 
    sort-object -Property $_.CreationTime |
    select-object Name, CreationTime
    

    收件人:

    get-childitem -path $bak_path -Filter "*.bak" -File | 
    sort-object -Property CreationTime |
    select-object Name, CreationTime
    

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多