Set Up an Azure File Share Used as a Mounted Drive
Setting up a place to store files in the cloud isn't quite as easy as doing it on-prem. That's where Microsoft's Azure Files service comes in.
- By Adam Bertram
The file share has been a ubiquitous presence in the IT world for a long time. Just about every Windows administrator out there has probably created a share on a server at one point and had to tell others a UNC path starting with "whack, whack."
In the cloud, though, setting up a place to store files isn't quite this easy because the protocol that a file share runs off of is the Server Message Block (SMB). SMB isn't a "cloud-native" protocol, but Microsoft has released a service called Azure Files to bring SMB to the cloud.
An Azure file share is just like a file share you're used to setting up on-prem and can be accessed just like an SMB share that's hosted on a local Windows machine.
A lot of the time, organizations choose to map a drive letter to an SMB share to provide resources like user home folders or various departmental folder shares. Or, scripters may want to use this share like a PowerShell drive as a pointer to upload and download files from.
In this article, let's see how we can create an Azure file share and learn how to create a PowerShell drive using PowerShell.
Azure File Share Setup
Assuming that you have an Azure subscription, the Azure PowerShell module installed and have authenticated with Connect-AzureRmAccount, you're ready to create the Azure file share. That requires a few different cmdlets. We'll first need to collect some information about the storage account we'd like to setup the Azure file share on.
I'm going to be creating the share on my storage account called "adbtestingdisk656." This storage account is in the adbtesting resource group. In the example below, I'm building a storage context based on the information returned from my storage account. The storage context is a required component to create the share.
You can see that I'm first getting the storage account information. This isn't technically required since all I need is the storage account resource group and its name for future commands. But this allows me to reference the name and resource group via the $storageAccount variable later on down the code.
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName adbtesting -Name adbtestingdisks656
$storageKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName | select -first 1).Value
$storageContext = New-AzureStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageKey
Once I have the storage context for my storage account, I can then create the share using the New-AzureStorageShare cmdlet, providing it with the desired name and the storage context.
$share = New-AzureStorageShare -Name myfileshare -Context $storageContext
Creating a PowerShell Drive
Once the share is created, you can access it like other SMB shares by using the UNC path \\adbtestingdisks656.file.core.windows.net\myfileshare. But we need to go one step further; we want to map this UNC path as a PowerShell drive. This isn't as straightforward as mapping an on-prem SMB share because we're not using Active Directory's single sign-on capability. Besides, it's not possible to connect to the share via a standard user name and password. This requires the Azure storage key we discovered earlier.
To authenticate to the share, we have to provide the name of the storage account as the user name and the storage key as the password, so I'll create a PSCredential object.
$secKey = ConvertTo-SecureString -String $storageKey -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList "Azure\$($storageAccount.StorageAccountName)", $secKey
When I create the object is when I can now create the PowerShell drive. By providing the UNC path as the value for Root, using the PSCredential object just created and using the optional Persist parameter, I can create the PowerShell drive as shown below.
$shareDrive = New-PSDrive -Name X -PSProvider FileSystem -Root "\\$($storageAccount.StorageAccountName).file.core.windows.net\$($share.Name)" -Credential $credential -Persist
Once this completes, you're done. In this instance, you'll also be able to reference all of the files in the Azure file share via the X drive.
About the Author
Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.