Since iTunes doesn't really provide much in the way of logging I am looking through our IIS Logs and trying to pull out specific requests for mp3/mp4 files or podcast requests as a way of at least determining the number of requests we are getting for specific audio broadcasts. I came up with this script and while it works, it feels like I could probably do some of the parsing a little better. Are there some simpler ways to improve on this?
#########################################
# IIS Filter variables
$iisRawData = @()
$logMatchData = @()
$pattern = "(podcast|mp3\s+)"
$reportFile = "reportOutput.csv"
$podcastFile = "reportPodcast.csv"
[string]$siteName = "IIS:\Sites\MAIN"
#########################################
# Get the Year, Month, and Current Week for the File Name
$reportDate = get-Date -uformat %Y%m-%W
$reportFile = $reportDate + $reportFile
$podcastFile = $reportDate + $podcastFile
#########################################
# Get the log files from the past week
$logs = Get-ItemProperty $siteName -name logFile.directory
$iisRawData = Get-ChildItem -Path $logs.Value -Filter *.log -Recurse | `
? { $_.LastWriteTime -gt (Get-Date).AddDays(-7) } | `
Select-String $pattern
# Add headers to the data file
"Date,Time,URL,UserAgent,IP,Host`n" >> $reportFile
# Prepare to get the entries
foreach ($_ in $iisRawData) {
# Need to convert to a String from MatchInfo
$temp = ($_.Line).ToString()
# Split line to be able to only get the fields needed
$data = $temp.split(" ")
[string]$logDate = $data[0]
[string]$logTime = $data[1]
[string]$logUri = $data[6]
[string]$logAgent = $data[12]
[string]$logHost = $data[13]
# Add to the Matches
if ($logUri -eq $null -or $logUri -eq "") {
# Then we don't want to record it
} elseif ($logUri -match "podcast") {
# Add to the podcast report file
$logDate + "," + $logTime + "," + $logUri + "," + $logAgent + "," + $logHost + "`n" >> $podcastFile
} else {
# Add the matched data to the report file
$logDate + "," + $logTime + "," + $logUri + "," + $logAgent + "," + $logHost + "`n" >> $reportFile
}
}