1

Here's what I have now:

Write-S3Object `
   -BucketName "user-staging" `
   -Key  "app/access/partials/webapi.html" `
   -File "app/access/partials/webapi.html" `
   -HeaderCollection @{"Cache-Control" = "public,max-age=600"} 
Write-S3Object `
   -BucketName "user-staging" `
   -Key  "app/auth/partials/agreement_policy.html" `
   -File "app/auth/partials/agreement_policy.html" `
   -HeaderCollection @{"Cache-Control" = "public,max-age=600"}

I would like to publish some but not all of the files in the partials directory. To do this right now I am listing every file one by one.

I know I can use this code:

   Get-ChildItem . -Recurse | Foreach-Object{

but that would list all files in the directory.

Is there some way I can put the file names in an array and do the Write-S3Object for each element of the array?

  • `$array | foreach { Write-S3Object -File $_ ...; etc. }` – Ryan Bemrose Apr 09 '16 at 04:55
  • @RyanBemrose - Could you give an example of how I could populate the array with the file names as an answer so I can accept. Thanks –  Apr 09 '16 at 05:03
  • If not all files in the directory, how do you choose what filenames you want in the array? Is it manual? – Ryan Bemrose Apr 09 '16 at 05:11
  • Yes it's manual. I just need to fill an array somehow with 7-8 different filenames and I'm okay to type that in. A lot better than 7-8 different write blocks which I have now. –  Apr 09 '16 at 05:12

1 Answers1

0

Foreach-Object was made for problems like this.

$array = @()
$array += "path/file1"
$array += "path/file2"
$array += "path/file3"

$array | foreach {
  Write-S3Object `
     -BucketName "user-staging" `
     -Key  $_ `
     -File $_ `
     -HeaderCollection @{"Cache-Control" = "public,max-age=600"} 
}

A more compact form to create the array is simply $array = @("path/file1", "path/file2", etc)

You can also populate the array by assigning it the output of a filtered Get-ChildItem

$array = gci '*.txt' -exclude 'not-this-one.*'
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54