You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助Azure Bicep结合PowerShell脚本启用VM复制并复用rsv.bicep参数?

通过Azure Bicep结合PowerShell启用VM复制

步骤1:修正并扩展现有RSV Bicep配置

原rsv.bicep存在语法问题(VM列表元素未加引号),需添加输出项以便PowerShell获取Vault关键信息:

param rsv string = 'testing-rsv'
param location string = resourceGroup().location
param VMlist array = ['VM1','VM2'] // 修正:数组元素需用引号包裹

resource recoveryServiceVault 'Microsoft.RecoveryServices/vaults@2021-01-01' = { 
  name: rsv 
  location: location 
  sku: {
    name: 'RS0' 
    tier: 'Standard'
  }
}

// 输出PowerShell需要的Vault及VM列表信息
output vaultId string = recoveryServiceVault.id
output vaultName string = recoveryServiceVault.name
output vaultLocation string = recoveryServiceVault.location
output vmList array = VMlist

步骤2:PowerShell部署Bicep并提取参数

执行以下脚本部署Bicep配置,同时捕获输出变量供后续复制操作使用:

# 替换为你的资源组名称
$resourceGroupName = "your-resource-group-name"

# 部署Bicep文件并获取输出
$deploymentOutput = New-AzResourceGroupDeployment `
  -ResourceGroupName $resourceGroupName `
  -TemplateFile ./rsv.bicep

# 提取关键变量
$vaultId = $deploymentOutput.Outputs.vaultId.Value
$vmList = $deploymentOutput.Outputs.vmList.Value

步骤3:PowerShell批量启用VM复制

确保已安装Az.RecoveryServices模块(执行Install-Module -Name Az.RecoveryServices -Force完成安装),然后执行以下脚本完成VM复制启用:

# 设置Recovery Services Vault上下文
Set-AzRecoveryServicesVaultContext -VaultId $vaultId

# 替换为你的复制策略名称(需提前创建)
$replicationPolicy = Get-AzRecoveryServicesAsrPolicy -FriendlyName "DefaultReplicationPolicy"

# 替换为VM所在的资源组名称
$vmResourceGroupName = "your-vm-resource-group"

# 遍历VM列表启用复制
foreach ($vmName in $vmList) {
    # 获取目标VM对象
    $targetVm = Get-AzVM -ResourceGroupName $vmResourceGroupName -Name $vmName

    # 获取保护容器及映射
    $protectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -FriendlyName (Get-AzRecoveryServicesVault -VaultId $vaultId).Location
    $containerMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $protectionContainer

    # 启用VM复制
    Enable-AzRecoveryServicesAsrReplicationProtectedItem `
      -VM $targetVm `
      -ProtectionContainerMapping $containerMapping `
      -Policy $replicationPolicy

    Write-Host "VM $vmName 的复制已启动"
}

关键注意事项

  • 若未创建复制策略,可通过以下命令创建(按需调整参数):
    New-AzRecoveryServicesAsrPolicy `
      -Name "DefaultReplicationPolicy" `
      -ReplicationProvider "HyperVReplicaAzure" ` # 根据复制场景选择,如VMware到Azure用"VMwareAzure"
      -RecoveryPointRetentionInHours 24 `
      -ApplicationConsistentSnapshotFrequencyInHours 6
    
  • 确保执行脚本的账号拥有VM及Recovery Services Vault的参与者或更高权限
  • 替换脚本中所有占位符(资源组名称、策略名称等)为实际环境值

内容的提问来源于stack exchange,提问作者satish B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:29:53