PowerShell Script to manage FTP/SFTP/OpenSSH log files

The script below began based on a script found in the OpenSSH GitHub forum. We had an OpenSSH log file that was about 550 MB in size. This made it hard to open in NotePad++ and difficult to find anything on a given date.

Then we realized we have a similar issue (but not nearly as bad) with BizTalk FTP/SFTP log files. In a SendPort you can turn on logging. Fortunately all the files were written to a “logs” folder. The second part of the script does the same logic for all *.log or *.txt files in the “logs” folder.

#--------------------------------------------------------------
#
# Neal Walters - 04/02/2020 - modified from suggested script from 
# https://github.com/PowerShell/Win32-OpenSSH/issues/935
# to keep the size of the log file down - 
#
# Schedule this in task scheduler.
# Problem is that the log file get's huge after a few weeks, 
# and it can be hard to find and isolate issues. 
#
# Script basically dates each log file, and a new one will 
# be created for each day (the new/current one doesn't have a
# date) 
#
#--------------------------------------------------------------

cls
$ErrorActionPreference = "Stop"   #keep script from continuing if any error 

#--------------------------------------------------------------

$OpenSSHLogFile = "c:\ProgramData\ssh\logs\sshd.log"

#Original sample checked for size of file, but we want a new file every day. 
#if ((Get-ChildItem $OpenSSHLogFile).Length -gt 100000)

$fileDateFormat = get-date -f _yyyy_MM_dd
$newFileName = $OpenSSHLogFile.Replace("sshd","sshd_$fileDateFormat"); 
Write-Host "NewFile: $newFileName" 
if (-not (Test-Path $newFileName))
   {
      copy-item $OpenSSHLogFile $newFileName 
      $null | Set-Content $OpenSSHLogFile 
   }

#--------------------------------------------------------------
# Part 2 - do similar for all BizTalk FTP/SFTP Log Files 
# BizTalk Send/Receive for FTP/SFTP have the option to log, 
# and we place them in the disk structure below. 
#--------------------------------------------------------------

$logFolder = "e:\Integration\Logs"

$logFiles = Get-ChildItem $logFolder -Recurse -File 

foreach ($logFile in $logFiles) 
  {
     #when we rename file and put date on it, we put a double underscore in it 
     #so that on the next pass, we ignore all files iwth double underscores 
     if (-not $logfile.FullName.Contains("__"))
     {
         $fileDateFormat = get-date -f _yyyy_MM_dd
         # file may have suffix of .txt or .log 
         $newFileName = $logFile.FullName 
         $newFileName = $newFileName.Replace(".log","__$fileDateFormat.log"); 
         $newFileName = $newFileName.Replace(".txt","__$fileDateFormat.txt"); 
         # If someone runs scripts twice on same day, we don't want 
         # file with zero size to wipe out the file from the previous run, so don't copy it in that case. 
         if (-not (Test-Path $newFileName))
           {
             copy-item $logFile.FullName $newFileName 
             $null | Set-Content $logFile.FullName  
           }
     }
  } 

#--------------------------------------------------------------
# Part 3 : delete log files here if file is over x days old 
#--------------------------------------------------------------

$keepDays = 60 
$files = GET-CHILDITEM $logFolder -RECURSE -FILE | Where  LastWriteTime -lt  (Get-Date).AddDays(-$keepDays)

foreach ($file in $files)
{
   #Write-Host $file.LastWriteTime 
   if ($file.LastWriteTime.AddDays($keepDays) -lt (GET-Date))
   {
       #Write-Host "$file.FileName will be deleted" 
       $deleteCount = $deleteCount + 1 
       Remove-Item $file.FullName
   }
}

Write-Host "End" 

Results

The first day you run it, the old large file will be renamed. Then each subsequent day will only have the activity for that single day.

Leave a Reply

Recent Posts

Archives

Categories

Meta

All Rights Reserved Theme by 404 THEME.