0
votes

I have the below Powershell script which tries to archive the logs.

  1. 1st step is to move all the files that contain Spotfire.Dxp...* string the ProjectLogsDir directory.

  2. 2nd step is identify the Spotfire.Dxp...* files in RotatedLogsDir directory that are older than 60 days and put them in a zip file together in a ArchivedLogsDir directory

  3. 3rd step is to delete zipped file older than 120 days.

here 2nd step function isn't working with error

E:\TIBCO\logsArchival\rotatedDir\C0005749_2014-05-09-12-53-47_Spotfire.Dxp.Web.231.log E:\TIBCO\logsArchival\rotatedDir\C0005749_2014-05-09-12-53-47_Spotfire.Dxp.Web.232.log: WARNING: The filename, directory name, or volume label syntax is incorrect.

   $sysname=$env:COMPUTERNAME
   $Date = Get-Date
   $Now = Get-Date -format yyyy-MM-dd-HH-mm-ss
   $host_date=$sysname +"_"+ $Now
 
   $RotateDays = "60"
   $ArchiveDays="120"
   $ProjectLogsDir = "E:\TIBCO\*\6.0.0\Logfiles"
   $RotatedLogsDir = "E:\TIBCO\logsArchival\rotatedDir"
   $ArchivedLogsDir= "E:\TIBCO\logsArchival\archiveDir"
   $psLogsDir= "E:\TIBCO\logsArchival\shLogsDir"     
   $LastRotate = $Date.AddDays(-$RotateDays)
   $LastArchive = $Date.AddDays(-$ArchiveDays)
   $RenameLogFiles = Get-Childitem $ProjectLogsDir -include Spotfire.Dxp.*.*.* -exclude    spotfire.Dxp.web.KeepAlive.* -Recurse
   $RenameLogFiles
   $RenameLogFiles | Where-Object {!$_.PSIsContainer} |  Rename-Item -NewName { $host_date +"_"+ $_.Name.Replace(' ','_') };
 
   $RotatedLogFiles = Get-Childitem $ProjectLogsDir -include *_Spotfire.Dxp.*.*.* -Recurse
   $RotatedLogFiles
   $RotatedLogFiles | move-item -destination "$RotatedLogsDir"
 
   $ZippedLogFiles = Get-Childitem $RotatedLogsDir -include *_Spotfire.Dxp.*.*.* -Recurse | Where {$_.LastWriteTime -le "$LastRotate"}
   $ZippedLogFiles
 
   function create-7zip([String] $aDirectory, [String] $aZipfile) {
     [string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe";
     [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory";
     & $pathToZipExe $arguments;
      } 
 
   create-7zip "$ZippedLogFiles"  "$ArchivedLogsDir\$host_date.zip"
 
    $ZippedLogFiles | Remove-Item -Force
 
    $DeleteZippedFiles = Get-Childitem $ArchivedLogsDir\*.zip -Recurse | Where  {$_.LastWriteTime -le "$LastArchive"}
    $DeleteZippedFiles
    $DeleteZippedFiles | Remove-Item -Force
 
    $DeletePsFiles = Get-Childitem $psLogsDir\*.log -Recurse | Where {$_.LastWriteTime -le "$LastRotate"}
    $DeletePsFiles
    $DeletePsFiles | Remove-Item -Force

Please provide the help here to get the files zipped.

1

1 Answers

0
votes

The issue is that you are calling your 7-Zip function incorrectly. Look at what the function takes:

function create-7zip([String] $aDirectory, [String] $aZipfile) {

It has 2 strings as parameters. Then look at what you are calling it with:

create-7zip "$ZippedLogFiles"  "$ArchivedLogsDir\$host_date.zip"

"$ZippedLogFiles" was defined just before this by running $ZippedLogFiles = Get-Childitem $RotatedLogsDir with a few parameters to filter the results. So that right there is an array of FileInfo objects... not a string. That's the issue, is that you are not calling the function correctly.

What you really want to include is "$RotatedLogsDir\*_Spotfire.Dxp*.*" so try calling it with that instead of "$ZippedLogFiles".

Edit: Comment moved to answer for zipping only files over 60 days.

You can use built in .Net calls to archive things and not have to use 7-Zip. Even easier in my opinion would be to install the PowerShell Community Extensions and use their Write-Zip cmdlet like this:

$ZippedLogFiles = Get-Childitem $RotatedLogsDir -include *_Spotfire.Dxp.*.*.* -Recurse | Where {$_.LastWriteTime -le "$LastRotate"}
$ZippedLogFiles
$ZippedLogFiles | Write-Zip "$ArchivedLogsDir\$host_date.zip"