我正在尝试创建一个强大的脚本来抓取某些文件和/或文件夹并自动将它们拉上来以使我的技术更快地重新映像
在重新映像系统之前,我们必须从某些位置抓取拉链和/或文件夹进行备份。任何人都可以帮忙吗?
我对Powershell很新。
Param(
[string]$FilesLocation,
[string]$BackupPath,
$Days
)
$CurrentTime = "\" + (get-date -f MM_dd_yyyy) + "_" + (get-date -f HH_mm) + "_Backup_"
#$BackupPath = $BackupPath+$CurrentTime
$BackupPath
$Name = $CurrentTime + ".zip"
$ArchiveFile = Join-Path -Path $BackupPath -ChildPath $Name
If (Test-path $ArchiveFile) {Remove-item $ArchiveFile}
Get-ChildItem -Path $FilesLocation - |
Where-Object {$_.CreationTime -gt (Get-date).AddDays(-1)} |
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($FilesLocation, $ArchiveFile)
答案 0 :(得分:0)
我尝试了这个,它对我来说很好。
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
$logFolder=Read-Host "enter foldername (eg=c:/temp)"
$zipFileName=Read-Host "enter a name for zip file (eg=filename.zip)"
$finalPath=$logFolder+"/"+$zipFileName
Write-Host $finalPath
$startDate=Read-Host "enter date in the format mm/dd/yyyy"
$endDate=Read-Host "enter date in the format mm/dd/yyyy"
# creates empty zip file:
[System.IO.Compression.ZipArchive] $archive = [System.IO.Compression.ZipFile]::Open($finalPath,[System.IO.Compression.ZipArchiveMode]::Update)
# add your files to archive
Get-ChildItem -path $logFolder | foreach {[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($archive,$_.FullName,$_.Name)}
# archive will be updated with files after you close it. normally, in C#, you would use "using ZipArchvie arch = new ZipFile" and object would be disposed upon exiting "using" block. here you have to dispose manually:
$archive.Dispose()
它将为$logFolder
上的所有文件创建zip。