How to Use Azure File Share to Automatically Copy Files to Windows VMSS Instances?

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

Recognized by Microsoft Azure Collective

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật