In many organisations, there is a need to map Azure Files shares to Windows virtual machines without deploying a traditional file server or a fully-fledged Entra Domain Services (Azure AD Domain Services) environment. One efficient and secure method is to use the system-assigned managed identity on your Azure VM. This approach eliminates the need to store credentials on the VM and ensures that access to your storage account is governed through Azure RBAC roles rather than static credentials.
This guide will walk you through:
- Assigning appropriate RBAC roles to your VM’s managed identity.
- Creating a PowerShell script to retrieve a storage account key using the managed identity.
- Mapping the Azure Files share to a drive letter on the VM.
- Automating drive mapping for all users at logon through a scheduled task.
This solution is especially useful for scenarios like FSLogix profile containers utilising Cloud Cache. By setting the CCDLocations registry key in FSLogix to point to the newly mapped network drive, you can seamlessly store and retrieve user profiles on Azure Files without needing Entra Domain Services.
1. Assign Required RBAC Roles
Before you can programmatically retrieve your storage account keys through the system-assigned managed identity, you must grant the identity sufficient permissions to access and manage the keys and files:
- Storage Account Key Operator Service: Allows the managed identity to retrieve the storage account keys.
- Storage File Data SMB Share Contributor (optional but strongly recommended): Ensures the VM can read, write, and delete files/directories in the Azure file share.
Steps to Assign RBAC Roles
- Navigate to your Storage Account in the [Azure portal].
- Under Access control (IAM), select Add > Add role assignment.
- Assign Storage Account Key Operator Service to the VM’s system-assigned managed identity.
- Optionally, assign Storage File Data SMB Share Contributor for full file-level operations.
Once these roles are set, your VM’s identity can securely manage and mount the Azure Files share.
2. Create the PowerShell Script
Below is a PowerShell script that:
- Authenticates using the VM’s system-assigned managed identity.
- Retrieves the storage account key via Azure REST API calls.
- Maps a network drive to the Azure Files share using the retrieved key.
Make sure to update the variables (such as storageAccountName, shareName, resourceGroupName, SubscriptionId, and driveLetter) to match your environment. Save the script locally on the VM (e.g., C:\MapDrive.ps1).
3. Create a Scheduled Task to Map the Drive at Logon
To ensure the drive is mapped for all users who log on to the VM, configure a scheduled task that triggers whenever a user logs on. This way, each user session automatically has the drive mapped.
Run the following PowerShell commands (adjusting file paths if needed) to register the task:
Important Notes
- The
-ExecutionPolicy Bypassparameter ensures the script runs without being blocked by the default PowerShell execution policy. - The
-WindowStyle Hiddenparameter keeps the console minimised during script execution, providing a cleaner user experience.
4. (Optional) FSLogix Configuration with Cloud Cache
If you are using FSLogix to manage user profiles, you can set up Cloud Cache by adding multiple storage locations in the CCDLocations registry key. For example, point to Z:\Profiles (if your mapped drive is Z:) to store FSLogix profile data.
Registry Path:HKLM\SOFTWARE\FSLogix\Profiles
Value: CCDLocations (REG_MULTI_SZ)
Data: Paths to each storage location, one per line (e.g., Z:\Profiles).
This allows you to leverage the robust features of FSLogix Cloud Cache while storing user profiles on Azure Files, all without needing a traditional domain controller or Entra Domain Services.
5. Testing and Validation
- Run the Script Manually: From an elevated PowerShell session, run your
MapDrive.ps1script to confirm the drive is mapped successfully. - Verify Permissions: Attempt to create, read, and delete files on the mapped drive.
- User Logon Test: Log off and log on as a new user (or an existing user without a prior session) to confirm the scheduled task automatically maps the drive.
Conclusion
Using a system-assigned managed identity on an Azure VM to map an Azure Files share is a straightforward and secure approach that avoids the need for deploying Entra Domain Services. By leveraging built-in Azure RBAC roles (like Storage Account Key Operator Service and Storage File Data SMB Share Contributor), you can ensure that your VM has appropriate access without storing or passing credentials in plain text.
Moreover, FSLogix users can benefit from this method to store user profiles on Azure Files, optionally enabling Cloud Cache for resilient and efficient profile management. With a scheduled task in place, you can be confident that all users logging in will have the drive mapped and ready to go, simplifying your file-sharing and profile-management strategies in Azure.
