alcor8
alcor8

Reputation: 373

Azure Block Blob PowerShell Script Creating 0 B files

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?

Upvotes: 0

Views: 195

Answers (1)

alcor8
alcor8

Reputation: 373

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

Upvotes: 0

Related Questions