Created
August 19, 2018 00:35
-
-
Save smaglio81/c433f0303fd874a3e7ab05c53341bc60 to your computer and use it in GitHub Desktop.
Modification of https://notsomany.wordpress.com/2017/12/08/iis-logs-to-sql-database-using-powershell-and-log-parser/
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
############## https://notsomany.wordpress.com/2017/12/08/iis-logs-to-sql-database-using-powershell-and-log-parser/ | |
$dbserver = "$env:COMPUTERNAME,1433" | |
$database = "iislogs" | |
$proxyserver = "<your-web-server>" | |
$ErrorActionPreference = "Stop" | |
$Error.Clear() | |
$name = "Import-IisLogFiles" | |
$start = [DateTime]::Now | |
$startFormatted = $start.ToString("yyyyMMddHHmmss") | |
$logpath = "E:\Logs\Scripts\IIS\$name\$name-log-$startFormatted.txt" | |
$logoutput = Enable-LogFile $logpath | |
$logParser = "${env:ProgramFiles(x86)}\Log Parser 2.2\LogParser.exe" | |
try { | |
#Import-Module Pscx -EA 0 | |
Write-Host "Start: $start" -ForegroundColor Yellow | |
function Import-IisLogFilesToDb( | |
[string] $LogPath, | |
[string] $IisServer, | |
[string] $IisSiteName, | |
[string] $DbServer, | |
[string] $DbName | |
) | |
{ | |
if ([string]::IsNullOrEmpty($LogPath) -eq $true) | |
{ | |
throw "The log path must be specified." | |
} | |
if ((Test-Path $LogPath) -eq $false) | |
{ | |
throw "The log file doesn't exist, $LogPath." | |
} | |
$query = ` | |
"SELECT" ` | |
+ " TO_TIMESTAMP(date, time) AS EntryTime" ` | |
+ ", LogFilename" ` | |
+ ", RowNumber" ` | |
+ ", '$IisSiteName' as SiteName" ` | |
+ ", '$IisServer' as ServerName" ` | |
+ ", s-ip AS sIp" ` | |
+ ", cs-method AS csMethod" ` | |
+ ", cs-uri-stem AS csUriStem" ` | |
+ ", cs-uri-query AS csUriQuery" ` | |
+ ", s-port AS sPort" ` | |
+ ", TO_STRING(cs-username) AS csUsername" ` | |
+ ", c-ip AS cIp" ` | |
+ ", cs(User-Agent) AS csUserAgent" ` | |
+ ", cs(Referer) AS csReferer" ` | |
+ ", sc-status AS scStatus" ` | |
+ ", sc-substatus AS scSubstatus" ` | |
+ ", sc-win32-status AS scWin32Status" ` | |
+ ", sc-bytes AS scBytes" ` | |
+ ", cs-bytes AS csBytes" ` | |
+ ", time-taken AS timeTaken" ` | |
+ " INTO IisLog" ` | |
+ " FROM $LogPath" | |
$connectionString = "Driver={SQL Server Native Client 11.0};Server=$DbServer;Database=$DbName;Trusted_Connection=yes;" | |
[string[]] $parameters = @() | |
$parameters += $query | |
$parameters += "-i:W3C" | |
$parameters += "-e:-1" | |
$parameters += "-o:SQL" | |
$parameters += "-createTable:ON" | |
$parameters += "-oConnString:`"$connectionString`"" | |
Write-Debug "Parameters: $parameters" | |
Write-Host ("[{0}] Importing $LogPath to $database.$dbserver ..." -f [DateTime]::Now) | |
$elapsed = [System.Diagnostics.Stopwatch]::StartNew() | |
## this is the important line | |
$result = . $logParser $parameters | |
$elapsed.Stop() | |
$recordCount = $result |% { if($_ -match "Elements output") { $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)[2] } } | |
Write-Host ("[{0}] Finished importing $LogPath to $DbName.$DbServer (Running Time: {1}, Records: {2})" -f [DateTime]::Now, $elapsed.Elapsed, $recordCount) | |
} | |
## run the program | |
$sites = dir -Path "\\$proxyserver\Logs\IIS\" -Directory | |
$todaysLog = ("u_ex{0}.log" -f [DateTime]::UtcNow.ToString("yyMMdd")) | |
$yesterdaysLog = ("u_ex{0}.log" -f [DateTime]::UtcNow.AddDays(-1).ToString("yyMMdd")) | |
foreach($site in $sites) { | |
if(Test-Path "$($site.FullName)\LogFiles") { | |
$w3Name = (dir -Path "$($site.FullName)\LogFiles" -Directory).Name | |
$logs = dir -Path "$($site.FullName)\LogFiles\$w3Name" -File -Filter *.log; | |
$logs = $logs |? { $_.Name -match $yesterdaysLog } | |
foreach($log in $logs) { | |
#Write-Host $log.FullName | |
Import-IisLogFilesToDb ` | |
-LogPath $log.FullName ` | |
-IisServer $server ` | |
-IisSiteName $site.Name ` | |
-DbServer $dbserver ` | |
-DbName $database | |
} | |
} | |
} | |
} finally { | |
foreach($er in $Error) { | |
$er | |
} | |
$end = [DateTime]::Now | |
Write-Host "End: $end" -ForegroundColor Yellow | |
Write-Host "Total: $($end - $start)" | |
$logoutput | Disable-LogFile | |
if(@($Error).Count -gt 0) { | |
# send an email to steven | |
$priority = [System.Net.Mail.MailPriority]::Normal; | |
if($environment -eq "prod") { $priority = [System.Net.Mail.MailPriority]::High } | |
$messageParams = @{ | |
Subject = "[Error] $name Error ($environment, $($env:COMPUTERNAME))"; | |
Priority = $priority; | |
BodyAsHtml = $true; | |
Body = "<pre>`n" + ((Get-Content $logoutput.Path) -join "`n") + "`n</pre>"; | |
From = "noreply@sa.ucsb.edu"; | |
To = @("Your Name <your.name@your.domain.com>") | |
SmtpServer = "smtp.your.domain.com"; | |
}; | |
Send-MailMessage @messageParams; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment