Azure, PowerShell

Deploying an Azure File Share with PowerShell

On Twitter, I saw a post by Scott Cate, a Developer Advocate for Microsoft, showing how to set up Azure File Shares with the GUI. Azure File Shares allow administrators to build SMB file shares on Azure. These can then be mounted by any supported OS such as Windows, Linux or MacOS. Being a PowerShell guy, I decided to dive into the code and see what I could come up with.

So my work-in-progress code is listed here and I’ll break it down for you. Basically, I have the code to build a new Azure Storage Account and create a File Share in an existing Azure Resource Group.

$RSG = Read-Host "Enter Resource Group"
$Location = Read-Host "Enter Location"
$SKUName = Read-Host "Enter SKU"
$StorageAccount = Read-Host -Prompt "Enter Azure Storage Account Name"
$ShareName = Read-host -Prompt "Enter Share Name (all lower case)"

#create storage account based on input
$storageAcct = New-AzureRmStorageAccount `
                    -ResourceGroupName $RSG `
                    -AccountName $StorageAccount `
                    -Location $SKUName `
                    -SkuName $StorageAccount -Verbose

#Create Variable for storage key
$StorageKey = Get-AzureRmStorageAccountKey `
                    -ResourceGroupName $storageAcct.ResourceGroupName `
                    -Name $storageAcct.StorageAccountName -Verbose |
                         Where KeyName -eq 'key1' |
                          select -ExpandProperty value


#Create Storage Context for working with Storage Account
$storageContext = New-AzureStorageContext $StorageAccount $storagekey

#Create Share
$share = New-AzureStorageShare -Name $ShareName -Context $storageContext

Note: All of my code was run via Azure Shell, a web-based secure shell for managing Azure. If you haven’t checked it out, you definitely should.

First, I needed to build the storage account that my share would be part of. For this, I used the new-AzureRMStorageAccount command to build that out. Also, I created variables to be used for all the inputs.

#variables for script
$RSG = Read-Host "Enter Resource Group"
$Location = Read-Host "Enter Location"
$SKUName = Read-Host "Enter SKU"
$StorageAccount = Read-Host -Prompt "Enter Azure Storage Account Name"
$ShareName = Read-host -Prompt "Enter Share Name (all lower case)"

#create storage account based on input
$storageAcct = New-AzureRmStorageAccount `
                    -ResourceGroupName $RSG `
                    -AccountName $StorageAccount `
                    -Location $SKUName `
                    -SkuName $StorageAccount -Verbose

Once the storage account is created, the Azure Storage Account Key is stored in a variable for later use. Because there are two keys attached to each storage account (which you can re-key if needed) the first key, key1, is selected out.

$StorageKey = Get-AzureRmStorageAccountKey `
                    -ResourceGroupName $storageAcct.ResourceGroupName `
                    -Name $storageAcct.StorageAccountName -Verbose |
                         Where KeyName -eq 'key1' |
      
                    select -ExpandProperty value

With the key in hand, I create a variable for the storage context. The context encapsulates the storage account name and account key so I can work with my storage account.

#Create Storage Context for working with Storage Account
$storageContext = New-AzureStorageContext $StorageAccount $storagekey

Last, I’m going to create the share and verify the share was created.

#Create Share
$share = New-AzureStorageShare -Name $ShareName -Context $storageContext

As of this writing, the code is rudimentary so just copy it into Azure Shell to use. I plan to migrate the code to a parametized script that I’ll post on Github at a later date.
Was this helpful?

Let me know in the comments.


 

About Michael Bender

Just a geek trying to "share the wealth" with the rest of the world... Michael is a teacher at heart. Coming from a career teaching at a community college and work for 19 years as a system engineer, his passion is for helping people learn the skills necessary to ‘level-up’ their careers, and helping businesses achieve their customer goals. Sharing everything is how he guides his career as everyone wins when you share what you know. Currently, he is a Cloud Ops Advocate at Microsoft. This team focuses on engaging and advocating for Operations-focused communities to the engineering teams in Windows Azure. You’ll find Michael and his #AzOps team at conferences, online, and building great content at docs.com. Michael specializes in Windows Server, Virtualization, PowerShell and Windows Azure. As part of his goal of sharing his knowledge, he has a number of courses published on Pluralsight.com covering PowerShell and Windows Server. He is passionate about the IT Operations community. For the last 8 years, he has led up a global user group called The Krewe.he is the current president and a founding member of The Krewe, a worldwide networking group for IT Pros and Developers. This group provides a global network for the sharing of ideas and solutions for many in the Operations community. For more information, check out The Krewe on Facebook or Twitter.
View all posts by Michael Bender →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.