Scripts de compression et suppression des logs IIS avec des outils natifs / Compression and removal scripts for IIS logs with built-in tools

English version

[MAJ 23/06/2016] IIS n'inclut pas de script ou de mécanisme de rotation de ses logs et par expérience je sais que peu de clients implémentent un tel mécanisme. Je vais vous montrer au travers de cet article qu'il est très facile et en peu de lignes de mettre en place un tel mécanisme. Pour cela je m'appuierai sur deux scripts n'utilisant que des ressources présentes nativement sous Windows:

Et vous vous dites en PowerShell ça doit pouvoir se faire ... la réponse est


Version française

[Updated 06/23/2016] IIS doesn't include scripts or mechanism for the IIS logs rotation and I know by par experience that only few customer implement a such mechanism. I'll show you through this article that it is very easy and in few lines to set up such a mechanism. For this I will build on two scripts using only these Windows built-in resources:

And you say to yourself in PowerShell it should be easy ... the answer is here.

 @ECHO OFF
CLS
SETLOCAL

SET COMPRESSION_TOOL=makecab
SET CURRENT_DIR=%~dp0
SET CURRENT_DIR=%CURRENT_DIR:~0,-1%
SET LOG_FILE=%~dpn0.log
SET ERROR_FILE=%~dpn0.err
SET TIMESTAMP=%DATE:~-10%_%TIME%
SET TIMESTAMP=%TIMESTAMP:/=_%
SET TIMESTAMP=%TIMESTAMP::=_%
SET TIMESTAMP=%TIMESTAMP:.=_%
SET TIMESTAMP=%TIMESTAMP: =_%
SET TIMESTAMP=%TIMESTAMP:,=_%

REM Getting the site id for every website
FOR /F "delims=" %%i IN ('%WINDIR%\system32\inetsrv\appcmd list site /text:id') DO (
REM Getting the log folder for every website got by the id
FOR /F "delims=" %%j IN ('%WINDIR%\system32\inetsrv\appcmd.exe list site /id:%%i /text:logfile.directory') DO (
ECHO [%TIMESTAMP%] Processing %%j\W3SVC%%i ... >> %LOG_FILE% 2>>%ERROR_FILE%
REM Compressing the UTF-8 log files older than 10 days by using makecab 
FORFILES /p %%j\W3SVC%%i /s /m u_ex*.log /d -10 /c "cmd /c %COMPRESSION_TOOL% @path @fname.cab && echo Compressing @path ... && del @path" >> %LOG_FILE% 2>>%ERROR_FILE%
REM Compressing the ANSI log files older than 10 days by using makecab 
FORFILES /p %%j\W3SVC%%i /s /m ex*.log /d -10 /c "cmd /c %COMPRESSION_TOOL% @path @fname.cab && echo Compressing @path ... && del @path" >> %LOG_FILE% 2>>%ERROR_FILE%
)
)

SET COMPRESSION_TOOL=
SET CURRENT_DIR=
SET LOG_FILE=
SET ERROR_FILE=
SET TIMESTAMP=

 

 @ECHO OFF
CLS
SETLOCAL


SET CURRENT_DIR=%~dp0
SET CURRENT_DIR=%CURRENT_DIR:~0,-1%
SET LOG_FILE=%~dpn0.log
SET ERROR_FILE=%~dpn0.err
SET TIMESTAMP=%DATE:~-10%_%TIME%
SET TIMESTAMP=%TIMESTAMP:/=_%
SET TIMESTAMP=%TIMESTAMP::=_%
SET TIMESTAMP=%TIMESTAMP:.=_%
SET TIMESTAMP=%TIMESTAMP: =_%
SET TIMESTAMP=%TIMESTAMP:,=_%

REM Getting the site id for every website
FOR /F "delims=" %%i IN ('%WINDIR%\system32\inetsrv\appcmd list site /text:id') DO (
REM Getting the log folder for every website got by the id
FOR /F "delims=" %%j IN ('%WINDIR%\system32\inetsrv\appcmd.exe list site /id:%%i /text:logfile.directory') DO (
ECHO [%TIMESTAMP%] Processing %%j\W3SVC%%i ... >> %LOG_FILE% 2>>%ERROR_FILE%
REM Removing the UTF-8 log files older than 30 days
FORFILES /p %%j\W3SVC%%i /s /m u_ex*.* /d -30 /c "cmd /c echo Erasing @path ... && del @path /s" >> %LOG_FILE% 2>>%ERROR_FILE%
REM Removing the UTF-8 log files older than 30 days
FORFILES /p %%j\W3SVC%%i /s /m ex*.* /d -30 /c "cmd /c echo Erasing @path ... && del @path /s" >> %LOG_FILE% 2>>%ERROR_FILE%
)
)

SET CURRENT_DIR=
SET LOG_FILE=
SET ERROR_FILE=
SET TIMESTAMP=

Laurent.