Script to create archive using powershell and 7zip

前端 未结 3 2003
天命终不由人
天命终不由人 2021-01-21 09:15

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 delet

3条回答
  •  萌比男神i
    2021-01-21 09:50

    After reading some more and using the scripts and assistance posted here, I came up with something that does what I need it to do. I'm sure there are better ways to go about it (like adding more logic and if/then statements) but it's good enough. I might revisit this script when I get better at scripting powershell. Here's what I got...

    #========================================================
    #
    #           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" 
    

提交回复
热议问题