0

I'm new to Azure Blob Storage and I've written a PowerShell script for uploading SQL Server backup files from a given folder to my Azure Container. When I execute it a file is appearing in Azure but it's 0 Bytes in size. Here's my code:

#Finds newest .bak file in folder
$dir = "I:\Backup\*.bak"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1

#Get the File-Name without path
$name = (Get-Item $latest).Name

#The target URL wit SAS Token
$uri = "https://*****.blob.core.windows.net/sapprodback/$($name)?***My SAS Token is here***"

#Define required Headers
$headers = @{
'x-ms-blob-type' = 'BlockBlob'
}

#Upload File...
Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -InFile $latest

What am I missing?

2
  • 1
    where is $file defined? should it be $latest? Commented Jul 16, 2020 at 6:31
  • Good point. Let me update my code. $file should be $latest Commented Jul 16, 2020 at 6:52

1 Answer 1

0

I figured it out thanks for ewong's comment. My -InFile didn't have the full file path. I created another variable and concatenated it to my $latest.name and it worked.

#Finds newest .bak file in folder
$dir = "I:\Backup\*.bak"
$dir2 = "I:\Backup\"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1

#Get the File-Name without path
$name = (Get-Item $latest).Name
$file = $dir2 + $name

#The target URL wit SAS Token
$uri = "https://*****.blob.core.windows.net/sapprodback/$($name)?***My SAS Token is here***"

#Define required Headers
$headers = @{
'x-ms-blob-type' = 'BlockBlob'
}

#Upload File...
Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -InFile $file
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.