Download Blob to Azure VM Using Custom Script Extension via PowerShell
Join the DZone community and get the full member experience.
Join For FreeVirtual Machines in Azure can be spun up in multiple ways — Azure Portal, PowerShell, or by deploying ARM templates. They are created to serve a purpose, and often, we come across the need to store files in the VM. This can be achieved in multiple ways — Disks, AzCopy, download from Blob, Github, or other URLs. Installing or copying these files post-VM-creation is a daunting task, as that requires you to RDP or SSH into the machine and then start executing commands.
With the help of Custom Script Extension, copying files post VM creation can be automated with a simple PowerShell script. All you have to do is point out the location to the PowerShell script stored as a Blob in one of the containers.
In this post, I would like to share the simple PowerShell script that can be used as a Custom Script Extension.
xxxxxxxxxx
# Install the packages required
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module Az.Storage -Force
# Storage account name and Container name
$StorageAccountName = "Your-Storage-Account-Name"
$ContainerName = "Your-Storage-Account-Container-Name"
# Give the connection string.
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=Your-Storage-Account-Name;AccountKey=Your-Storage-Account-Key"
$Ctx = New-AzStorageContext -ConnectionString $ConnectionString
#Download File
$FileName1 = "ABC.txt"
$FileName2 = "XYZ.txt"
#Destination Path
$localTargetDirectory = "C:\"
#Download Blob to the Destination Path
Get-AzStorageBlobContent -Blob $FileName1 -Container $ContainerName -Destination $localTargetDirectory -Context $ctx
Get-AzStorageBlobContent -Blob $FileName2 -Container $ContainerName -Destination $localTargetDirectory -Context $ctx
Open file in GitHub.
For more Azure snippets, follow this GitHub Repo — Azure-Snippets
Opinions expressed by DZone contributors are their own.
Comments