windows - 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 更新)?
解决方案
更新:
我能够使用以下脚本使其工作:
##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"
推荐阅读
- css - 如何在引导弹性盒中使用 css 过渡?
- angular - Angular 单元测试:'mat-nav-list' 不是已知元素
- javascript - 如何获取和显示图像
- c# - 在 SelectPDF 生成的 PDF 中,有没有办法仅删除第一页的上边距?
- java - ArrayList 是什么
我怀疑这意味着 sortedSourceClasses arrayList 可以包含任何扩展 ReferenceSource 类的类。那是对的吗?
ArrayList<Class<? extends ReferenceSource>> sortedSourceClasses = new
- excel - 如何在vba上输入用户决定的变量值?
- google-apps-script - 在 g Suite Market Place sdk 配置屏幕中禁用了 gmail 附加扩展
- linechart - 如何在给定表格的 Apache Superset 中显示折线图?
- c# - 如何从控制台中的字符串中删除一个字符
- java - JavaFx:TreeTableCell 边框 css