0

I' am trying to put together a PowerShell script that will allow me to upload files to my AWS S3 Bucket based on the last modified date of a folder.

This is what i have thus far:

using namespace System.IO;
Set-AWSCredentials -StoredCredentials MyCredentialsAws
Set-DefaultAWSRegion us-east-1

[String] $root = "C:\Users\Administrator\Documents\TestFolder";

[DateTime]$today = [DateTime]::Now.Date;

[FileSystemInfo[]]$folderList = Get-ChildItem -Path $root -Directory;
foreach( $folder in $folderList ) {

    if( $folder.LastWriteTime -lt $today ) {
        [String] $folderPath = $folder.FullName;
        aws s3 cp $folder s3://bucketname/$folder --recursive 
   }
}

However, the above gives me the error:

"The User-provided path does not exist"

Any help would be appreciated.

HAL9256
  • 12,384
  • 1
  • 34
  • 46
TheManBehindTheMan
  • 31
  • 1
  • 3
  • 10

1 Answers1

0

For one thing, you used $folder as the source in the aws commandline when you probably intended to use $folderPath. Also, the expansion of Get-ChildItem is not consistent. Depending on the context the objects are sometimes expanded to just the name, and sometime to the full path (see here for a more detailed explanation). Because of that it's considered good practice to explicitly use the property needed for the given scenario (Name, FullName, ...).

if ($folder.LastWriteTime -lt $today) {
    $folderPath = $folder.FullName
    $folderName = $folder.Name
    aws s3 cp $folderPath s3://bucketname/$folderName --recursive 
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328