Hi guys,
I was wondering if someone can help me. I have to create a PowerShell script to compress log files older than 2 days from multiple paths and move then. I want to use Octopus variables in my script, but I don’t know how to accomplish that. So far I have this:
#get the list of files in the original folder
$rootFolder = "$Octopusvariable1"
$tempVariable = $rootFolder
$files = Get-ChildItem -Path $rootFolder
$FileType = "log"
$targetPath = "$Octopusvariable2"
#create a temporary folder using today's date
$tempFolderRoot = "C:\Temp_"
$date = Get-Date
$date = $date.ToString("yyyy-MM-dd")
$tempFinalFolder = "$tempFolderRoot$date"
New-Item -ItemType directory -Path $tempFinalFolder -Force
#decide how long back to go
$timespan = new-timespan -days 0
#move the files to a temporary location
foreach($file in $files)
{
$fileLastModifieddate = $file.LastWriteTime
if(((Get-Date) - $fileLastModifiedDate) -gt $timespan)
{
Move-Item "$rootFolder\$file" -destination $tempFinalFolder
}
}
#Compress the files to ZIP
Get-ChildItem $tempFinalFolder | ForEach-Object {
$FileName = $_.Name -replace $FileType, ".zip"
Compress-Archive -LiteralPath $_.FullName -DestinationPath $targetPath$FileName -Update
Write-Host $FileName
$FileName = $null
}
#Remove Temp Folder
Remove-Item $tempFinalFolder -RECURSE
So… Inside #Octopusvariable1, I have to insert multiple paths, for example:
C:\Logs\app1
C:\Logs\app2
C:\Logs\app3
For #Octopusvariable2, I have to send those files to another server folder root but respecting the same folder structure… So files from C:\Logs\app1 must go to \Server2\RootfolderLogs\app1, files from C:\Logs\app2 must go to \Server2\Rootfolder\Logs\app2… and so on.
Do you guys have any idea, how to build my script?
Thanks!
Ney Santos