Search code examples
azurepowershellazure-storagenetwork-drive

Azure file storage mapped to Win10 as network drive requests login after reboot


I am working with an Azure File Storage and have been mapping the storage as a network drive U on my physical Windows 10 PC. I mount it using PowerShell:

net use U: \\exampleaccount.file.core.windows.net\filesharename /u:AZURE\exampleaccount AzureAccessKey /persistent:Yes

However, every time I reboot my PC, the network drive asks for me to input the credentials associated with the storage account.

1) Does Windows not store the AzureAccessKey key originally used to map the drive?

2) What would be the simplest way to automatically fix this every time the system is rebooted?


Solution

  • Please refer the doc here to persist Azure file share credentials in Windows:

    The cmdkey utility allows you to store your storage account credentials within Windows. This means that when you try to access an Azure file share via its UNC path or mount the Azure file share, you will not need to specify credentials. To save your storage account's credentials, run the following PowerShell commands, replacing <your-storage-account-name> and <your-resource-group-name> where appropriate.

    $resourceGroupName = "<your-resource-group-name>"
    $storageAccountName = "<your-storage-account-name>"
    
    # These commands require you to be logged into your Azure account, run Login-AzAccount if you haven't
    # already logged in.
    $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
    $storageAccountKeys = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName
    
    # The cmdkey utility is a command-line (rather than PowerShell) tool. We use Invoke-Expression to allow us to 
    # consume the appropriate values from the storage account variables. The value given to the add parameter of the
    # cmdkey utility is the host address for the storage account, <storage-account>.file.core.windows.net for Azure 
    # Public Regions. $storageAccount.Context.FileEndpoint is used because non-Public Azure regions, such as sovereign 
    # clouds or Azure Stack deployments, will have different hosts for Azure file shares (and other storage resources).
    Invoke-Expression -Command ("cmdkey /add:$([System.Uri]::new($storageAccount.Context.FileEndPoint).Host) " + `
    "/user:AZURE\$($storageAccount.StorageAccountName) /pass:$($storageAccountKeys[0].Value)")
    

    You can verify the cmdkey utility has stored the credential for the storage account by using the list parameter:

    cmdkey /list
    

    If the credentials for your Azure file share are stored successfully, the expected output is as follows (there may be additional keys stored in the list):

    Currently stored credentials:
    
    Target: Domain:target=<storage-account-host-name>
    Type: Domain Password
    User: AZURE\<your-storage-account-name>
    

    You should now be able to mount or access the share without having to supply additional credentials.