- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
Since an Azure file share needs a place to store your files, you'll need to have a storage account created ahead of time. Also, I'm assuming you have downloaded and installed the AzureRm PowerShell modules, and you're authenticated to Azure in your PowerShell console.
To get started, you'll first need to create an Azure storage context object. This object lets you pass the storage account name and key to create the file share. The command to do this is called New-AzureStorageContext, and it requires you to know the storage account name you'll be using for the file share and the storage account key. The name is straightforward, but if you don't know your key, you can use the Get-AzureRmStorageAccountKey command.
Below you can see that this command returns my primary and secondary keys. I just need the primary.
PS> Get-AzureRmStorageAccountKey -ResourceGroupName General -Name generaldiag805 KeyName Value Permissions ------- ----- ----------- key1 XXXXXXXXXXXXXXXXXXX Full key2 XXXXXXXXXXXXXXXXXXX Full
I can retrieve the primary key and assign it to a variable by capturing the first key returned.
PS> $key = (Get-AzureRmStorageAccountKey -ResourceGroupName General -Name generaldiag805)[0].value
Once I know the storage account key and name, I can pass these to New-AzureStorageContext.
$parameters = @{ StorageAccountName = 'generaldiag805' StorageAccountKey = $key } $storageContext = New-AzureStorageContext @parameters
At this point, I can create the file share or a storage share like the PowerShell command New-AzureStorageShare calls it.
New-AzureStorageShare logs -Context $storageContext
When this command is complete, it will return the endpoint representing the file share, which will be important to mount this share later.
After creating the file share, you will then notice it inside the Azure portal under Storage Accounts –> Files as shown below.
You now have an Azure file share created, but unless you know how to access it, it won't do much good. Since the file share is a standard SMB file share, we can actually mount this file share in Windows as a standard drive using New-PSDrive. We simply need to know the file endpoint.
We can mount the file share as a drive by creating a PSCredential object and use the New-PSDrive command specifying the Root parameter as a UNC path to the file endpoint as shown below.
$parameters = @{ String = '<password>' AsPlainText = $true Force = $true } $acctKey = ConvertTo-SecureString @parameters $credential = New-Object System.Management.Automation.PSCredential ArgumentList 'Azure\fileshare',$acctKey New-PSDrive -Name P -PSProvider Filesystem -Root '\\generaldiag805.file.core.windows.net\logs' -Credential $credential
After mounting the drive, you can then browse the P: drive just as any other Windows drive, but you'll be working with files in Azure instead.
Subscribe to 4sysops newsletter!
An Azure file share is a great way to migrate your legacy, on-prem file shares to Azure. You don't have to worry about blob storage at all!
Great write-up. Thanks for sharing!
Don’t forget – the new Azure Az Module for Windows PowerShell, Core, and Cloud Shell Replaces AzureRM.