From 4a902ad6f8ddb8bb6b1de7583c8801a15f360450 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Thu, 15 Feb 2024 16:03:01 +0100 Subject: [PATCH 1/2] Update con logfile giornaliero + pulizia --- WDC_SyncWebCompo.ps1 | 48 +++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/WDC_SyncWebCompo.ps1 b/WDC_SyncWebCompo.ps1 index 3affa6e..624d182 100644 --- a/WDC_SyncWebCompo.ps1 +++ b/WDC_SyncWebCompo.ps1 @@ -2,60 +2,63 @@ ####INIZIO DICHIARAZIONE VARIABILI#### +# num gg per cui tenere i log +$Days = "30" +$CutoffDate = (Get-Date).AddDays(-$Days) + #rilevo time per log inizio analisi $startTime = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") # avvio stopwatch -$mainStopWatch = [system.diagnostics.stopwatch]::StartNew() +$mainStopWatch = [system.diagnostics.stopwatch]::StartNew() #abilitazione log: 0=disattivo, 1=abilitato $logEnable = 0 #cartella file di log -$WdcLogFolder="c:\Steamware\Logs\WDC\" +$WdcLogFolder = "c:\Steamware\Logs\WDC\" #nome file di log -$logFile = Join-Path $WdcLogFolder "WDC_Log.log" +$dayName = "WDC_" + (Get-Date).toString("yyyy-MM-dd") + ".log" +$logFile = Join-Path $WdcLogFolder $dayName #folder source per robocopy -$sourceFolder = "\\stor01\TEAM DRIVES\30_Clienti\ABH\DataImport\Compo" +$sourceFolder = "U:\NextCloud\ABH\Compo" #folder destination per robocopy -$destFolder = "\\IIS04\WebDoorSync\NewComp\Compo" +$destFolder = "U:\WebDoorSync\NewComp\Compo" + +# ex IIS04 +#$sourceFolder = "\\stor01\TEAM DRIVES\30_Clienti\ABH\DataImport\Compo" +#$destFolder = "\\IIS04\WebDoorSync\NewComp\Compo" ####FINE DICHIARAZIONE VARIABILI#### #dichiaro funzione per scrittura output -Function WriteLogOutput -{ +Function WriteLogOutput { Param ([string]$logString) #scrivo su file la stringa Add-content $logFile -value "$logString" #scrivo su terminale la stringa se $logEnable=1 - if($logEnable -eq 1){ + if ($logEnable -eq 1) { Write-Output($logString) } } #creazione folder di Log se non già esistente -if (Test-Path $WdcLogFolder) -{ +if (Test-Path $WdcLogFolder) { } -else -{ +else { New-Item $WdcLogFolder -ItemType Directory } #effettuo robocopy -try -{ +try { WriteLogOutput "------WDC SYNC------" - if ($logEnable -eq 1) - { + if ($logEnable -eq 1) { robocopy $sourceFolder $destFolder /mir /log+:$logFile } - else - { + else { WriteLogOutput "" WriteLogOutput "Started: $startTime" WriteLogOutput "Source: $sourceFolder" @@ -64,15 +67,18 @@ try } WriteLogOutput "Robocopy OK" } -catch -{ +catch { WriteLogOutput "An error occurred" } +# elimino file log troppo vecchi +Get-ChildItem -Path $WdcLogFolder -Recurse -File | Where-Object { $_.LastWriteTime -lt $CutoffDate } | Remove-Item -Force +WriteLogOutput "Log Cleanup complete!" + # fermo stopwatch e calcolo durata script $mainStopWatch.Stop() $durataScript = $mainStopWatch.Elapsed.TotalSeconds #scrivo le statistiche WriteLogOutput "Duration: $durataScript seconds" -WriteLogOutput "" \ No newline at end of file +WriteLogOutput "" From a9c0f5fbd8fbd8551b870b1791abab6ded1954ac Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Thu, 15 Feb 2024 16:13:04 +0100 Subject: [PATCH 2/2] Aggiunta script pulizia log IIS --- CleanupLogs.ps1 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 CleanupLogs.ps1 diff --git a/CleanupLogs.ps1 b/CleanupLogs.ps1 new file mode 100644 index 0000000..4c33615 --- /dev/null +++ b/CleanupLogs.ps1 @@ -0,0 +1,20 @@ +$LogPath = "C:\inetpub\logs" +$maxDaystoKeep = -90 +$outputPath = "C:\Steamware\Logs\Cleanup_Logs.log" + +Write-Output "Start Cleanup" > $outputPath + +$itemsToDelete = dir $LogPath -Recurse -File *.log | Where LastWriteTime -lt ((get-date).AddDays($maxDaystoKeep)) + +if ($itemsToDelete.Count -gt 0){ + ForEach ($item in $itemsToDelete){ + "$($item.BaseName) is older than $((get-date).AddDays($maxDaystoKeep)) and will be deleted" | Add-Content $outputPath + Remove-Item $item.FullName -Verbose + } +} +ELSE{ + "No items to be deleted today $($(Get-Date).DateTime)" | Add-Content $outputPath + } + +Write-Output "Cleanup of log files older than $((get-date).AddDays($maxDaystoKeep)) completed..." +start-sleep -Seconds 3 \ No newline at end of file