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

如何使用Azure.ResourceManager.Compute基于现有快照创建Azure VM

基于Azure.ResourceManager.Compute从快照创建VM的实现

Azure.ResourceManager.Compute完全支持基于现有快照创建VM并生成OS磁盘的功能,以下是对应你原有逻辑的C#代码示例:

首先确保已安装NuGet包:Azure.ResourceManager.Compute 和 Azure.Identity(用于身份验证)

完整代码示例

using Azure;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.Compute.Models;

// 初始化必要参数
string subscriptionId = "你的订阅ID";
string vmResourceGroupName = "目标资源组名称";
string vmName = "要创建的VM名称";
string vmOSDiskName = "要创建的OS磁盘名称";
AzureLocation vmRegion = AzureLocation.EastUs; // 替换为你的区域
string snapshotId = "现有快照的资源ID";
string networkInterfaceId = "现有网络接口的资源ID";
VirtualMachineSizeType vmSize = VirtualMachineSizeType.StandardE2sV3;

// 创建ArmClient(默认使用Azure CLI/Visual Studio等已配置的身份验证)
ArmClient armClient = new ArmClient(new DefaultAzureCredential());

try
{
    // 获取快照资源
    ResourceIdentifier snapshotResourceId = new ResourceIdentifier(snapshotId);
    SnapshotResource snapshot = armClient.GetSnapshotResource(snapshotResourceId);
    await snapshot.GetAsync(); // 验证快照存在

    Console.WriteLine("Creating os disk for vm...");

    // 构建磁盘创建参数
    DiskData diskData = new DiskData(vmRegion)
    {
        CreationData = new DiskCreationData(DiskCreateOption.Copy)
        {
            SourceResourceId = snapshot.Id
        },
        DiskSizeGB = 128,
        Sku = new DiskSku(DiskStorageAccountType.StandardSsdLrs)
    };

    // 创建磁盘
    ResourceGroupResource resourceGroup = armClient.GetResourceGroupResource(ResourceGroupResource.CreateResourceIdentifier(subscriptionId, vmResourceGroupName));
    DiskResource disk = await resourceGroup.GetDisks().CreateOrUpdateAsync(WaitUntil.Completed, vmOSDiskName, diskData);

    if (disk != null)
    {
        Console.WriteLine("Creating virtual machine from Snapshot...");

        // 构建VM创建参数
        VirtualMachineData vmData = new VirtualMachineData(vmRegion)
        {
            HardwareProfile = new VirtualMachineHardwareProfile()
            {
                VmSize = vmSize
            },
            NetworkProfile = new VirtualMachineNetworkProfile()
            {
                NetworkInterfaces =
                {
                    new VirtualMachineNetworkInterfaceReference()
                    {
                        Id = new ResourceIdentifier(networkInterfaceId),
                        Primary = true
                    }
                }
            },
            OsProfile = new VirtualMachineOsProfile()
            {
                ComputerName = vmName
            },
            StorageProfile = new VirtualMachineStorageProfile()
            {
                OsDisk = new VirtualMachineOsDisk(DiskCreateOptionType.Attach)
                {
                    ManagedDisk = new VirtualMachineManagedDiskParameters()
                    {
                        Id = disk.Id
                    },
                    OsType = OperatingSystemType.Windows
                }
            }
        };

        // 创建VM
        VirtualMachineResource vm = await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(WaitUntil.Completed, vmName, vmData);

        Console.WriteLine($"VM {vm.Data.Name} 创建成功");
    }
    else
    {
        Console.WriteLine("Unable to create os disk for vm...");
    }
}
catch (RequestFailedException ex)
{
    Console.WriteLine($"操作失败: {ex.Message}");
}

关键逻辑说明

  • 磁盘创建:通过DiskCreationData指定DiskCreateOption.Copy,并将SourceResourceId指向快照的资源ID,实现从快照复制生成新磁盘。
  • VM关联磁盘:在VirtualMachineOsDisk中设置DiskCreateOptionType.Attach,并关联刚创建的磁盘ID,同时指定操作系统类型为Windows。
  • 身份验证:示例使用DefaultAzureCredential,支持本地开发环境的多种身份验证方式(Azure CLI、Visual Studio、Service Principal等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 13:54:55