What is the best way to use Azure File Share to automatically copy files to a directory on Windows Virtual Machine Scale Set (VMSS) instances?
I have successfully used the following command to execute a custom script extension on my Windows VMSS instances:
--settings "{"scriptUri": "https://raw.githubusercontent.com/<censored>/webapp-extentions/main/scripts/custom-script.ps1", "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File custom-script.ps1"}" ^
This works well for running the script, but now I need to ensure that files from the Azure File Share are automatically copied to the VMSS instances’ directories.
What steps or configurations do I need to set up for this? Any help would be greatly appreciated!
1
How to Use Azure File Share to Automatically Copy Files to Windows VMSS Instances?
Here is the PowerShell
script to mount the file share on VMSS instances, initiate the copy to the local drive from the file share after mounting, and disconnect the drive once the copy is complete
Fileshare.ps1
# Variables
$targetDirectory = "C:Fileshare data"
$mountDrive = "L:" # The drive letter where the file share is mounted
# Mount the Azure File Share
$connectTestResult = Test-NetConnection -ComputerName "venkatrgaebd.file.core.windows.net" -Port 445
if ($connectTestResult.TcpTestSucceeded) {
# Save the password so the drive will persist on reboot
cmd.exe /C "cmdkey /add:`"venkatrgaebd.file.core.windows.net`" /user:`"localhostvenkatrgaebd`" /pass:`"TIwD7+kLzjq4dkX18nMnYDC94SesFesFNUu1XmnsfsjYEj8ftsjgq3RFc4ob6nXdZBtPhny934ax+AStnzZucg==`""
# Mount the drive
New-PSDrive -Name L -PSProvider FileSystem -Root "\venkatrgaebd.file.core.windows.netvenkatfileshare" -Persist
} else {
Write-Error "Unable to reach the Azure storage account via port 445. Ensure your network allows access to port 445."
exit
}
# Wait for a few seconds to ensure the mounted drive is ready
Start-Sleep -Seconds 10
# Ensure the target directory ("Fileshare data") exists
if (-not (Test-Path -Path $targetDirectory)) {
New-Item -ItemType Directory -Path $targetDirectory
}
# Copy files from the mounted Azure File Share to the target directory
try {
# Use the correct path for the mounted drive: K:
Copy-Item -Path "$mountDrive*" -Destination $targetDirectory -Recurse -Force -ErrorAction Stop
Write-Host "Files copied successfully from $mountDrive to $targetDirectory"
} catch {
Write-Error "Failed to copy files. Error: $_"
}
# Optionally, unmount the file share after copying
try {
Remove-PSDrive -Name "L"
Write-Host "Drive L: unmounted successfully."
} catch {
Write-Error "Failed to remove the drive. Error: $_"
}
You can place the script (Fileshare.ps1) in GitHub or Azure Blob Storage. If you upload the script to Blob Storage
, add the storage account and access key details in the VMSS Extension command.
Command to create VMSS Extension
az vmss extension set --vmss-name "Venkat" --name "CustomScriptExtension" --resource-group "Venkat_group" --version "1.10" --publisher "Microsoft.Compute" --settings '{"fileUris": ["https://venkatrgaebd.blob.core.windows.net/venkat/Fileshare.ps1"], "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File Fileshare.ps1"}' --protected-settings '{"storageAccountName": "venkatrgaebd", "storageAccountKey": "Zucg=="}'
Output
Fileshare data
After creating the VMSS
extension, the same data will be copied to the VMSS
instance
Reference: az vmss extension set
4