0

I'm trying replicate the answer in downloading-from-s3-with-aws-cli-using-filter-on-specific-prefix using PowerShell. I tried using the method in whats-the-equivalent-of-xargs-in-powershell, and have gotten close to getting the key name from S3 ls and passing it to a call command.

,@(Get-ChildItem -recurse | aws s3 ls s3://s3-bucket-name/year_2022/ABCDEFT-8D) | Select-Object -ExpandProperty syncroot |  Select-Object @{Name='KeyName';Expression={$_.split(" ")[-1]}} | %{&"aws" s3 cp s3://s3-bucket-name/year_2022/$_ .}

Although it iterates and gets all the key names in Select-Object but in foreach it ends up passing

s3://s3-bucket-name/year_2022/@{KeyName=ABCDEFT-8D-FILE1.XML}

s3://s3-bucket-name/year_2022/@{KeyName=ABCDEFT-8D-FILE2.XML}

And I get the key not found error. After searching around I tried replacing $_ with @_ but doing that passes the argument as s3://s3-bucket-name/year_2022/@_

I have no experience in powershell and made it this far by just searching existing answers, but I'm not able to figure this one out.

For reference, if I run the powershell code without the call

,@(Get-ChildItem -recurse | aws s3 ls s3://s3-bucket-name/year_2022/ABCDEFT-8D) | Select-Object -ExpandProperty syncroot |  Select-Object @{Name='KeyName';Expression={$_.split(" ")[-1]}}

I get this:

KeyName


ABCDEFT-8D-FILE1.XML

ABCDEFT-8D-FILE2.XML

...

From what I could figure out, the output above is a hashtable and so I thought that @_ should work, but doesn't.

The powershell version is 7.2.5

jvian
  • 63
  • 2
  • 6
  • I don't know aws. Does this example help? `get-childitem | foreach-object { remove-item $_ } ` The `$_` is what is piped in. – js2010 Jun 27 '22 at 14:36
  • Thanks, let me try it out. The first option removed all objects, hopefully the string works – jvian Jun 27 '22 at 15:44
  • Thank you @js2010 , this worked. Used the Foreach-Object to output a string. `@(Get-ChildItem -recurse | aws s3 ls s3://s3-bucket-name/year_2022/ABCDEFT-8D) | Select-Object -ExpandProperty syncroot | foreach-Object {$_.split(" ")[-1]} | %{&"aws" s3 cp s3://s3-bucket-name/year_2022/$_ .}` – jvian Jun 27 '22 at 15:51

1 Answers1

1

I think instead of select-object creating an object, you want foreach-object outputting a string:

# return the last word
'one two three' | Foreach-Object { $_.split(" ")[-1] }

three
js2010
  • 23,033
  • 6
  • 64
  • 66