Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Dynamic Log File Upload

$
0
0

Hi Folks,

I have a powershell script that uploads log files created yesterday to AWS S3 storage, it then deletes all files older than 1 month.

The issue is there are new services added weekly so it is necessary to add a dynamic structure to look at the folder and dynamically react when a new file has been added.

The proposed new structure would be the script looks at the logfiles location:

$localPath = "C:\inetpub\logs\LogFiles\"

Creates a list of all the folders within this file structure and adds the folder name to the S3 bucket location:

$keyPrefix = "applicationlogs/IIS/<FOLDERNAME>/$timestampY/$timestampM/$timestampD/"

The script then runs through the list and processes each uploading their files to the S3 bucket

-------------------------------------------------------------------------

$timestampD = ((Get-Date).adddays(-1).toString("dd"))
$timestampM = ((Get-Date).adddays(-1)).toString("MMM")
$timestampY = ((Get-Date).adddays(-1).year)

$bucket = "<BUCKET NAME>"

$localPath = "C:\inetpub\logs\LogFiles\"

$keyPrefix = "applicationlogs/IIS/<DYNAMICFOLDERNAME>/$timestampY/$timestampM/$timestampD/"

Get-ChildItem $localPath | where { ($_.CreationTime -gt ((get-date).AddDays(-2))) -and ($_.CreationTime -lt ((get-date).AddDays(-1))) }
foreach ($f in Get-ChildItem $localPath  | where { ($_.CreationTime -gt ((get-date).AddDays(-2))) -and ($_.CreationTime -lt ((get-date).AddDays(-1))) }) 
{ $key="$keyPrefix$f"
 Write-S3Object -BucketName $bucket -File $f.FullName -Key $key -AccessKey $accessKey -SecretKey $secretKey -Region $region
}

-----------------------------------------------------------------------------------------------------------------------

Can I have some advice with the dynamic folder list creation?


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>