重新创建 Azure VM 时如何重新配置​​ Azure 诊断扩展

How do I reconfigure the Azure diagnostics extension when recreating an Azure VM

我需要对现有计算机上不允许的 Azure 资源管理器虚拟机进行更改,例如更改可用性组。所以我必须删除并重新创建机器,将现有磁盘、网络适配器等附加到新 VM。我有一个 PowerShell 脚本来执行此操作,但我 运行 遇到了虚拟机扩展的问题。

这是我的代码:

$NewVMConfig = New-AzureRmVMConfig -VMName $VM.Name -VMSize $VM.HardwareProfile.VmSize
$NewVMConfig = Set-AzureRmVMOSDisk -VM $NewVMConfig -Name $VM.StorageProfile.OSDisk.Name -VhdUri $VM.StorageProfile.OSDisk.VHD.Uri -CreateOption attach -Windows
foreach ($disk in $vm.StorageProfile.DataDisks) {
    $NewVMConfig = Add-AzureRmVMDataDisk -VM $NewVMConfig -Name $disk.Name -VhdUri $disk.Vhd.Uri -Caching $disk.Caching -DiskSizeInGB $disk.DiskSizeGB -CreateOption attach -Lun $disk.Lun
}
$NewVMConfig.AvailabilitySetReference = $VM.AvailabilitySetReference
$NewVMConfig.DiagnosticsProfile = $VM.DiagnosticsProfile
$NewVMConfig.Extensions = $VM.Extensions
$NewVMConfig.NetworkProfile = $VM.NetworkProfile
$location = $VM.Location
$resourceGroupName = $VM.ResourceGroupName

# Delete machine.  
Remove-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name

# Recreate machine
New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $NewVMConfig 

注意这行:

$NewVMConfig.Extensions = $VM.Extensions

脚本运行没有任何错误,但新 VM 的扩展名与原始 VM 不同。诊断扩展不见了,它现在有原机器上没有的 BGInfo 扩展。

我可以使用 Remove-AzureRmVMExtension 命令删除 BGInfo 扩展,但我未能成功重新创建诊断扩展。我试过 Set-AzureRmVMExtensionSet-AzureRmVMDiagnosticsExtension 都没有用。

那些VM扩展命令还不支持ARM。因此,我建议您改用 ARM 模板。有一个quick-start template specifically for Windows VM with diagnostics extension on GitHub。您可以下载它并对其进行修改以满足您的需要,例如为您的 VM 指定 VHD。并且,使用 New-AzureRmResourceGroupDeployment 部署您的虚拟机。

对于您的情况,将上述模板与 201-specialized-vm-in-existing-vnet 模板结合使用即可满足您的需求。

注意:201-vm-diagnostics-extension-windows 模板部署了带有诊断扩展的 Windows VM,而 201-specialized-vm-in-existing-vnet 模板部署了一个具有现有 VNet 和 VHD 的 VM

有关此的详细信息,请参阅 Create a Windows Virtual machine with monitoring and diagnostics using Azure Resource Manager Template

有关编写 ARM 模板的详细信息,请参阅 Authoring Azure Resource Manager templates

有关部署 ARM 模板的详细信息,请参阅 Deploy a Resource Group with Azure Resource Manager template

Jack Zeng 对虚拟机模板的回答向我展示了我在尝试重新配置 Azure 诊断扩展时遗漏了什么。

关键是,当您获得 VM 并查看扩展 属性(或扩展文本 属性)时,它不包括扩展的受保护设置。 (这是保护它们的一种方式。)因此您没有重新创建扩展所需的所有信息。您必须重建受保护的设置,这些设置因扩展而异,因此您需要知道特定扩展的要求。 Jack 向其提供的虚拟机模板 link 显示了 Azure 诊断扩展的受保护设置所需的信息,即存储帐户名称、密钥和端点。

运行 重新创建虚拟机后的以下代码成功地重新配置了诊断。在这段代码中 $VM 是我们在重新创建机器之前通过调用 Get-AzureRmVM 获得的原始虚拟机对象。

$diagnosticsExtension = $VM.Extensions | Where { $_.Name -eq 'Microsoft.Insights.VMDiagnosticsSettings' }

# The $VM.Extensions.Settings property does not correctly return the values of the different settings.
# Instead, use the $VM.ExtensionsText property to get the old settings.
$oldSettings = $VM.ExtensionsText | ConvertFrom-Json | Where { $_.Name -eq 'Microsoft.Insights.VMDiagnosticsSettings' } | foreach {$_.'properties.settings'}

# Need settings in a hash table.
$settings = @{
    xmlCfg = $oldSettings.xmlCfg; 
    StorageAccount = $oldSettings.StorageAccount
}

$storageAccounts = Get-AzureRmStorageAccount
$storageAccount = $storageAccounts | Where { $_.StorageAccountName -eq $settings.StorageAccount }
$storageAccountKeys = $storageAccount | Get-AzureRmStorageAccountKey

$protectedSettings = @{
    storageAccountName = $settings.StorageAccount;
    storageAccountKey = $storageAccountKeys.Key1;
    storageAccountEndPoint = "https://core.windows.net/"
}

Write-Host "Reconfiguring Azure diagnostics extension on $Name..."
$result = Set-AzureRmVMExtension -ResourceGroupName $newVM.ResourceGroupName -VMName $newVM.Name  -Name $diagnosticsExtension.name -Publisher $diagnosticsExtension.Publisher -ExtensionType $diagnosticsExtension.VirtualMachineExtensionType -TypeHandlerVersion $diagnosticsExtension.TypeHandlerVersion -Settings $settings -ProtectedSettings $protectedSettings -Location $diagnosticsExtension.Location

请注意,我是 运行 Azure PowerShell 扩展的 1.2.1 版。在此版本中,Set-AzureRmVMDiagnosticsExtension 似乎已损坏,因此我没有使用它。