Skip to content

Instantly share code, notes, and snippets.

@mastry
Last active May 28, 2019 01:29
Show Gist options
  • Save mastry/37cb6c315d9daf3db76939b3e2785b03 to your computer and use it in GitHub Desktop.
Save mastry/37cb6c315d9daf3db76939b3e2785b03 to your computer and use it in GitHub Desktop.
Retrieves the most recent IIS log file from a list of servers. Assumes the logs are in the default location (change first line of PROCESS section if needed).
[CmdletBinding()]
Param(
[Parameter(Position=0, ValueFromPipeline = $true, Mandatory = $true)]
[string[]] $ComputerName,
[switch] $Compress
)
BEGIN {
$targetFileList = @()
$tempFolder = [guid]::NewGuid().ToString()
$basePath = (pwd).Path
$tempFolder = "$($basePath)\$tempFolder"
mkdir $tempFolder
}
PROCESS{
$files = dir "\\$($ComputerName)\C$\inetpub\logs\LogFiles\W3SVC3"
$targetFile = ($files | Sort-Object -Property "LastWriteTime" -Descending)[0]
$targetFile.CopyTo("$($tempFolder)\$($ComputerName)_$($targetFile.Name)")
$targetFileList += $targetFile
}
END{
if( $Compress.IsPresent ) {
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::CreateFromDirectory("$($tempFolder)","$($tempFolder).zip")
del "$($tempFolder)\*.*"
rmdir $tempFolder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment