DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Hands-On with Azure Local via the Azure Portal
  • Effective Secrets Management: Retrieving Secrets From Azure Key Vault With Powershell Script
  • Power of Azure B Series Virtual Machines
  • Azure Virtual Machines

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Idempotent Consumers: Preventing Stale Retries With Version-Fenced Writes
  • Choosing a BBD Framework
  • Introducing TypeSafe Jev: Typed Judgments as Programming Primitives

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Download Blob to Azure VM Using Custom Script Extension via PowerShell

Download Blob to Azure VM Using Custom Script Extension via PowerShell

By 
Mourya Chigurupati
·
May. 07, 20 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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


Open file in GitHub.

For more Azure snippets, follow this GitHub Repo — Azure-Snippets

Virtual Machine PowerShell azure Download

Opinions expressed by DZone contributors are their own.

Related

  • Hands-On with Azure Local via the Azure Portal
  • Effective Secrets Management: Retrieving Secrets From Azure Key Vault With Powershell Script
  • Power of Azure B Series Virtual Machines
  • Azure Virtual Machines

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook