I am using the popular cleanuplogs script for my exchange server. All works fine. But I want to be able to set a different “olderThan” date for the certain folders.<\/p>\n
is it best to make multiple tasks for each folder? OR is there a way i can set it ?<\/p>\n
This is the script<\/p>\n
$ExecutionPolicy = Get-ExecutionPolicy
\nif ($ExecutionPolicy -ne “RemoteSigned”) {
\nSet-ExecutionPolicy RemoteSigned -Force
\n}<\/p>\n
$days = 2<\/p>\n
$IISLogPath = \"C:\\inetpub\\logs\\LogFiles\"
\n$ExchangeLoggingPath = \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\Logging\"
\n$ETLLoggingPath = \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\Bin\\Search\\Ceres\\Diagnostics\\ETLTraces\"
\n$ETLLoggingPath2 = \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\Bin\\Search\\Ceres\\Diagnostics\\Logs\"<\/p>\n
Function CleanLogfiles($TargetFolder) {
\nWrite-Host -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder<\/p>\n
if (Test-Path $TargetFolder) { I am using the popular cleanuplogs script for my exchange server. All works fine. But I want to be able to set a different “olderThan” date for the certain folders.<\/p>\n is it best to make multiple tasks for each folder? OR is there a way i can set it ?<\/p>\n This is the script<\/p>\n $ExecutionPolicy = Get-ExecutionPolicy $days = 2<\/p>\n $IISLogPath = \"C:\\inetpub\\logs\\LogFiles\" Function CleanLogfiles($TargetFolder) { if (Test-Path $TargetFolder) { The way that I would handle it would be to change your CleanLogfiles function to accept a second argument<\/p>\n Call the functions like this though<\/p>\n
\n$Now = Get-Date
\n$LastWrite = $Now.AddDays(-$days)
\n$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $.Name -like “*.log” -or $<\/em>.Name -like “.blg\" -or $_.Name -like \"<\/em>.etl” } | Where-Object { $_.lastWriteTime -le “$lastwrite” } | Select-Object FullName
\nforeach ($File in $Files) {
\n$FullFileName = $File.FullName
\nWrite-Host “Deleting file $FullFileName” -ForegroundColor “yellow”;
\nRemove-Item $FullFileName -ErrorAction SilentlyContinue | out-null
\n}
\n}
\nElse {
\nWrite-Host “The folder $TargetFolder doesn’t exist! Check the folder path!” -ForegroundColor “red”
\n}
\n}
\nCleanLogfiles($IISLogPath)
\nCleanLogfiles($ExchangeLoggingPath)
\nCleanLogfiles($ETLLoggingPath)
\nCleanLogfiles($ETLLoggingPath2)<\/p>","upvoteCount":3,"answerCount":7,"datePublished":"2021-05-13T11:19:07.000Z","author":{"@type":"Person","name":"haveyoutriedinverting","url":"https://community.spiceworks.com/u/haveyoutriedinverting"},"suggestedAnswer":[{"@type":"Answer","text":"<\/a>Set execution policy if not set<\/h1>\n
\nif ($ExecutionPolicy -ne “RemoteSigned”) {
\nSet-ExecutionPolicy RemoteSigned -Force
\n}<\/p>\n<\/a>Cleanup logs older than the set of days in numbers<\/h1>\n
<\/a>Path of the logs that you like to cleanup<\/h1>\n
\n$ExchangeLoggingPath = \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\Logging\"
\n$ETLLoggingPath = \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\Bin\\Search\\Ceres\\Diagnostics\\ETLTraces\"
\n$ETLLoggingPath2 = \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\Bin\\Search\\Ceres\\Diagnostics\\Logs\"<\/p>\n<\/a>Clean the logs<\/h1>\n
\nWrite-Host -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder<\/p>\n
\n$Now = Get-Date
\n$LastWrite = $Now.AddDays(-$days)
\n$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $.Name -like “*.log” -or $<\/em>.Name -like “.blg\" -or $_.Name -like \"<\/em>.etl” } | Where-Object { $_.lastWriteTime -le “$lastwrite” } | Select-Object FullName
\nforeach ($File in $Files) {
\n$FullFileName = $File.FullName
\nWrite-Host “Deleting file $FullFileName” -ForegroundColor “yellow”;
\nRemove-Item $FullFileName -ErrorAction SilentlyContinue | out-null
\n}
\n}
\nElse {
\nWrite-Host “The folder $TargetFolder doesn’t exist! Check the folder path!” -ForegroundColor “red”
\n}
\n}
\nCleanLogfiles($IISLogPath)
\nCleanLogfiles($ExchangeLoggingPath)
\nCleanLogfiles($ETLLoggingPath)
\nCleanLogfiles($ETLLoggingPath2)<\/p>","upvoteCount":3,"datePublished":"2021-05-13T11:19:07.000Z","url":"https://community.spiceworks.com/t/exchange-cleanup-logs-script/799780/1","author":{"@type":"Person","name":"haveyoutriedinverting","url":"https://community.spiceworks.com/u/haveyoutriedinverting"}},{"@type":"Answer","text":"Function CleanLogfiles($TargetFolder, $TargetDays) {\n \n Write-Host -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder\n\n if (Test-Path $TargetFolder) {\n $Now = Get-Date\n $LastWrite = $Now.AddDays(-$TargetDays)\n $Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Name -like \"*.log\" -or $_.Name -like \"*.blg\" -or $_.Name -like \"*.etl\" } | Where-Object { $_.lastWriteTime -le \"$lastwrite\" } | Select-Object FullName\n foreach ($File in $Files) {\n $FullFileName = $File.FullName\n Write-Host \"Deleting file $FullFileName\" -ForegroundColor \"yellow\";\n Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null\n }\n }\n Else {\n Write-Host \"The folder $TargetFolder doesn't exist! Check the folder path!\" -ForegroundColor \"red\"\n }\n}\n\nCleanLogfiles $IISLogPath 2\nCleanLogfiles $ExchangeLoggingPath 3\nCleanLogfiles $ETLLoggingPath 5\nCleanLogfiles $ETLLoggingPath2 20\n\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-05-13T11:52:12.000Z","url":"https://community.spiceworks.com/t/exchange-cleanup-logs-script/799780/2","author":{"@type":"Person","name":"atypical-it-guy","url":"https://community.spiceworks.com/u/atypical-it-guy"}},{"@type":"Answer","text":"
CleanLogfiles $IISLogPath 2\nCleanLogfiles $ExchangeLoggingPath 3\nCleanLogfiles $ETLLoggingPath 5\nCleanLogfiles $ETLLoggingPath2 20\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2021-05-13T11:53:53.000Z","url":"https://community.spiceworks.com/t/exchange-cleanup-logs-script/799780/3","author":{"@type":"Person","name":"atypical-it-guy","url":"https://community.spiceworks.com/u/atypical-it-guy"}},{"@type":"Answer","text":"