Friday, April 7, 2017

Mount an Azure File Share using PowerShell

I'll start this blog post with the exact use-case I went through. I was deploying a SharePoint environment in the Cloud. The requirement was to deploy a staging environment and the production environment where each environment has 7 servers deployed with SharePoint. To make the deployments process easier, I had to download all the necessary binaries in to one location which can be accessible from all servers. This includes all the other supporting tools. So basically a shared drive for all machines.

The good thing with Azure file Share is, its not limited to Cloud when sharing or accessing. Once the storage account is created which includes a file share, it can be accessed from machines/VMs in cloud as well as machines in on-premises.

I named my file share as fscommon. Well, as a practice I used to as a prefix for all the resources I create.
Prior mounting the file share, you need to make sure you copy your storage account key.

You can select one of the keys given by azure. You can replace the existing keys by regenerating new keys. Below PowerShell script can be used to attached your Azure File Share to your machine. Make sure to replace the storage account key, storage account name, Drive letter and the share name with your own values.

 $acctKey = ConvertTo-SecureString -String "<storage-account-key>" -AsPlainText -Force  
 $credential = New-Object System.Management.Automation.PSCredential -ArgumentList "Azure\<storage-account-name>", $acctKey  
 New-PSDrive -Name <desired-drive-letter> -PSProvider FileSystem -Root "\\<storage-account-name>.file.core.windows.net\<share-name>" -Credential $credential  


But shared drive will be mounted till you restart your machine. If you need to mount the drive permanently (till you unmount), use the -persist parameter with the New-SPDrive cmdlet.

Happy days :)