Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.