Welcome Forums General PowerShell Q&A Archive Log Files Older Than 10 Days

Viewing 1 reply thread
  • Author
    Posts
    • #166528
      Participant
      Topics: 1
      Replies: 0
      Points: 18
      Rank: Member

      I am attempting to archive log files on a group of servers after the files are older than 10 days. If I run the portion of the code to filter the files on each server, I get an accurate list. When I run the entire script, the files on only the last server in the list are archived. I have spent hours trying to get this to work.

      Thanks!

      $Servers = Get-Content C:\Scripts\Scripts\FUTS\FutsServers.txt
      $path = "\\$Server\C$\AppLogs\FUTS-PM\"
      $destpath = "\\$Server\C$\AppLogs\FUTS Archive\"
      $datestring =(Get-Date -UFormat "%Y%m%d")
      ForEach ($Server in $Servers)
      {
      Get-ChildItem -Path "\\$Server\C$\AppLogs\FUTS-PM\*" | 
      Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-10)}}
          ForEach ($Server in $Servers){
          Compress-Archive -Path $path -CompressionLevel Fastest -DestinationPath "$destpath$datestring" 
      }
    • #166591
      Senior Moderator
      Topics: 8
      Replies: 1160
      Points: 4,056
      Helping Hand
      Rank: Community Hero

      First Foreach loop simply returns the files which are not modified for last 10 days and doing nothing after that. You need the the archive code in the same loop.

      $Servers = Get-Content C:\Scripts\Scripts\FUTS\FutsServers.txt
      $path = "\\$Server\C$\AppLogs\FUTS-PM\"
      $destpath = "\\$Server\C$\AppLogs\FUTS Archive\"
      $datestring = (Get-Date -UFormat "%Y%m%d")
      
      ForEach ($Server in $Servers) {
          $FileList = Get-ChildItem -Path "\\$Server\C$\AppLogs\FUTS-PM\*" | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-10)}
          Compress-Archive -Path $FileList.FullName -CompressionLevel Fastest -DestinationPath $destpath$datestring
      }
      
Viewing 1 reply thread
  • The topic ‘Archive Log Files Older Than 10 Days’ is closed to new replies.