I'm basically trying to do some basic log deleting operation
by several steps.
1st - get all the files that are older then X days (7 in my case)
2nd - zip them to a different location
3rd - delete the zipped files
4th - go over the folder of zipped logs and delete older then 30 Days
1st - accomplished - I get the list of files
3rd - not a problem - I think
4th - same as 1st ...
2nd ... here I try to use the 7zip as it is already embedded in Windows we have strict policies regrading 3rd party tools so winrar is not an option
this is the code I have tried but I don't get any results it fails on the zip command
if ((Test-Path "$env:ProgramFiles\7-Zip\7z.exe") -eq $true){Set-Alias sz "$env:ProgramFiles\7-Zip\7z.exe" }
$DateStr = (Get-Date).ToString("dd-MM-yyyy")
$arcPath = "D:\SDDP\LOG_Archive_$DateStr.zip"
$limit = (Get-Date).AddDays(-7)
$path = "D:\SDDP\LOG"
$filesToBackUP = Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit }
ForEach ( $file in $filesToBackUP )
{
sz a -tzip $archPath $file.FullName
}
the error I get is :
Open archive: D:\SDDP\LOG\DISTRIBUTOR(232)\04-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
sz : ERROR: D:\SDDP\LOG\DISTRIBUTOR(232)\04-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
At line:13 char:9
+ sz a -tzip $archPath $file.FullName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: D:\SDDP\...4-09-2015_1.csv:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
D:\SDDP\LOG\DISTRIBUTOR(232)\04-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
Open ERROR: Can not open the file as [zip] archive
ERRORS:
Is not archive
System ERROR:
Incorrect function.
Reference to the zip command I took from here
as I gone through the add reference there is nothing about creating a new 7zip file to add the files to, so I suppose it creates automatically but not so sure about it.
Please advice.
Thanks.
Try something along the lines of:
[string]$Zip = "C:\path to 7zip\7-zip\7z.exe";
[array]$args = "a", "-tzip", "-y", "-r", "$arcPath ";
ForEach ( $file in $filesToBackUP )
{
$Zip $args $file;
}
Sorry, I am currently on the move and unable to test this.