我对Powershell和脚本非常陌生,如果有人能够提供帮助,我将非常感激。
我正在尝试创建一个PowerShell脚本,该脚本执行以下操作以每天在一定数量的目录中备份所有更改。
仅在过去24小时内从多个预设位置递归查找任何新的/修改过的文件或目录。
压缩zip文件中的所有结果条目
我设法使用以下脚本找到第一位工作:
Get-ChildItem $SRC | Where {$_.PsIsContainer} | foreach-object {
Get-ChildItem $_ -Recurse -ErrorAction SilentlyContinue |
Where {$_.LastWriteTime -gt (get-date).AddDays(-1).date} |
Select FullName, LastWriteTime, Mode | Sort LastWriteTime, Mode
} >> C:\temp\LastModified-$TimeStamp.log
我有另一个脚本可以压缩给定目录,但我一直在努力使用上面脚本的结果将文件复制到我可以编辑的位置,或者是否有办法创建一个zip文件在不丢失每个文件的文件夹结构的情况下,无需先将文件复制到某处?
我有以下脚本可以压缩给定目录(但我需要将多个目录压缩成一个zip文件,除非我首先在某处复制文件并且如何让robocopy只复制最后修改的文件24小时)
$Source = 'C:\Users\Downloads\RobocopyLocation\'
$Destination = 'D:\OneDrive\'
$TimeStamp = Get-Date -f yyyyMMdd_HHmmss
$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory($Source, $ZipName)
当我运行下面的脚本时,我得到并且错误消息抱怨路径参数为null。
$F1 = 'C:\Users\Downloads\Folder A'
$F2 = 'C:\Users\Downloads\Folder B'
$F3 = 'C:\Users\Downloads\Folder C'
$SrcFolders = ($F1, $F2, $F3);
$BackupDrive = "D:\OneDrive"
$DestFolders = ("$BackupDrive\Folder A", "$BackupDrive\Folder B", "$BackupDrive\Folder C");
$Destination = 'C:\Users\SysAdmin\Downloads\PLSWork\'
$TimeStamp = Get-Date -f yyyyMMdd_HHmmss
$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '
$Option = "{$_.PsIsContainer} | foreach-object { Get-ChildItem $_ -Recurse -ErrorAction SilentlyContinue | Where {$_.LastWriteTime -gt (get-date).AddDays(-1).date} | Select FullName, LastWriteTime, Mode | Sort LastWriteTime, Mode}"
Foreach ($SrcFolders in (Get-ChildItem $SrcFolders | where $Option)){
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory($SrcFolders, $ZipName)
}
答案 0 :(得分:1)
修改你的行
$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '
与
$ZipName = "$Destination$TimeStamp" + '_Backup.zip '
答案 1 :(得分:0)
这样的事情怎么样?
$RootPath = "P:\PSZip\"
$Folders = (
"Folder A",
"Folder B",
"Folder C"
)
$BackupFolder = "P:\PSZip\Destination\"
$Files = @()
foreach($Folder in $Folders) {
$Files = Get-ChildItem ("{0}{1}\" -f $RootPath, $Folder) | Where {$_.PSIsContainer} | ForEach-Object { Get-ChildItem $_.FullName -Recurse | Where { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } }
$ZipName = ("{0}{1}\{2}_Backup.zip" -f $BackupFolder, $Folder, (Get-Date -f yyyyMMdd_HHmmss))
Compress-Archive -Path $Files.FullName -DestinationPath $ZipName
}