首页 > 解决方案 > Copy-Item Recursive by *FILE* date modified,但保留文件夹

问题描述

我对此感到有些困惑,因此我正在努力在离线环境中为 WSUS 自动化特定流程。基本上只是在手动批准文件后准备导出文件,并准备好将它们刻录到 CD 以用于隔离环境。

它需要做几件事。首先,它需要清理导出文件夹(检查),在新位置制作现有 WSUS 文件夹的完整副本(检查),确定有多少新文件上传到该副本位置(检查),仅复制那些新文件到维护文件夹结构的导出文件夹,即使文件夹为空(检查...有点)。我的问题是,当文件移动时,它们在文件夹之外......当然,记录......

我正在使用模拟环境在我的个人笔记本电脑上对此进行测试。所以,到目前为止,这是我的代码......

##Set directory locations and variables
$nicedate = Get-Date -UFormat %m-%d-%Y
$source = "C:\temp\WsusContent\"
$bckp = "C:\temp2\WsusContent"
$dest = "C:\temp2\export"
$date = (Get-Date).AddDays(-28)
$log = "C:\temp\wsus_log_$nicedate.txt"
$files = Get-ChildItem -Recurse $source

##Delete all subfolders and Files from destination
Get-ChildItem $dest | Remove-Item -Force -Recurse

#Duplicate new Files (i.e. delta.bat)
try{
    xcopy /E/H/D/C/I/Y $source $bckp
    Write-Output("BACKUP:     WSUS Backup complete")
}
catch{"ERROR:     Backup Failed"}

##Log events
Start-Transcript -Path $log

##Copy source Files and folder structure to destination
foreach($file in $files){
    $fname = $file.fullname
    $fdate = $file.LastWriteTime
    if($fdate -gt $date){
       (Copy-Item $fname -Destination $dest -PassThru).count
    }
}
 
#End logging
Get-Date
Stop-Transcript

这就是他们最终的结果: 导出文件夹

我缺少什么让这些文件在文件夹中移动,但仍然使用 FILE 修改日期(因为我不想获得所有旧的 wsus 更新)?

标签: windowspowershell

解决方案


更新:

我能够使用以下脚本使其工作:

##Set directory locations and variables
$nicedate = Get-Date -UFormat %m-%d-%Y
$source = "C:\temp\WsusContent\"
$bckp = "C:\temp2\WsusContent\"
$dest = "C:\temp2\export\"
$date = (Get-Date).AddDays(-28)
$log = "C:\temp\wsus_log_$nicedate.txt"

##Delete all subfolders and Files from destination
Get-ChildItem $dest | Remove-Item -Force -Recurse

##Duplicate new files (i.e. delta.bat)
try{
    xcopy /E/H/D/C/I/Y $source $bckp > "C:\temp\delta_$nicedate.log"
    Write-Output("BACKUP:     WSUS Backup complete")
}
catch{"ERROR:     Backup Failed"}

##Log events
Start-Transcript -Path $log 

##Copy source files and folder structure to destination
$folders = Get-ChildItem $source -Directory
$count = foreach ($folder in $folders){
    $fname = $folder.FullName
    Copy-Item $fname -Destination $dest
    $files = Get-ChildItem $fname -File
    foreach ($file in $files){
        $fname = $file.FullName
        $sourcefolder = $fname.Replace($source,"")
        $newdest = $dest + $sourcefolder
        if($file.LastWriteTime -gt $date){
            Write-Output $newdest
            Copy-Item $fname -Destination $newdest
        }
    }
}
$total = $count.Count
Write-Output $count 
Write-Output("$total Files moved")
 
##End logging
Get-Date
Stop-Transcript

##Run the WSUS Utility export
Set-Location "C:\Program Files\Update Services\Tools" 
Start-Process wsusutil.exe export "E:\$nicedate-export.xml.gz" "E:\$nicedate-export.log"

推荐阅读