1 Answer
- Newest
- Most votes
- Most comments
1
Hi rePost-User-7158120,
I understand that you are trying to copy files from your on-premises storage to your AWS S3 bucket using a PowerShell Script and would like to filter for files that are over 60 days old from the current day.
AWS has a PowerShell module with AWS Tools that enable you to script operations on your AWS resources from the PowerShell command line (https://aws.amazon.com/powershell/). You can download from the website or install by running Install-Module -Name AWSPowerShell if you are running PowerShell 5 or later.
For your specific case, I have included a sample script that may fit what you are trying to accomplish. Please test it before using in your production environment.
Import-Module AWSPowerShell
#---Define current date---#
$Current = Get-Date
#---Define number of days---#
$Days = 60
#---Define source folder---#
$CopyFrom = ‘C:\Backupsource\’
#---Define destination folder---#
$CopyTo = ‘C:\Destination’
#---Define extension---#
$LastWrite = ($Current).AddDays(-$Days)
#---Define files to copy from source folder that are older than specified days---#
$Files = Get-ChildItem $CopyFrom -Recurse | Where-Object { $_.LastWriteTime -le "$LastWrite"}
#---Define number of files copied/not copied---#
$FilesCopied = 0
$FilesNotCopied = 0
#---Copy each file---#
foreach ($File in $Files)
{
#---Define file exists---#
$FileExists = Test-Path -Path $CopyTo/$File
#---Determine if the file is not null and does not already exist---#
if (($File -ne $NULL) -and ($FileExists -eq $False))
{
#---Copy the file to the destination folder---#
aws s3 cp $File.FullName $CopyTo
#---Alternate option: Copy-Item $File.FullName $CopyTo---#
#---Add to number of files copied---#
$FilesCopied += 1
}
else
{
#---Add to number of files not copied---#
$FilesNotCopied += 1
}
}
#---Display the number of files copied/not copied---#
Write-Host “Files copied:” $FilesCopied -ForegroundColor Green
Write-Host “Files not copied:” $FilesNotCopied -ForegroundColor Red
answered 2 years ago
Relevant content
- asked 8 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 6 months ago