- 1 year ago
Automatically map Azure drive on Win2012 R2 server
To automatically map an Azure File Share as a drive on a Windows Server 2012 R2 machine, you can use a PowerShell script that utilizes the New-PSDrive
cmdlet. Here's how you can do it:
Install the Azure PowerShell Module:
If you haven't already, you need to install the Azure PowerShell module on your Windows Server 2012 R2 machine. You can install it using the following command:powershellInstall-Module -Name Az -AllowClobber -Scope CurrentUser
Create a PowerShell Script:
Create a PowerShell script (e.g.,MapAzureDrive.ps1
) with the following content:powershell# Replace these values with your actual Azure Storage account and share information
$storageAccountName = "yourstorageaccountname"
$storageAccountKey = "yourstorageaccountkey"
$shareName = "yoursharename"
$driveLetter = "Z"
# Construct the UNC path for the Azure File Share
$uncPath = "\\$storageAccountName.file.core.windows.net\$shareName"
# Create a secure password from the storage account key
$storageAccountKeyBytes = [System.Convert]::FromBase64String($storageAccountKey)
$storageAccountKeySecureString = ConvertTo-SecureString -AsPlainText -Force ([System.Text.Encoding]::UTF8.GetString($storageAccountKeyBytes))
# Create a PSCredential object
$credential = New-Object System.Management.Automation.PSCredential($storageAccountName, $storageAccountKeySecureString)
# Map the drive
New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $uncPath -Persist -Credential $credential
Run the Script:
Open a PowerShell console with administrator privileges and run the script using the following command:powershellSet-ExecutionPolicy RemoteSigned
.\MapAzureDrive.ps1
Make sure to replace the placeholder values (
yourstorageaccountname
,yourstorageaccountkey
,yoursharename
) with your actual Azure Storage account and share information.
This script will map the Azure File Share as a drive with the specified drive letter on your Windows Server 2012 R2 machine. The -Persist
flag ensures that the mapping remains after a reboot. Remember that storing storage account keys directly in scripts is not a recommended security practice. You might consider using Azure Managed Identities or Azure Key Vault for more secure credential handling.