使用powershell和7zip创建存档的脚本

时间:2012-11-01 15:31:56

标签: powershell

我们有几台服务器每天将日志文件写入C:\ Logs。每个月都应运行一个脚本来识别超过30天的文件,将其归档并删除源文件。 C:\ Logs文件夹包含日志文件和包含日志文件的子文件夹(名为1234,4567,7890)。作为Powershell的新手,我将下面的脚本放在一起,希望能够自动完成这项任务。

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

脚本可能不是以最好的方式编写的,所以,如果您对我如何改进它并让它做它需要的任何建议,我很乐意听到任何建议。

$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" 

3 个答案:

答案 0 :(得分:1)

    $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

    $Days = "30" #Number of days past today's date that will be archived
    $CutDay = [DateTime]::Now.AddDays($Days)
    $Files  =  get-childitem $Source  -include $Extension -recurse | Where-Object {$_.LastWriteTime -lt $CutDay}                                                         

    foreach ($File in $Files)
    {
        write-host "File Name : $File " $File.LastWriteTime 
    }

    pushd $Source
    $FileNames = @($Files | %{$_.FullName.Substring($Source.Length+1)} )

    if($FileNames.Count -ne 0)
    {
        [string]$Zip = "C:\apps\7-zip\7z.exe"; #path to 7Zip executable
        [array]$arguments = @("a", "-tzip", "-y", $Archive) + $FileNames
        & $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" 

答案 1 :(得分:1)

你必须使用7zip吗?

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

这是我的代码,你可以使用它并将日期逻辑添加到它:

[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()

答案 2 :(得分:0)

在阅读了更多内容并使用此处发布的脚本和帮助后,我想出了一些能够满足我需要它做的事情。我确信有更好的方法可以解决它(比如添加更多逻辑和if / then语句),但这已经足够了。当我在脚本PowerShell上做得更好时,我可能会重新访问这个脚本。这就是我得到的......

#========================================================
#
#           Log Archive & Move Script
#           Ver. 1.0.0  11/12/2012
#
#========================================================

#Machine hostname - needed for archive creation and identification
$hname = hostname

#Map network drive
$net = $(New-Object -Com WScript.Network)
$net.MapNetworkDrive("X:", "\\your network share\your folder",  
$false, "domain\user", "password") 

#Network folder where archive will be moved and stored
$newdir = "X:\your folder\$hname"

#Archive log entry
$log = "c:\logs\"+ $today + " " + $hname + " " + "Log Archive.txt"

#Local folder where archive file will be created
$archive = "C:\temp\"+ $today + " " + $hname + ".zip" 

#Path to network folder
$archpath = "$newdir" 

#Today's date
$Now = Get-Date 

#Today's date formatted for archive file creation
$today = Get-Date -f MM-yyyy 

#Files older than $Days will be archived and deleted
$Days = "45" 

#Folder where files you want archived are stored
$TargetFolder = "C:\Folder\Logs" 

#Only files with this extension will be archived and deleted
$Extension = "*.txt" 

$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -   le "$LastWrite"}
$allfiles = "C:\temp\@()"

new-item $newdir -itemtype directory -force



#=================================================
#                        
#   -Identify log files older than x days
#   -Create zip archive
#   -List which files were archived
#   -Write log entry
#
#=================================================

foreach ($File in $Files)  
{ 

add-content -path $log -value "Archived: $file  to $archive  on $Now succesfully"

if ($File -ne $NULL)
    {

function create-7zip ([string] $TargetFolder){
#path to 7Zip executable
[string]$Zip = "C:\path to 7zip\7-zip\7z.exe"; 
[array]$args = "a", "-tzip", "-y", "-r", "$archive";

& $Zip $args $file;
}


    write-host "Archiving File $File" -ForegroundColor "Blue" 
    create-7zip | Out-File $allfiles
    }
else
    {
    Write-Host "No files to archive" -foregroundcolor "Red"
    }
}





#=================================================
#                       
#   -Delete files that were already archived
#   -Exclude newly created zip file
#   -List which files were deleted
#   -Write log entry
#
#=================================================     

foreach ($File in $Files)
{
write-host "Deleting file :"$File
remove-item $File -exclude *.zip
add-content -path $log -value "Deleted file:`t$file succesfully"
}




#======================================================
#                       
#   -Create folder in log archive repository
#   -Move zip archive to long term storage
#   -List files that were moved
#   -Write log entry
#
#====================================================== 

new-item $newdir -itemtype directory -force
try
{
Move-Item $archive -destination $archpath -force -ErrorAction:SilentlyContinue
"Moved $archive to $archpath at $now successfully   `r`n=================================================================================================    ========================================`r`n" | add-content $log
}
catch
{
"Error moving $archive:" | add-content $log
}
write-progress -activity "Moving Archive" -status "Progress:"


#=========================================================
#                       
#   -Email log to recepients with a brief explanation
#
#=========================================================

$Mail = @{
SMTPServer = '127.0.0.1'
Body = 'Log archive operations have completed on server:' + "  " + $hname + "  " + '@' + "  " +    $now  
To = 'youremail@mail.com'
From = $hname + '@mail.com'
Subject = $hname + " " + 'Log Archive Created'
 }
"c:\logs\"+ $today + " " + $hname + " " + "Log Archive.txt" | Send-MailMessage @Mail


#=========================================================
#                       
#   -Disconnect mapped network drive used to move archive
#
#=========================================================

$net.RemoveNetworkDrive("x:") 


#=========================================================
#                       
#   -Finish script
#
#=========================================================

write-host -foregroundcolor "Red" "`r`n Archive Completed. Email Sent"