0

I am trying to create a script that zips files according to date(one day of Tomcat logs & one day of IIS Logs), then moves the appropriate zip file to a share.

I have tried to use several powershell scripts and batch files(.cmd) to get this done, all with no avail.

I have also tried to use http://exchangeserverpro.com/powershell-script-iis-logs-cleanup but cannot get it to work on a daily basis.

Can anyone help? I got the following to work but cant seem to figure out how to get the files to be exported before they are deleted

$purge = (Get-Date).AddDays(-1)
$path = "D:\Tomcat\apache-tomcat-7.0.39\logs"

# Delete files older than the $purge.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $purge } | Remove-Item -Force
Avshalom
  • 8,657
  • 1
  • 25
  • 43

2 Answers2

1

You already have a script to retrieve the log files depending on the CreatonTime. Now you need to Zip the files and move them using the Move-Item cmdlet to the share (you don't need to copy them because you want to delete them anyway).

Community
  • 1
  • 1
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
0

First, See how to Zip with Powershell:

How to create a zip archive with PowerShell?

...
$FilesToPurge = Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $purge }

Add the Files to ZIP:

$FilesToPurge | Write-Zip \\Server\Share\Archive.zip # or any other function.

Delete the Items:

$FilesToPurge | Remove-Item
Community
  • 1
  • 1
Avshalom
  • 8,657
  • 1
  • 25
  • 43