【问题标题】:PowerShell copying folders from a backup Folder with date to destination folderPowerShell将带有日期的备份文件夹中的文件夹复制到目标文件夹
【发布时间】:2014-01-03 04:15:04
【问题描述】:

我在网络共享上有一个文件夹 - 将其命名为 \Server\Backup\November.25.2013.backup。 此文件夹包含子文件夹 \test1、\test2、\test3。

例子:

\\Server\Backup\November.25.2013.backup\
                                       .\Test1
                                       .\Test2
                                       .\Test3

我需要将 November.25.2013.backup 的子文件夹复制到 c:\Test。 此功能仅用于复制指定日期的备份文件夹内容(此处为昨天的备份)。我正在使用这个脚本来恢复最后一天的备份减去名称(November.25.2013.backup)。这是我一直在尝试利用的:

Get-ChildItem -Path \\Server\Backup -r | Where-Object {$_.LastWriteTime -gt (Get-Date).Date}
% { Copy-Item -Path $_.FullName -Destination C:\Test -WhatIf }

但是我得到了错误

    Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:3 char:20
+ % { Copy-Item -Path <<<<  $_.fullname -destination C:\Test -whatif }
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

请理解,我仍然是 Powershell 脚本的新手,我不知道如何解决这个问题。我很感激任何建议。

我的目标是从备份文件夹中恢复文件夹。谢谢。

【问题讨论】:

  • 您忘记在两个语句之间添加|(管道)。 %(foreach) 没有输入。

标签: powershell


【解决方案1】:

您在第一行末尾缺少一个管道。

另外,如果您尝试获取最后写入时间为昨天的文件夹,则它会小于-lt 当前日期

Get-ChildItem -Path \\Server\Backup -r | Where-object {$_.lastwritetime -lt (get-date).date} |
% { Copy-Item -Path $_.fullname -destination C:\Test -whatif }

但是,如果每天都有一个文件夹,那么这可能会获取比您想要的更多的历史记录。如果您只想要昨天写的内容,请使用:

Get-ChildItem -Path \\Server\Backup -r | Where-object {($_.lastwritetime.date -eq ((get-date).adddays(-1)).date)} |
% { Copy-Item -Path $_.fullname -destination C:\Test -whatif }

评论示例:

Get-ChildItem -Path c:\test -r | Where-object {$_.PSIscontainer -and (($_.lastwritetime.date -eq ((get-date).adddays(-1)).date))}  |
% { Copy-Item $_.fullName -destination C:\Testoutput\ -recurse}

【讨论】:

  • 这个工作除了现在文件夹遇到但其中没有文件我需要添加这个!($_.psiscontainer) -AND到我的脚本
  • 是的,这会起作用,只要确保在 -whatif 当前所在的复制项的末尾添加 -Recurse 即可。我可以将其添加为单独的示例。如果您认为已回答,请将此问题标记为已回答,并针对任何其他问题创建一个新主题。
  • 像魅力一样工作 - 非常感谢请考虑这个问题已回答
  • 当有人回答您的问题时,您应该将答案标记为已接受,而不是发表评论感谢回答者。一旦您有至少 15 个代表(包括接受的答案),您还可以对任何您认为有用的答案进行投票。请注意,厄里斯首先指出了丢失的管道,但这个答案解决了其他问题。请决定您认为哪个答案最能解决您的问题并将其标记为已接受。请参阅此处了解更多信息:stackoverflow.com/help/someone-answers
【解决方案2】:

(也作为答案添加。)

在您粘贴的代码中,Where-Object 和 % 之间没有管道。

简单的解决方案:在第一行末尾添加一个|

Get-ChildItem -Path \\Server\Backup -r | ? {$_.lastwritetime -gt (get-date).date} |
% { Copy-Item -Path $_.fullname -destination C:\Test -whatif }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2017-03-31
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    相关资源
    最近更新 更多