Created
January 29, 2015 22:07
-
-
Save KyleGobel/d0eb5bb0c02a659520c1 to your computer and use it in GitHub Desktop.
Zip and upload logs to S3 - Powershell
This file contains hidden or 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
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition | |
$logFile = Join-Path "C:\inetpub\logs\LogFiles\W3SVC2" "upload_logs.logFile" | |
Try | |
{ | |
$files = Get-ChildItem -Filter *.log | where-object {$_.lastwritetime -lt (Get-Date).AddHours(-1)} | |
$processedDir = Join-Path $scriptPath "uploaded_to_s3" | |
if ($files.count -gt 0) { | |
Write-Output "Starting Uploads to S3" | Out-File $logFile -Append | |
if ((Test-Path $processedDir) -eq 0) | |
{ | |
Write-Host "$processedDir does not exist, creating" | |
New-Item -ItemType Directory -Path $processedDir | |
} | |
Foreach ($f in $files) | |
{ | |
$logFile = "{0}_{1}" -f $env:Computername, $f | |
$keyName = Join-Path "tracker_iis_logs\email_tracker\logdrop" $logFile | |
Write-Host "Uploading file to $keyName" | |
Write-S3Object -BucketName mobi-logs -File $f -Key $keyName -CannedACLName Private | |
Write-Host "Moving file to processed directory" | |
$newPath = Join-Path $processedDir $f | |
Move-Item $f $newPath | |
} | |
} | |
else | |
{ | |
Write-Output "No items to process" | Out-File $logFile -Append | |
} | |
} | |
Catch | |
{ | |
$errorMsg = $_.Exception.Message | |
$failedItem = $_.Exception.ItemName | |
Write-Output "Error: $errorMsg, FailedItem: $failedItem" | Out-File $logFile -Append | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment