如何使用Azure File Share自动将文件复制到Windows VMSS实例?

问题描述 投票:0回答:1

使用 Azure 文件共享自动将文件复制到 Windows 虚拟机规模集 (VMSS) 实例上的目录的最佳方法是什么?

我已成功使用以下命令在我的 Windows VMSS 实例上执行自定义脚本扩展:

--settings "{\"scriptUri\": \"https://raw.githubusercontent.com/<censored>/webapp-extentions/main/scripts/custom-script.ps1\", \"commandToExecute\": \"powershell -ExecutionPolicy Unrestricted -File custom-script.ps1\"}" ^

这对于运行脚本来说效果很好,但现在我需要确保 Azure 文件共享中的文件自动复制到 VMSS 实例的目录中。

我需要为此设置哪些步骤或配置?任何帮助将不胜感激!

azure azure-vm-scale-set azure-file-share
1个回答
0
投票

如何使用 Azure File Share 自动将文件复制到 Windows VMSS 实例?

这里是

PowerShell
脚本,用于在VMSS实例上挂载文件共享,挂载后启动从文件共享到本地驱动器的复制,并在复制完成后断开驱动器

文件共享.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:`"localhost\venkatrgaebd`" /pass:`"TIwD7+kLzjq4dkX18nMnYDC94SesFesFNUu1XmnsfsjYEj8ftsjgq3RFc4ob6nXdZBtPhny934ax+AStnzZucg==`""

    # Mount the drive
    New-PSDrive -Name L -PSProvider FileSystem -Root "\\venkatrgaebd.file.core.windows.net\venkatfileshare" -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: $_"
}

您可以将脚本 (Fileshare.ps1) 放置在 GitHub 或 Azure Blob Storage 中。如果您将脚本上传到

Blob Storage
,请在 VMSS 扩展命令中添加存储帐户并访问密钥详细信息。

创建 VMSS 扩展的命令

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=="}'

输出

enter image description here

文件共享数据

enter image description here

创建

VMSS
扩展后,相同的数据将被复制到
VMSS
实例

enter image description here

参考az vmss扩展集

© www.soinside.com 2019 - 2024. All rights reserved.