# Copy Backup files to S3

**URL:** <https://forums.powershell.org/t/copy-backup-files-to-s3/22408>\
**Category:** PowerShell Help\
**Created:** [May 31, 2023, 9:44pm UTC](https://forums.powershell.org/t/copy-backup-files-to-s3/22408 "2023-05-31T21:44:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyanka](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@Priyanka](https://forums.powershell.org/u/Priyanka)\
**Post date:** [May 31, 2023, 9:44pm UTC](https://forums.powershell.org/t/copy-backup-files-to-s3/22408/1 "2023-05-31T21:44:43Z")

</div>

I somehow got this script working, but the write host command is displaying “no files to upload” message multiple times since its inside foreach loop and keeps iterating. I would like to display this message only once. Any help here would be much appreciated.

$Extensions = “.diff”,“.bak”,“.trn”  
$Today = (Get-date)  
$Bucket\_Name = (Invoke-Sqlcmd -Query “select Bucket\_Name from msdb.dbo.BackConfigInfo”).Bucket\_Name  
$AccessKey = (Invoke-Sqlcmd -Query “select AccessKey from msdb.dbo.BackConfigInfo”).AccessKey  
$Secret\_Key = (Invoke-Sqlcmd -Query “select Secret\_key from msdb.dbo.BackConfigInfo”).Secret\_key  
Set-AWSCredential -AccessKey $AccessKey -SecretKey $Secret\_Key -StoreAs default  
$HOSTNAME = $env:computername  
$HOSTNAME = $HOSTNAME + ‘\*\*\*\*\*\*\*\*’  
$ID = (Get-EC2Tag | Where-Object {$_.Key -eq ‘Hostname’ -and $_.Value -eq $HOSTNAME})  
$App = (Get-EC2Tag | Where-Object {$_.ResourceId -eq $ID.ResourceId -and $_.Key -eq ‘Application’})  
$tag = $App.Value  
$sourceloc = (Invoke-Sqlcmd -Query “EXEC master.dbo.xp\_instance\_regread ‘HKEY\_LOCAL\_MACHINE’, ‘Software\Microsoft\MSSQLServer\MSSQLServer’,‘BackupDirectory’”).Data

Get-ChildItem -Path $sourceloc -Recurse | Foreach-Object {  
$BackupFileOnDisk = $_.Name  
if(Get-S3Object -BucketName $Bucket\_Name | where{$_.Key -cmatch $BackupFileOnDisk})  
{  
Write-host “No files to Upload”  
}  
else  
{  
Write-host “$BackupFileOnDisk not in $Bucket\_Name”  
Write-S3Object -BucketName $Bucket\_Name $BackupFileOnDisk -Folder $sourceloc -KeyPrefix $HOSTNAME\Backup -TagSet @{Key=“Application”;Value=“$tag”} -ServerSideEncryption \*\*\*\*\*\* -Recurse  
}  
}

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [May 31, 2023, 10:35pm UTC](https://forums.powershell.org/t/copy-backup-files-to-s3/22408/2 "2023-05-31T22:35:00Z")

</div>

You have several places in your code where you have pipelining | where you are not using the pipeline variable denoted by $\_ and not just $ or $. like $.Key should be $\_.Key

See [https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about\_automatic\_variables?view=powershell-7.3](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3) for more info about automatic variables.

---

<div class="post-metadata">

**Author:** ![Priyanka](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@Priyanka](https://forums.powershell.org/u/Priyanka)\
**Post date:** [May 31, 2023, 11:01pm UTC](https://forums.powershell.org/t/copy-backup-files-to-s3/22408/3 "2023-05-31T23:01:24Z")

</div>

oops, my bad, somehow I missed the $\_.key, actually I am getting the output like this

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/9/92fd6e415cfaaaa30894b1e2761c0cecd3d9972b.png)

However I need this message to be printed only once. Any suggestions on this please

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [May 31, 2023, 11:06pm UTC](https://forums.powershell.org/t/copy-backup-files-to-s3/22408/4 "2023-05-31T23:06:56Z")

</div>

Either pull that code out of the loop or implement different logic on the trigger of that code path.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/copy-backup-files-to-s3/22408/5 "2024-05-16T20:14:53Z")

</div>

Closed on May 16, 2024, 8:14pm UTC


