This post shows how to create an SMB file share using Azure storage, that you can then use as a mounted drive on a remote server (such as your on-premise SQL Server machine, or a VM), step-by-step.
You will also need the Azure Powershell Az module (introduced in December 2018 to replace the AzureRm Powershell module), which can be installed using these instructions.
Connect to Azure via Powershell
First, open Powershell and create a connection to your Azure account with the “Connect-AzAccount”. You can do this from the remote server that you want to present the file share to, if you want to carry out this whole exercise from a single Powershell session.
Unless you are persisting your credentials across Powershell sessions, the Connect-AzAccount command will ask you to authenticate your connection via a web browser. After you’ve done this, you should be connected to your Azure account from Powershell.
Create a Resource Group
Create a new Resource Group to hold the storage account that will be used for the file share, if you don’t have one already. You’ll need to specify a name for the Resource Group, and a region in which you would like it to be created.
New-AzResourceGroup -Name YourResourceGroupName -Location "NameOfAnAzureRegion"
To see all of the existing Resource Groups within your Azure account, use “Get-AzResourceGroup”.
Create a Storage Account
Next, add a Storage Account to the Resource Group.
New-AzStorageAccount -ResourceGroupName NameOfResourceGroupToUse -Name "NameOfNewStorageAccount" -Location "NameOfAnAzureRegion" -SkuName ReplicationOption -Kind StorageAccountType
Your storage account name can only consist of numbers and lower-case letters, be between 3 and 24 characters in length, and will need to be unique across Azure.
When the new Storage Account has been created, a couple of Account Keys will be automatically created too. You’ll need to grab one of these keys to use later. The key can be retrieved and put into a variable with a command like:
$AccountKey = (Get-AzStorageAccountKey -ResourceGroupName "NameOfResourceGroupToUse" -AccountName "NameOfNewStorageAccount").Value
You now need to set a context that references the new Storage Account location, and provides the key to access it. This can be done using the Powershell command below. The “StorageAccountKey” will be provided by the variable you created above.
$Context = New-AzStorageContext -StorageAccountName "NameOfNewStorageAccount" -StorageAccountKey $AccountKey
Create a File Share
The $context variable that you have just set will then be used in the “New-AzStorageShare” command, when you actually create a new file share, in the storage account you created previously.
New-AzStorageShare -Name "NameOfNewFileShare" -Context $Context
The file share has now been created, and you can see it through the Azure Portal, as below.
The “New-AzStorageShare” command doesn’t give you the option to set the size of the share (a default value is used), but you can alter the size of the share with “Set-AzStorageQuota”.
Set-AzStorageShareQuota -ShareName "NameOfNewFileShare" -Context $Context -Quota 1024 #New Size Of File Share In MB
Mounting the File Share as a drive on a remote computer
Now we come to the final part of the process – mounting the new file share as a drive on a remote computer.
The first stage of this is to provide the credentials that the mapped drive will need to connect to the file share. This will be a “user name” of “Azure/NameOfNewFileShare”, and the account key you used previously to set the $AccountKey variable, converted to a secure string:
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList "Azure\NameOfNewFileShare", (ConvertTo-SecureString -String $AccountKey -AsPlainText -Force)
Finally, the actual mapping of the new file share to a drive can be done by the command below. The “-Name” arguments lets you specify a drive letter for the newly mapped drive (I’ve used “F” below), and then you just need to substitute in the names of the Storage Account and File Share you created previously.
New-PSDrive -Name F -PSProvider FileSystem -Root "\\NameOfStorageAccount.file.core.windows.net\NameOfNewFileShare" -Credential $credential -Persist
The file share should now be visible as a mounted drive on your remote computer.
You can now copy files to your new drive…
…and they’ll be available within your Azure Storage Account:
PowerShell script to create the File Share and map as a drive
I’ve condensed the whole process described above into the one PowerShell script below. Just connect to Azure, set the variables at the start of the script to the names, sizes, locations, etc that you want to use, and the rest of the script will deploy the mapped file share.
To carry out this process using just the UI, and not touching Powershell, see this alternative guide.
If you would like to hire Data Minister to carry out this work for you, or find out more about our services, please contact us at email@example.com