Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Read IIS Logs for ActiveSync

$
0
0

Hi

I have the following powershell script which will read activesync IIS logs and then provide a csv file with the http error codes in a list.

But I would like a way for the content of the CSV file to be extracted and inserted into a CSV file.

 

Param(
    [parameter(Mandatory=$false)]
    [Bool]$AutoOpen,
 [parameter(Mandatory=$false)]
    [String]$OutFile,
 [parameter(Mandatory=$false)]
    [Bool]$IgnoreInParams,
 [parameter(Mandatory=$false)]
    [Bool]$IgnoreOutParams)
 
$Error.Clear()
$DefaultFolder=[Environment]::GetFolderPath("MyDocuments")
$Destination = "IIS- HTTP 503 Errors [CSV].csv"
$Destination = $DefaultFolder + "\" + $Destination

if($OutFile -ne [String]::Empty)
{
 $OutFileType = [System.IO.Path]::GetExtension($OutFile.ToUpper())
 $OriginalFileType = [System.IO.Path]::GetExtension($Destination.ToUpper())
 if($OutFileType -ne $OriginalFileType)
 {
  Write-Host "You have chosen" $OutFileType "as the output, but this script was originally generated as" $OriginalFileType -ForegroundColor Red
  Write-Host "Either change -OutFile to" $OriginalFileType "or generate the script again with the output as" $OutFileType  -ForegroundColor Red
  Write-Host "You can also modify the OutputFormat variable in this script to match the correct Log Parser 2.2 COM output format." -ForegroundColor Red
  [System.Environment]::NewLine
  return
 }
 else
 {
  if($true -ne $OutFile.Contains("\"))
  {
    $Destination = $DefaultFolder + "\" + $OutFile
  }
  else
  {
    $Destination = $OutFile
  }
 }
}

$LogQuery = New-Object -ComObject "MSUtil.LogQuery"
$InputFormat = New-Object -ComObject "MSUtil.LogQuery.IISW3CInputFormat"
$OutputFormat = New-Object -ComObject "MSUtil.LogQuery.CSVOutputFormat"

if($IgnoreInParams-eq $false){
     $InputFormat.iCodePage=0
     $InputFormat.recurse=-1
     $InputFormat.minDateMod="1900-01-01 12:00:00"
     $InputFormat.dirTime=0
     $InputFormat.consolidateLogs=1
     $InputFormat.useDirectiveDateTime=0
     $InputFormat.useDoubleQuotes=0
}

if($IgnoreOutParams -eq $false){
     $OutputFormat.Headers="AUTO"
     $OutputFormat.oDQuotes="AUTO"
     $OutputFormat.tabs=0
     $OutputFormat.oTsFormat="yyyy-MM-dd hh:mm:ss"
     $OutputFormat.oCodepage=0
     $OutputFormat.fileMode=1
}
Write-Progress -Activity "Executing query, please wait..."  -Status " "

$SQLQuery = "SELECT cs-username, sc-status, cs-uri-stem, Count(*) AS Total INTO '" + $Destination + "' FROM 'C:\IISLOGS\W3SVC1\ex14070104.log' Where sc-status = 503 Group By cs-username, sc-status, cs-uri-stem, cs-uri-query order by cs-username "
$rtnVal = $LogQuery.ExecuteBatch($SQLQuery, $InputFormat, $OutputFormat);
$OutputFormat = $null;
$InputFormat = $null;
$LogQuery = $null;
#Get-Content C:\Documents and Settings\adm_patelv\My Documents
if($AutoOpen)
{
 try
 {
  Start-Process($Destination)
 }
 catch
 {
  Write-Host $_.Exception.Message  -ForegroundColor Red
  Write-Host $_.Exception.GetType().FullName  -ForegroundColor Red
  Write-Host "NOTE: No output file will be created if the query returned zero records!"  -ForegroundColor Gray
  }
 
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles