Friday 1 May 2020

MS Azure : PowerShell script to Map Azure Storage Fileshare as a Drive Letter

PowerShell script to map fileshare named folder "DatabaseFiles" from Azure fileshare named "filesdbprod01":

# Input Variables : Provide input to following Variables

$filesharename="filesdbprod01"
$accesskey = "asnjanskdaodnajksndjknaskdnand&^&T^ASDASasaksjdkasjsdajsod"
$root = "DatabaseFiles"
$drive = "X"


# Script Variables
$fullFSName= $filesharename + ".file.core.windows.net"
$FSroot = "\\" + $fullFSName + "\" +$root
$user = "Azure\" + $filesharename

$connectTestResult = Test-NetConnection -ComputerName $fullFSName -Port 445

if ($connectTestResult.TcpTestSucceeded) {
# Save the password so the drive will persist on reboot
cmd.exe /C "cmdkey /add:$fullFSName /user: $user /pass: $accesskey "
# Mount the drive
New-PSDrive -Name $drive -PSProvider FileSystem -Root $FSroot -Persist
} else {
Write-Error -Message "Unable to reach the Azure storage account via port 445.
Check to make sure your organization or ISP is not blocking port 445,
or use Azure P2S VPN, Azure S2S VPN, orExpress Route to tunnel 
SMB traffic over a different port."
}