0

I need to run a script on all my tenants devices to upload a file into an Azure Container

I saw this Script PowerShell Basics: How to Upload Files to Azure Storage but i can't manage to make it work.

I create a SAS key, i tried both Account key and User Delegation key

Here's my code

#Variable
$FolderPath= "D:\BatteryReport"
 
#Check if Folder exists
If(!(Test-Path -Path $FolderPath))
{
    New-Item -ItemType Directory -Path $FolderPath
    Write-Host "New folder created successfully!" -f Green
}
Else
{
  Write-Host "Folder already exists!" -f Yellow
}


powercfg /batteryreport /output D:\BatteryReport\BatteryReport_$(hostname).html

Install-Module -Name Az -AllowClobber 

$StorageURL = "https://mytenant.blob.core.windows.net/pub"
$FileName = "BatteryReport_$(hostname).html"
$SASToken = "mySASkey"

$blobUploadParams = @{
    URI = "{0}/{1}?{2}" -f $StorageURL, $FileName, $SASToken
    Method = "PUT"
    Headers = @{
        'x-ms-blob-type' = "BlockBlob"
        'x-ms-blob-content-disposition' = "attachment; filename=`"{0}`"" -f $FileName
        'x-ms-meta-m1' = 'v1'
        'x-ms-meta-m2' = 'v2'
    }
    Body = $Content
    Infile = $FileToUpload
}
  • To upload a file to a block blob, get a container reference, then get a reference to the block blob in that container. Once you have the blob reference, you can upload data to it by using Set-AzStorageBlobContent https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-powershell#upload-blobs-to-the-container – Tony Apr 03 '23 at 18:59
  • but this method i need to connect an Azure account before, isn't there a way, being my container public, to upload content with just the SAS key? We will run this script for the whole company and it needs to run in the background with no user interaction – AndreSantos Apr 05 '23 at 14:28

1 Answers1

0

To send a file using the SAS Token you can use New-AzStorageContext with the -SasToken parameter and Set-AzStorageBlobContent
Example:

# Get Context and Sends using Connect AzAccount
#Connect-AzAccount
#$StorageAccount =  Get-AzStorageAccount YourResourceGroupName YourStorageAccountName
#$Context = $StorageAccount.Context

# Get Context and Sends using only SAS Token
$SASToken = "mySASkey"
$Context = New-AzStorageContext -StorageAccountName YourStorageAccountName -SasToken $SASToken

#$StorageURL = "https://mytenant.blob.core.windows.net/pub"

$ContainerName = "myfiles"
$FileName = "BatteryReport_$(hostname).html"

$Blob1HT = @{
  File             = 'C:\temp\$FileName' #'D:\Images\$FileName'
  Container        = $ContainerName
  Blob             = $FileName
  Context          = $Context
  StandardBlobTier = 'Hot'
}

Set-AzStorageBlobContent @Blob1HT

See also:
https://stackoverflow.com/a/69031205/194717

Docs:
https://learn.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-9.5.0
https://learn.microsoft.com/en-us/powershell/module/az.storage/new-azstoragecontext?view=azps-9.5.0#example-9-create-a-context-by-using-an-sas-token
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-powershell#upload-blobs-to-the-container

Tony
  • 16,527
  • 15
  • 80
  • 134
  • but this method i need to connect an Azure account before, isn't there a way, being my container public, to upload content with just the SAS key? We will run this script for the whole company and it needs to run in the background with no user interaction – AndreSantos Apr 05 '23 at 14:28
  • I updated the answer, check now if this helps. If not, tell me here in this comment section and I can delete my answer. – Tony Apr 05 '23 at 18:54