# Every month a new log file is created\r
[String]$logDir = "C:\Users\Patrick\Desktop\DSMonRot\log"\r
# Keep log files for this amount of months (excluding the current month),\r
-# 0 for indefinite (currently not available)\r
-[Int32]$keepLogs = 1\r
+# 0 or less for indefinite (currently not available)\r
+# You should set this to at least the same as $keepMonths\r
+[Int32]$keepLogs = 2\r
\r
# Map network share to this drive letter, comment out if you don't want to use it\r
[String]$smbDrive = "Z"\r
}\r
}\r
\r
+function Rotate-Log {\r
+ if($keepLogs -le 0) {\r
+ return\r
+ }\r
+ \r
+ $keepLogsCount = $keepLogs\r
+ \r
+ Get-ChildItem $logDir -File | Where-Object {($_.Name -ne "$currMonth.log") -and ($_.Name -match "^\d{4,}-\d{2}\.log$")} | Sort-Object -Descending |\r
+ Foreach-Object {\r
+ if($keepLogsCount -ge 0) {\r
+ $keepLogsCount--\r
+ }\r
+ \r
+ if($keepLogsCount -eq -1) {\r
+ Write-Log "Deleting log file $($_.FullName)" -Path $logFile -Level Info\r
+ Remove-Item -Force $_.FullName\r
+ }\r
+ }\r
+}\r
+\r
$dsAdditionalArgs = @("--UseVSS")\r
\r
$errorMessages = @()\r
$currMonth = Get-Date -format "yyyy-MM"\r
$currDay = Get-Date -format "yyyy-MM-dd"\r
\r
-\r
if(!(Test-Path $logDir)) {\r
try {\r
New-Item -ItemType directory -Path $logDir -ErrorAction Stop | Out-Null\r
$errorMessages += "Could not disconnect network drive $smbDrive`: $_.Exception.Message"\r
}\r
}\r
+ \r
+ Rotate-Log\r
}\r
\r
if($emailOnError -and $errorMessages.Count -gt 0) {\r