Each time the loop executes the file is being replaced in the place of the first file . I want to upload it as a new file without distrubing the existing ones..
foreach ($blob in $blobs)
{
$file=New-TemporaryFile
$file=Get-AzureStorageBlobContent -Container $container_name -Blob $blob.Name -Context $ctx -Destination $localFile -Force
$contents = Get-Content $localFile -Raw -ErrorAction:SilentlyContinue
$f=New-TemporaryFile
Add-Content $f $contents
$Header = @{
"Content-Disposition"="attachment;filename=$($blob.Name)"
"Authorization"=$accessToken
}
Invoke-RestMethod -Uri $apiUrl -Headers $Header -Method put -InFile $f
}
I think you over-did it with the New-TemporaryFile
commands in your code.
Why use Get-AzureStorageBlobContent
to store blob content in a file and then create another temp file to copy the contents to?
This would be much simpler:
foreach ($blob in $blobs) {
$localFile = New-TemporaryFile
Get-AzureStorageBlobContent -Container $container_name -Blob $blob.Name -Context $ctx -Destination $localFile.FullName -Force
$Header = @{
"Content-Disposition"="attachment;filename=$($blob.Name)"
"Authorization"=$accessToken
}
Invoke-RestMethod -Uri $apiUrl -Headers $Header -Method put -InFile $localFile.FullName
}
Edit
The New-TemporaryFile
command returns a System.IO.FileInfo
object which you cannot use directly as the InFile
parameter. Instead, it expects a full path and file name, so we use $localFile.FullName