{{announcement.body}}
{{announcement.title}}

Download Blob to Azure VM using Custom Script Extension via PowerShell

DZone 's Guide to

Download Blob to Azure VM using Custom Script Extension via PowerShell

In this post, we share a simple Powershell script that can be used as a Custom Script Extension to copy and install files in an Azure VM post-creation.

· DevOps Zone ·
Free Resource

Virtual 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.


Shell
 




xxxxxxxxxx
1
22


 
1
# Install the packages required
2
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
3
Install-Module Az.Storage -Force
4
 
          
5
# Storage account name and Container name
6
$StorageAccountName = "Your-Storage-Account-Name"
7
$ContainerName = "Your-Storage-Account-Container-Name"
8
 
          
9
# Give the connection string.
10
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=Your-Storage-Account-Name;AccountKey=Your-Storage-Account-Key"
11
$Ctx = New-AzStorageContext -ConnectionString $ConnectionString
12
 
          
13
#Download File
14
$FileName1 = "ABC.txt"
15
$FileName2 = "XYZ.txt"
16
 
          
17
#Destination Path
18
$localTargetDirectory = "C:\"
19
 
          
20
#Download Blob to the Destination Path
21
Get-AzStorageBlobContent -Blob $FileName1 -Container $ContainerName -Destination $localTargetDirectory -Context $ctx
22
Get-AzStorageBlobContent -Blob $FileName2 -Container $ContainerName -Destination $localTargetDirectory -Context $ctx



Topics:
azure ,azure storage ,blob ,cloud ,powershell ,script

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}