Friday 29 January 2016

PowerShell script for extracting IIS log details for specific SharePoint webapplication

Import-Module sqlps -DisableNameChecking

#Database server name
$serverName="xxxxx"
#IIS Logging database name
$databaseName="IISLoggingDB"
#Path of the file LogParser.exe
$LogParserPath="C:\Program Files (x86)\Log Parser 2.2"
#Siteid (taken from IIS server)
$SiteId="W3SVC17xxxxx"
#Path of IIS Log files
$IISLogPath="C:\inetpub\logs\LogFiles\$SiteId"
#IP Address of WFE server
$ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1}
$IPAddress=$ip.ipaddress[0]

function Main {
param
(
    [parameter(mandatory=$true)][string]$serverName,
    [parameter(mandatory=$true)][string]$databaseName        
)

 $SQLCmd="SELECT count(*) as TotalCount FROM $SiteId";
 #Invoking SQL commnds
 $IsRecordExists=Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName  -Query $SQLCmd -ErrorAction SilentlyContinue

 if($IsRecordExists.TotalCount -gt 0)
 {
    #Get last execution date  
    $SQLCmd="SELECT TOP 1 date FROM $SiteId WHERE sIp='$IPAddress' ORDER BY date DESC";
    #Invoking SQL commnds
    $LastExecutionDate=Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName  -Query $SQLCmd
    if($LastExecutionDate -ne $null)
    {
    $LastExecutionDate = get-date $LastExecutionDate.date -Format 'yyyy-MM-dd'

    #Get last execution time  
    $SQLCmd="SELECT TOP 1 time FROM $SiteId  where date='"+$LastExecutionDate+"' ORDER BY time DESC";
    #Invoking SQL commnds
    $LastExecutionTime=Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName  -Query $SQLCmd
    $LastExecutionTime = get-date $LastExecutionTime.time.AddSeconds(1) -Format 'HH:mm:ss' 
    #Last execution date time
    $LastExecutionDateTime = get-date($LastExecutionDate +" "+ $LastExecutionTime) -Format 'yyyy-MM-dd HH:mm:ss'
    $Todaydate=Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
    #Locate the logparser path
    cd $LogParserPath

    if($LastExecutionDateTime -ne $null)
       {
           #Execute the logparser.exe
           #This will create a table and insert the IIS log deatils where the file name is having 'aspx' 
           .\logparser.exe "SELECT * INTO $SiteId FROM $IISLogPath\*.log WHERE (TO_TIMESTAMP(date, time) BETWEEN TIMESTAMP('$LastExecutionDateTime', 'yyyy-MM-dd HH:mm:ss') AND TIMESTAMP('$Todaydate', 'yyyy-MM-dd HH:mm:ss')) and (EXTRACT_FILENAME(cs-uri-stem) LIKE '%aspx%') and s-ip='$IPAddress'" -i:iisw3c -o:SQL -server:$serverName -database:$databaseName -createTable: ON
       }
    }
    else
    {
      #Locate the logparser path
      cd $LogParserPath
      #Execute the logparser.exe
      #This will create a table and insert the IIS log deatils where the file name is having 'aspx'
      .\logparser.exe "SELECT * INTO $SiteId FROM $IISLogPath\*.log WHERE EXTRACT_FILENAME(cs-uri-stem) LIKE '%aspx%' and s-ip='$IPAddress'" -i:iisw3c -o:SQL -server:$serverName -database:$databaseName -createTable: ON
    }
   
 }
 else
 {
     #Locate the logparser path
     cd $LogParserPath
     #Execute the logparser.exe
     #This will create a table and insert the IIS log deatils where the file name is having 'aspx'
     .\logparser.exe "SELECT * INTO $SiteId FROM $IISLogPath\*.log WHERE EXTRACT_FILENAME(cs-uri-stem) LIKE '%aspx%' and s-ip='$IPAddress'" -i:iisw3c -o:SQL -server:$serverName -database:$databaseName -createTable: ON
 }
}

Main $serverName $databaseName

No comments:

Post a Comment