PowerShell正在将文件夹从具有日期的备份文件夹复制到目标文件夹
我在网络共享上有一个文件夹-称之为\Server\Backup\searl.25.2013.Backup。 此文件夹包含子文件夹\test1、\test2、\test3 例如:PowerShell正在将文件夹从具有日期的备份文件夹复制到目标文件夹,powershell,Powershell,我在网络共享上有一个文件夹-称之为\Server\Backup\searl.25.2013.Backup。 此文件夹包含子文件夹\test1、\test2、\test3 例如: \\Server\Backup\November.25.2013.backup\ .\Test1 .\Test2
\\Server\Backup\November.25.2013.backup\
.\Test1
.\Test2
.\Test3
我需要将2013年11月25日的子文件夹复制到c:\Test。
此功能仅用于复制指定日期的备份文件夹内容(在本例中为昨天的备份)。我正在使用此脚本还原最后一天的备份(2013年11月25日,备份)。以下是我一直试图利用的:
Get-ChildItem-Path\\Server\Backup-r | Where对象{$\.LastWriteTime-gt(Get-Date.Date}
%{复制项-路径$\u.FullName-目标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
Copy Item:无法将参数绑定到参数“Path”,因为它为null。
第3行字符:20
+%{Copy Item-Path(也作为答案添加。)
在粘贴的代码中,Where对象和%之间没有管道
简单解决方案:在第一行末尾添加一个|
:
Get-ChildItem -Path \\Server\Backup -r | ? {$_.lastwritetime -gt (get-date).date} |
% { Copy-Item -Path $_.fullname -destination C:\Test -whatif }
第一行末尾缺少一根管道
此外,如果您试图获取最后一次写入时间为昨天的文件夹,它将小于当前日期-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}
您忘记在两个语句之间添加一个|
(管道)。%
(foreach)没有输入。这可以工作,但现在文件夹中没有文件,我需要添加它吗!($\u0.psiscontainer)还有我的脚本,它会起作用,请务必在最后一个副本中添加Recurse。我可以把它添加到一个单独的例子中。请把这个问题标记为“如果你觉得它是答案的话,并为任何其他问题启动一个新的线程”。回答您的问题,您应该将答案标记为已接受,而不是发表评论感谢回答者。您还可以在获得至少15个代表(包括已接受的答案)后,对任何您认为有用的答案进行投票。请注意,Eris首先指出了缺失的管道,但此答案解决了其他问题。请确定您认为解决问题的最佳答案,并将其标记为已接受。有关更多信息,请参阅此处: