I know this question has been asked several times but my requirement is slightly different from what I have read here and on google.
I have the below folder structure.
c:\weblogs\Servers\Server1
c:\weblogs\Servers\Server2
Server 1 folder may contain folders such as:
W3SVC1
W3SVC2
W3SVC3
Server 2 would have a similar folder structure.
I am trying to create a script which would look at every W3SVC*
folder for .log
files older than 30 days.
It would then create a zip file called Oldlogs-Server*-W3SVC*.zip
within each such W3SVC*
subfolder and move the older logs to that file. On the second run it would simply move any new files older than 30 days to the respective zip file.
I am not a developer but any examples or suggestions would be helpful if there is not an existing script available.
This is the script which I believe comes closest to what I am trying to achieve:
It currently throws an error:
"\WebApps\LogHarvest\pkzipc.exe needed"
# Alias for pkzip
if (-not (test-path "$env:F:\WebApps\LogHarvest\pkzipc.exe")) {
throw "$env:F:\WebApps\LogHarvest\pkzipc.exe needed"
}
set-alias sz "$env:WebApps\LogHarvest\pkzipc.exe"
############################################
#### Variables
$filePath = "F:\Weblogs\Test"
$log = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq ".log" }
########### END of VARABLES ##################
foreach ($file in $log) {
$name = $file.name
$directory = $file.DirectoryName
$zipfile = $name.Replace(".log",".7z")
sz a -t7z "$directory\$zipfile" "$directory\$name"
}
########### END OF SCRIPT ##########
You're testing a path
$env:F:\WebApps\LogHarvest\pkzipc.exe
, which evaluates to\WebApps\LogHarvest\pkzipc.exe
, since you probably don't have an environment variable$env:F
. Because of that your script is looking for the executable on the current drive, which most likely isC:
rather thanF:
. This is what causes the error you observed.The next statement tries to set an alias
$env:WebApps\LogHarvest\pkzipc.exe
. If you actually have an environment variable$env:WebApps
change your code to this:If you don't have that environment variable change your code to this:
As a side note: instead of creating an alias for your script I'd use the variable for the executable with the call operator (
&
):Also, I'm not sure if
pkzipc
can actually handle 7z archives. Or if it supports the argumentsa -t7z
in the first place. Those look like arguments for the 7zip programs to me.