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.


 

Leave a Reply

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