How To Copy Files From On-Prem To Amazon S3 With PowerShell Scripts with over 60 days

0

How To Copy Files From On-Prem To Amazon S3 With PowerShell Scripts with over 60 days is there any way to filter the data from on-prem to copy files to S3 which are over 60 days old from the current day

1 Answer
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
Kegan_S
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions