Pages

Thursday, June 17, 2010

Log Retrieval Script

Today's project was to write a script that would retrieve log files using the HTTP protocol.  As I've said in my introductory post, I'm working on teaching myself PowerShell so this seemed like a great task for a script.  There are 24 log files per day so this script will get all 24 log files from the previous day. Here is the code:
# Log Retrevial Script
# Script Constants
$BaseURL = "http://www.example.com/"
$BaseFilePrefix = "fileprefix_"
$Yesterday = Get-Date -format yyyyMMdd ((Get-Date).adddays(-1))
$HourArray = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
$BaseFileSuffix = "-0000.log.gz"
$SavePath = "C:\Log_Downloads\"

$Client = New-Object System.Net.WebClient
Foreach ($Hour in $HourArray)
 {
   if ($Hour -lt 10)
   {
    $File = $BaseFilePrefix + $Yesterday + "0" + $Hour + BaseFileSuffix
   }
  else
   {
   $File = $BaseFilePrefix + $Yesterday + $Hour + $BaseFileSuffix
  }
  $URL = $BaseURL + $File
  $SaveFile = $SavePath + $File
  $Client.DownloadFile($URL, $SaveFile)
  Type $SaveFile
 }

No comments:

Post a Comment