使用 powershell 和 7zip 创建存档的脚本 [英] Script to create archive using powershell and 7zip

查看:42
本文介绍了使用 powershell 和 7zip 创建存档的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有几台服务器每天将日志文件写入 C:\Logs.每个月,应该运行一个脚本来识别超过 30 天的文件,将它们存档,并删除源文件.C:\Logs 文件夹包含日志文件和也包含日志文件的子文件夹(名为 1234、4567、7890).作为 Powershell 的新手,我把下面的脚本放在一起,希望我可以自动化这个任务.

We have several servers that write log files to C:\Logs on a daily basis. Every month, a script is supposed to run to identify files older than 30 days, archive them, and delete the source file. The C:\Logs folder contains log files, and sub folders (named 1234, 4567, 7890) that also contain log files. Being new to Powershell, I put together the script below in the hopes that I could automate this task.

该脚本能够识别超过 30 天的文件(我在测试它时输出了文件名)但是当存档例程开始时,它会将所有内容压缩到 C:\Logs 文件夹中并执行不保留子文件夹结构.

The script is able to identify the files older than 30 days (I had it output the file names when I was testing it) but when the archive routine kicks off, it zips everything up in the C:\Logs folder and does not keep the sub folder structure.

脚本可能不是以最好的方式编写的,所以如果您对我如何改进它并让它做它需要做的事情有任何建议,我很想听听任何建议.

The script is probably not written in the best way possible, so please, if you have any suggestions on how I can improve it and get it to do what it needs to, I would love to hear any suggestions.

$Hname = hostname #Name of server
$Source = "C:\logs" #Folder where log files reside
$Archive = "C:\logs\$hname\Archive.zip" #Folder where archive file will be created
$Extension = "*.txt" #Only files with this extension will be identified and archived
$Date = Get-Date #Today's date
$Days = "30" #Number of days past today's date that will be archived
$Files  =  get-childitem $Source  -include $Extension -recurse | Where-Object {$_.LastWriteTime -lt $Date.AddDays(-$Days)}                                                         
$FileNames = ""

foreach ($File in $Files)
{
    write-host "File Name : $File " $File.LastWriteTime 
    $FileNames = $FileNames + " " + $File  
}
write-host "Files to archive : " $FileNames

if($FileNames.Trim() -ne "")
{
    [string]$Zip = "C:\apps\7-zip\7z.exe"; #path to 7Zip executable
    [array]$arguments = "a", "-tzip", "-y", $Archive, $Source+$Extension;
    & $Zip $arguments ;
}

foreach ($File in $Files)
{
    write-host "Deleting file :"$File
    #remove-item $File -exclude *.zip
}
else
{
    write-host "No files to archive"
}

write-host "Archive Completed" 

推荐答案

你必须使用 7zip 吗?

Do you have to use 7zip?

我使用 dotnetzip 完成了同样的任务,并且能够保存文件夹结构.

I have done the same task using dotnetzip and was able to save the folder structure.

这是我的代码,您可以使用它并向其添加日期逻辑:

Here is my code, you can use this and add your date logic to it:

[System.Reflection.Assembly]::LoadFrom("c:\\\User\\bin\\Ionic.Zip.dll");
$zipfile = new-object Ionic.Zip.ZipFile("C:\user\temp\logs\TestZIP.zip");

$directory = "C:\user\temp\logs\"
$children = get-childitem -path $directory
foreach ($o in $children)
{
   if($o.Name.EndsWith(".log")){
      $e = $zipfile.AddFile($o.FullName)
   }
}
$zipfile.Save()
$zipfile.Dispose()

这篇关于使用 powershell 和 7zip 创建存档的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆