{"id":93617,"date":"2022-03-28T17:00:10","date_gmt":"2022-03-28T17:00:10","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=93617"},"modified":"2022-05-05T23:56:57","modified_gmt":"2022-05-05T23:56:57","slug":"azure-storage-mapping-file-shares-and-powershell-drives","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/azure-storage-mapping-file-shares-and-powershell-drives\/","title":{"rendered":"Azure Storage: Mapping File Shares and Powershell drives"},"content":{"rendered":"<p><strong>Storage Accounts<\/strong> allow you to create blob containers or file shares. These are two different services provided by them, but you probably already know that. The main difference between these services is the protocol used to access them. While blob containers use <strong>REST<\/strong>, file shares can use <strong>REST<\/strong> and <strong>SMB<\/strong>.<\/p>\n<p>This means when we use file shares we can map them directly to our file system and work with them using <strong>Windows Explorer<\/strong>, as any other network share.<\/p>\n<p>Of course, this is an oversimplification. There are some tricks when mapping an azure file share to the local machine. If you do it wrong, it can cause a lot of trouble.<\/p>\n<h2>File Share Quota<\/h2>\n<p>Each file share has a quota. The default quota is 6GB, which is quite small. Using the file share menu you can click the option Edit Quota to change this limit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93618\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/stormapping01.png\" alt=\"\" width=\"893\" height=\"87\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93619\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/stormapping02.png\" alt=\"\" width=\"262\" height=\"262\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93620\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/Stormapping03.png\" alt=\"\" width=\"566\" height=\"313\" \/><\/p>\n<p>The storage account also has a quota for all the file shares. This quota has only two possible files: 5tb or 100tb.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93621\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/stormapping04.png\" alt=\"\" width=\"799\" height=\"82\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93622\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/stormapping05.png\" alt=\"\" width=\"315\" height=\"264\" \/><\/p>\n<h2>Creating the Mapping<\/h2>\n<p>Create the mapping is very easy. On the file share menu, we can select the option Connect. Azure itself will provide us with a script to create the mapping on Windows, Linux or Mac.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93623\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/stormapping06.png\" alt=\"\" width=\"579\" height=\"720\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>For windows, it&#8217;s a powershell script. We can open <strong>PowerShell ISE<\/strong>, copy\/paste the script from the portal to ISE and execute. We will immediately have a new network mapping pointing to our file share on Azure.<\/p>\n<p>There are two configuration options we can choose. Once we choose these options, the script is changed to use our choices.<\/p>\n<p><strong>Authentication:<\/strong> We can choose if we will use <strong>Azure AD<\/strong> authentication or the storage account key. Azure Authentication is way better option than the storage key. However, Azure AD doesn&#8217;t work with <strong>SMB<\/strong> out-of-the-box. We need to provision an <strong>Azure AD Directory Services<\/strong> to use Azure authentication with <strong>SMB<\/strong>.<\/p>\n<p><strong>Drive letter:<\/strong> You can choose the drive letter which will be used for the mapping on your machine.<\/p>\n<h2>\nFile Share Mapping Internals<\/h2>\n<p>There are some interesting internal tricks related to the file share mapping. Let&#8217;s analyse this and discover how to avoid some mistakes.<\/p>\n<h3>It&#8217;s not your regular Network Mapping<\/h3>\n<p>This mapping created through the PowerShell code is not your regular network mapping. You can&#8217;t managed it using the regular tools for a network mapping. It needs to be managed using Powershell.<\/p>\n<p>These mappings are called Powershell drives. You can discover more about Powershell drives on this link <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/samples\/managing-windows-powershell-drives?view=powershell-7.2\">https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/samples\/managing-windows-powershell-drives?view=powershell-7.2<\/a><\/p>\n<p>It&#8217;s specially important to remember you can&#8217;t delete or disconnect the mapping using traditional OS tools, you need to use <strong><em>Remove-PSDrive<\/em><\/strong> cmdlet in Powershell<\/p>\n<h3>It&#8217;s not for everyone<\/h3>\n<p>The mapping is created for the user who executes the script and only for this user. One example is that if you execute the script with powershell running as administrator, you will not see it in a regular windows explorer window.<\/p>\n<h2>Big Mistake &#8211; be aware and never do this<\/h2>\n<p>What gave me the idea to write this article was exactly the fact I faced a huge error when handling the mappings in a wrong way. I found almost no information about how to fix this, so here it is.<\/p>\n<p>The problem: You should never attempt to delete the mappings using regular OS statements. For example:<\/p>\n<p><strong>NET USE &lt;drive&gt; \/DELETE \/YES<\/strong><\/p>\n<p>Is a complete error &#8211; don&#8217;t do this. I did.<\/p>\n<p>When this is done, the mappings are not removed. The OS, on the other hand, becomes a bit crazy, slow. Windows explorer hangs sometimes, not understanding very well the mappings.<\/p>\n<p>So, it&#8217;s just about opening powershell and executing <strong><em>Remove-PSDrive<\/em><\/strong>, right?<\/p>\n<p>Unfortunately not. Powershell also goes crazy and doesn&#8217;t open anymore, showing the error message <em>Attempt to perform the InitializeDefaultDrives operation on the FileSystem&#8217; provider failed<\/em>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93625\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/stormapping08.png\" alt=\"\" width=\"1280\" height=\"272\" \/><\/p>\n<p>No work-around:<\/p>\n<ul>\n<li>Run powershell on the prompt using the -Command parameter doesn&#8217;t work, same error<\/li>\n<li><strong>Windows Termminal<\/strong> doesn&#8217;t work, same error<\/li>\n<li>Execute powershell as administrator or System doesn&#8217;t work. The mapping will not be there.<\/li>\n<\/ul>\n<p>I end up finding the solution in a <a href=\"https:\/\/docs.microsoft.com\/en-us\/troubleshoot\/windows-server\/networking\/mapped-network-drive-disconnected\">strange documentation from Microsoft<\/a>. Strange because it describes a completely different reason for the error, but the solution works.\u00a0<\/p>\n<h2>The Solution<\/h2>\n<p>You need to remove the mappings directly in the registry. Using <strong>RegEdit<\/strong>, open the following registry key and delete the folders for each mapping: <em>HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\MountPoints2<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93624\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/stormapping07.png\" alt=\"\" width=\"820\" height=\"307\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The additional trick on this solution is that you need to immediately restart the machine. If you open windows explorer or powershell again, the deleted folders appear again, they are like cockroaches.<\/p>\n<h2>Summary<\/h2>\n<p>Mapping a file share on the local machine is a powerful feature, but it has some tricks and if used in a wrong way can cause complex errors<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mapping an Azure Storage Account file share on a local machine is a powerful feature. In this post, Dennes Torres explains how to do it and what to watch out for.&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[145933,145491,4635],"coauthors":[6810],"class_list":["post-93617","post","type-post","status-publish","format-standard","hentry","category-blogs","tag-azure-file-share","tag-azure-storage","tag-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/93617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=93617"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/93617\/revisions"}],"predecessor-version":[{"id":94188,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/93617\/revisions\/94188"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=93617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=93617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=93617"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=93617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}