Last active
October 30, 2016 10:28
-
-
Save amogram/0ccdb78882640df5d9cbc4fa64e0f111 to your computer and use it in GitHub Desktop.
Extracts IIS Log Files and exports to SQL Server Database. Script inspired by http://www.technologytoolbox.com/blog/jjameson/archive/2012/02/29/import-website-iis-logs-into-sql-server-using-log-parser.aspx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference = "Stop" | |
function ImportLogFiles([string] $httpLogPath) | |
{ | |
If ([string]::IsNullOrEmpty($httpLogPath) -eq $true) | |
{ | |
Throw "The log path must be specified." | |
} | |
[string] $logParser = "${env:ProgramFiles(x86)}" ` | |
+ "\Log Parser 2.2\LogParser.exe" | |
[string] $query = ` | |
[string] $query = ` | |
"SELECT" ` | |
+ " LogFilename" ` | |
+ ",TO_TIMESTAMP(date, time) AS EntryTime" ` | |
+ ",s-sitename AS SiteName" ` | |
+ ",s-computername AS ServerName" ` | |
+ ",s-ip AS ServerIpAddress" ` | |
+ ",cs-method AS Method" ` | |
+ ",cs-uri-stem AS UriStem" ` | |
+ ",cs-uri-query AS UriQuery" ` | |
+ ",s-port AS Port" ` | |
+ ",cs-username AS Username" ` | |
+ ",c-ip AS ClientIpAddress" ` | |
+ ",cs-version AS HttpVersion" ` | |
+ ",cs(User-Agent) AS UserAgent" ` | |
+ ",cs(Cookie) AS Cookie" ` | |
+ ",cs(Referer) AS Referrer" ` | |
+ ",cs-host AS Hostname" ` | |
+ ",sc-status AS HttpStatus" ` | |
+ ",sc-substatus AS HttpSubstatus" ` | |
+ ",sc-win32-status AS Win32Status" ` | |
+ ",sc-bytes AS BytesFromServerToClient" ` | |
+ ",cs-bytes AS BytesFromClientToServer" ` | |
+ ",time-taken AS TimeTaken" ` | |
+ " INTO IISLogs.IISLogs" ` | |
+ " FROM '$httpLogPath\*.log'" | |
[string[]] $parameters = @() | |
$parameters += "-i:IISW3C" | |
$parameters += "-o:sql" | |
$parameters += "-ignoreIdCols:on" | |
$parameters += "-createTable:off" | |
$parameters += "-server:localhost" | |
$parameters += "-database:ServerFileLogs" | |
$parameters += "-driver:`"sql server`"" | |
$parameters += "`"$query`"" | |
Write-Debug "Parameters: $parameters" | |
Write-Host "Importing log files to database..." | |
& $logParser $parameters | |
} | |
function RemoveLogFiles([string] $httpLogPath) | |
{ | |
If ([string]::IsNullOrEmpty($httpLogPath) -eq $true) | |
{ | |
Throw "The log path must be specified." | |
} | |
Write-Host "Removing log files..." | |
Remove-Item ($httpLogPath + "\*.log") | |
} | |
function Main | |
{ | |
Write-Host "Log Files Importer" | |
[string] $httpLogPath = "C:\Inetpub Log Files" | |
$filesExist = Test-Path "$httpLogPath\*.log" | |
If($filesExist -eq $False) | |
{ | |
Write-Host -Fore Yellow "No log files found. Script stopped." | |
exit | |
} | |
ImportLogFiles $httpLogPath | |
RemoveLogFiles $httpLogPath | |
Write-Host -Fore Green "Successfully imported log files." | |
} | |
Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment