daily pastebin goal
68%
SHARE
TWEET

Start-ExchangeLogMaintenance.ps1

anonit Dec 25th, 2017 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ########################################
  2. #                                      #
  3. #   Start-ExchangeLogMaintenance.ps1   #
  4. #                                      #
  5. ########################################
  6.  
  7. # Delete Exchange diagnostics logfiles and IIS logfiles
  8. # Modify the $DaysToRetain variable to modify the amount of logs to keep
  9.  
  10. $DaystoRetain="2"
  11.  
  12. # This will delete log files from the system, ensure you have a backup prior to running
  13.  
  14. # Deletes the *.blg files from performance monitoring - Default C:\Program Files\Microsoft\Exchange Server\V15\Logging\Diagnostics\
  15. # Deletes the *.evt files from Office search - Default C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\
  16. # Deletes the *.log files from IIS - Default C:\inetpub\logs\LogFiles\
  17. # Deletes the *.log files from %ExchangeInstallDir%\Logging\
  18. # Deletes the *.log files from %ExchangeInstallDir%\TransportRoles\Logs
  19.  
  20. # if $env:ExchangeInstallPath is blank the script will abort
  21.  
  22. # Get Exchange install location
  23. if (!$env:ExchangeInstallPath)
  24. {
  25.     write-error "ExchangeInstallPath is blank"
  26.     exit
  27. }
  28.  
  29. # References:
  30. #
  31. # https://social.technet.microsoft.com/wiki/contents/articles/31117.exchange-2013-logging-clear-out-the-log-files.aspx
  32. # https://gallery.technet.microsoft.com/Clear-Exchange-2013-Log-71abba44#content
  33. # https://stackoverflow.com/questions/26784883/get-iis-log-location-via-powershell
  34. # https://blogs.technet.microsoft.com/exchange/2015/04/20/a-better-way-to-collect-logs-from-your-exchange-servers/
  35.  
  36. Import-Module WebAdministration
  37.  
  38. # Stop and Disable the Exchange Health Management and Diagnostics Services
  39. $ServiceList="MSExchangeHM","MSExchangeDiagnostics"
  40. foreach ($Service in $ServiceList)
  41. {
  42.     stop-service $Service
  43.     set-service $Service -StartupType Disabled
  44. }
  45.  
  46. # Stop and disable the Scheduled Tasks for the performance monitoring
  47. $ScheduledTaskList="ExchangeDiagnosticsDailyPerformanceLog","ExchangeDiagnosticsPerformanceLog"
  48. foreach ($ScheduledTask in $ScheduledTaskList)
  49. {
  50.     $ScheduledTaskPath=(get-scheduledtask $scheduledtask).taskpath
  51.     Stop-ScheduledTask -taskname $ScheduledTask -taskpath $ScheduledTaskPath
  52.     Disable-ScheduledTask -taskname $ScheduledTask -taskpath $ScheduledTaskPath
  53. }
  54.  
  55. # Get the diagnostic log file location
  56. # uses logman to query the performance monitors
  57. # remove the left 22 characters as a sample would be:
  58. # Root Path:            C:\Program Files\Microsoft\Exchange Server\V15\Logging\Diagnostics\DailyPerformanceLogs
  59. # This leaves us with just the path C:\Program Files\Microsoft\Exchange Server\V15\Logging\Diagnostics\DailyPerformanceLogs
  60. # use logman without arguements to get list of performance logs
  61. $DailyPerformanceLogPath=logman query ExchangeDiagnosticsDailyPerformanceLog | find "Root Path:"
  62. $DailyPerformanceLogPath=$DailyPerformanceLogPath.substring(22)
  63. $PerformanceLogPath=logman query ExchangeDiagnosticsPerformanceLog | find "Root Path:"
  64. $PerformanceLogPath=$PerformanceLogPath.substring(22)
  65.  
  66. # Remove the performance monitoring logs
  67. Get-ChildItem $DailyPerformanceLogPath -recurse -Include *.blg | Where-Object lastwritetime -lt (Get-Date).AddDays(-$DaysToRetain) | remove-item
  68. Get-ChildItem $PerformanceLogPath -recurse -Include *.blg | Where-Object lastwritetime -lt (Get-Date).AddDays(-$DaysToRetain) | remove-item
  69.  
  70. # ETL file location is stored at HKLM\software\microsoft\office server\16.0\search\diagnostics\tracing
  71. # get the path locaton and remove the files
  72. $regpath=get-itemproperty -path "registry::HKEY_LOCAL_MACHINE\software\microsoft\office server\16.0\search\diagnostics\tracing\" -name tracingpath
  73. Get-ChildItem $regpath.tracingpath -recurse -Include *.etl | Where-Object lastwritetime -lt (Get-Date).AddDays(-$DaysToRetain) | remove-item
  74.  
  75. # get IIS log file locations
  76. foreach($WebSite in $(get-website))
  77. {
  78.     $IISLogPath="$($Website.logFile.directory)\w3svc$($website.id)".replace("%SystemDrive%",$env:SystemDrive)
  79.  
  80.     # Delete the log files
  81.     Get-ChildItem $IISLogPath -recurse -Include *.log | Where-Object lastwritetime -lt (Get-Date).AddDays(-$DaysToRetain) | remove-item
  82. }
  83.  
  84. # Remomve the %ExchangeInstallDir%\logging\*.log and %ExchangeInstallDir%\TransportRoles\Logs\*.log
  85. Get-ChildItem $env:ExchangeInstallPath\Logging\ -recurse -Include *.log | Where-Object lastwritetime -lt (Get-Date).AddDays(-$DaysToRetain) | remove-item
  86. Get-ChildItem $env:ExchangeInstallPath\TransportRoles\Logs -recurse -Include *.log | Where-Object lastwritetime -lt (Get-Date).AddDays(-$DaysToRetain) | remove-item
  87.  
  88.  
  89. # Enable the Scheduled Tasks for the performance monitoring
  90. foreach ($ScheduledTask in $ScheduledTaskList)
  91. {
  92.     $ScheduledTaskPath=(get-scheduledtask $scheduledtask).taskpath
  93.     Enable-ScheduledTask -taskname $ScheduledTask -taskpath $ScheduledTaskPath
  94. }
  95.  
  96. # Enable and start the Exchange Health Management and Diagnostics Services
  97. foreach ($Service in $ServiceList)
  98. {
  99.     set-service $Service -StartupType Auto
  100.     start-service $Service
  101. }
RAW Paste Data
Top