Skip to content

Instantly share code, notes, and snippets.

@damirarh
Created October 4, 2015 14:05
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 damirarh/9f9b2f636557b54338ab to your computer and use it in GitHub Desktop.
Save damirarh/9f9b2f636557b54338ab to your computer and use it in GitHub Desktop.
Script for downloading IIS logs from Azure and updating AWStats
# FTP details
$ftpServer = ''
$ftpUsername = ''
$ftpPassword = ''
# local paths
$perlPath = 'C:\Perl\bin\perl.exe'
$awstatsPath = 'C:\WWW\Internal\awstats-7.3\wwwroot\cgi-bin\awstats.pl'
$downloadDir = 'C:\WWW\_Logs\Azure'
# common credentials for FTP access
$credentials = New-Object System.Net.NetworkCredential($ftpUsername, $ftpPassword)
# get list of files
[System.Net.FtpWebRequest]$ftp = [System.Net.WebRequest]::Create($ftpServer)
$ftp.Credentials = $credentials
$ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
$response = $ftp.GetResponse().GetResponseStream();
[System.IO.StreamReader]$reader = New-Object System.IO.StreamReader($response)
# download files
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = $credentials
$localFiles = @()
while (($file = $reader.ReadLine()) -ne $null)
{
$localFile = $file.Substring(7) # strip file prefix (1d66b1-201509260745.log -> 201509260745.log)
$localFiles += $localFile
$localPath = Join-Path $downloadDir $localFile
"Downloading $file"
$webClient.DownloadFile($ftpServer + $file, $localPath)
"Downloaded to $localPath"
}
# sort files to put the latest (incomplete) one at the end
$localFiles = $localFiles | sort
# process all but the latest file
for ($i = 0; $i -lt $localFiles.Length - 1; $i++)
{
$localPath = Join-Path $downloadDir $localFiles[$i]
$cmd = "$perlPath $awstatsPath -config=damirscorner -LogFile=$localPath"
"Processing $localPath"
$cmd
Invoke-Expression $cmd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment