Skip to content

Instantly share code, notes, and snippets.

@hidori
Created August 19, 2012 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidori/3394634 to your computer and use it in GitHub Desktop.
Save hidori/3394634 to your computer and use it in GitHub Desktop.
Get-IisLogFile.ps1
Param(
[Parameter(Mandatory=$True,Position=0)]
[String[]]
$Path,
[Switch]
$Recurse,
[Switch]
$Utc,
[DateTime]
$After,
[DateTime]
$Before)
if ($After -ne $Null)
{
if ($Utc)
{
$DateTime = [DateTime]::Parse($After.ToString())
}
else
{
$DateTime = [DateTime]::Parse($After.ToString()).ToUniversalTime()
}
$AfterYmd = [DateTime]::Parse($DateTime.ToString("yyyy-MM-dd 00:00"))
$AfterYmdH = [DateTime]::Parse($DateTime.ToString("yyyy-MM-dd HH:00"))
}
if ($Before -ne $Null)
{
if ($Utc)
{
$DateTime = [DateTime]::Parse($Before.ToString())
}
else
{
$DateTime = [DateTime]::Parse($Before.ToString()).ToUniversalTime()
}
$BeforeYmd = [DateTime]::Parse($DateTime.ToString("yyyy-MM-dd 00:00"))
$BeforeYmdH = [DateTime]::Parse($DateTime.ToString("yyyy-MM-dd HH:00"))
if (($DateTime.Hour -gt 0) -or ($DateTime.Minute -gt 0))
{
$BeforeYmd = $BeforeYmd.AddDays(1)
}
if ($DateTime.Minute -gt 0)
{
$BeforeYmdH = $BeforeYmdH.AddHours(1)
}
}
Get-Item -Path $Path |
%{
if ($_.PSIsContainer)
{
if ($Recurse)
{
Get-ChildItem -Recurse -Path $_ | ?{ -not $_.PSIsContainer } | %{ $_.FullName }
}
else
{
Get-ChildItem -Path $_ | ?{ -not $_.PSIsContainer } | %{ $_.FullName }
}
}
else
{
$_.FullName
}
} |
%{
$Match = [Regex]::Match($_, "\\u_ex(?<Year>\d{2})(?<Month>\d{2})(?<Day>\d{2})(?<Hour>\d{2})?\.log$");
if ($Match.Success)
{
$Year = $Match.Groups["Year"].Value
$Month = $Match.Groups["Month"].Value
$Day = $Match.Groups["Day"].Value
if ([String]::IsNullOrEmpty($Match.Groups["Hour"].Value))
{
$FileDateTime = [DateTime]::Parse($("{0}{1}-{2}-{3}" -f [Int32]($AfterYmd.Year / 100), $Year, $Month, $Day))
$AfterDateTime = $AfterYmd
$BeforeDateTime = $BeforeYmd
}
else
{
$FileDateTime = [DateTime]::Parse($("{0}{1}-{2}-{3} {4}:00" -f [Int32]($AfterYmdH.Year / 100), $Year, $Month, $Day, $($Match.Groups["Hour"].Value)))
$AfterDateTime = $AfterYmdH
$BeforeDateTime = $BeforeYmdH
}
if (($After -ne $Null) -and ($Before -ne $Null))
{
if (($AfterDateTime -le $FileDateTime) -and ($FileDateTime -lt $BeforeDateTime))
{
$_
}
}
elseif ($After -ne $Null)
{
if ($AfterDateTime -le $FileDateTime)
{
$_
}
}
elseif ($Before -ne $Null)
{
if ($FileDateTime -lt $BeforeDateTime)
{
$_
}
}
else
{
$_
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment