Created
October 4, 2015 14:05
-
-
Save damirarh/9f9b2f636557b54338ab to your computer and use it in GitHub Desktop.
Script for downloading IIS logs from Azure and updating AWStats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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