Azure File Service offers managed file shares in the cloud that are accessible via the standard Server Message Block (SMB) protocol. This allows you to create one or more files shared in the cloud and use the share as a normal Windows file server. In this post, I want to show you how to create an Azure file share using PowerShell and the Azure CLI.

Common use cases for the Azure file share are:

Replace or supplement local file servers.

Repository for the application logs.

Use Azure File Sync to Replicate data on-premises and the Azure File Service.

Prerequisites

This tutorial assumes that you already have a Microsoft Azure account configured.

You created a Resource Group for these resources and the new ones deployed in this tutorial will join that group. If you want to know how to create a Resource Group, check out this link.

You can use an existing Storage Account, or you can create a new one. If you want to know how to create a Storage Account using PowerShell, check out this link.

Azure PowerShell Workaround

If you want to know how to install the PowerShell Azure module on your machine, check out this link.

The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount 1 2 3 Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account.

If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Get-AzSubscription Select-AzSubscription -Subscription "My Subscription" 1 2 3 4 Get-AzSubscription Select -AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you’re ready to start.

Set the variables

Here, we define the characteristics of our environment and the resource’s properties.

$resourceGroupName = "RG-DEMO-NE" $storageAccountName = "stoaccountps" $storageKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0] 1 2 3 4 5 $resourceGroupName = "RG-DEMO-NE" $storageAccountName = "stoaccountps" $storageKey = ( Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName ) . Value [ 0 ]

To create a share, you should first create an Azure Storage context object using the AzStorageContext cmdlet. This cmdlet requires the name of the storage key and the access key for the storage account.

$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName ` -StorageAccountKey $storageKey 1 2 3 4 $storageContext = New-AzStorageContext -StorageAccountName $storageAccountName ` -StorageAccountKey $storageKey

and then you must set a name for the share and pass the context object as a parameter in the New-AzureStorageShare cmdlet.

$shareName = "logsps" New-AzStorageShare -Name $shareName ` -Context $storageContext 1 2 3 4 5 $shareName = "logsps" New-AzStorageShare -Name $shareName ` -Context $storageContext

Azure CLI Workaround

In this case, we will use Azure Cloud Shell, a browser-based shell built into Azure Portal. This allows us to use the Azure command-line tools (Azure CLI and Azure PowerShell) directly from a browser. If you want to know more about Azure Cloud Shell, check out this link

To create an Azure File Share using Azure CLI, you should first retrieve the connection string using the az show-connection-string command.

storage_conn_string=$(az storage account show-connection-string -n stoaccountcli -g RG-DEMO-NE --query 'connectionString' -o tsv) 1 2 3 storage_conn_string = $ ( az storage account show - connection - string - n stoaccountcli - g RG - DEMO - NE -- query 'connectionString' - o tsv )

-n: storage account name.

-g: resource group.

-o : output format.

and then pass the value to the az storage share create command.

az storage share create --name logscli --quota 5120 --connection-string $storage_conn_string 1 2 3 az storage share create -- name logscli -- quota 5120 -- connection - string $storage_conn_string

–name: File Share name.

–quota: the storage capacity.

In the next post, I will show you how to connect and mount an Azure File Share from Windows.

If you want to know more about Azure File services, check out this link: https://docs.microsoft.com/en-us/azure/storage/files/storage-files-introduction

Was this article useful? Support my work!