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

如何用C#版Azure SDK创建启用Trusted Launch的Azure虚拟机?

使用C# Azure SDK创建启用Trusted Launch的Azure虚拟机

前置准备

  • 安装NuGet包:Azure.ResourceManager.Compute(推荐使用最新稳定版)
  • 确保Azure账号具备虚拟机创建权限,本地已配置Azure凭据(如通过Azure CLI登录、环境变量配置等,DefaultAzureCredential会自动识别)

核心代码实现

以下是完整示例代码,重点关注Trusted Launch配置部分:

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

var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential);

// 替换为你的订阅ID
string subscriptionId = "your-subscription-id";
// 替换为你的资源组名称
string resourceGroupName = "your-resource-group-name";
// 替换为虚拟机名称
string vmName = "your-vm-name";
// 替换为目标区域,例如"eastus"
AzureLocation location = AzureLocation.EastUS;

// 获取订阅资源
SubscriptionResource subscription = await armClient.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}")).GetAsync();

// 获取或创建资源组
ResourceGroupResource resourceGroup = await subscription.GetResourceGroups().GetOrCreateAsync(WaitUntil.Completed, resourceGroupName, new ResourceGroupData(location));

// 构建虚拟机配置数据(示例使用Windows Server镜像,可按需替换)
var vmData = new VirtualMachineData(location)
{
    HardwareProfile = new VirtualMachineHardwareProfile()
    {
        VmSize = VirtualMachineSizeTypes.StandardD2sV3 // 需使用支持Trusted Launch的Gen2虚拟机SKU
    },
    StorageProfile = new VirtualMachineStorageProfile()
    {
        ImageReference = new ImageReference()
        {
            Publisher = "MicrosoftWindowsServer",
            Offer = "WindowsServer",
            Sku = "2022-Datacenter",
            Version = "latest"
        },
        OsDisk = new VirtualMachineOsDisk(DiskCreateOptionTypes.FromImage)
        {
            ManagedDisk = new VirtualMachineManagedDiskParameters()
            {
                StorageAccountType = StorageAccountTypes.StandardLrs
            }
        }
    },
    OsProfile = new VirtualMachineOSProfile()
    {
        ComputerName = vmName,
        AdminUsername = "adminuser",
        AdminPassword = "YourStrongPassword123!" // 生产环境请使用安全密码管理方案
    },
    NetworkProfile = new VirtualMachineNetworkProfile()
    {
        NetworkInterfaces =
        {
            // 替换为你的现有网络接口ID,或提前创建网络接口
            new VirtualMachineNetworkInterfaceReference()
            {
                Id = new ResourceIdentifier("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkInterfaces/{nicName}"),
                Primary = true
            }
        }
    },
    // 关键:配置Trusted Launch
    SecurityProfile = new VirtualMachineSecurityProfile()
    {
        TrustedLaunchConfiguration = new TrustedLaunchConfiguration()
        {
            Enabled = true,
            // 可选:启用安全启动(默认启用)
            SecurityLaunchEnabled = true,
            // 可选:启用vTPM(默认启用)
            TpmEnabled = true
        }
    }
};

// 执行虚拟机创建操作
ArmOperation<VirtualMachineResource> operation = await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(WaitUntil.Completed, vmName, vmData);
VirtualMachineResource vm = operation.Value;

Console.WriteLine($"虚拟机 {vm.Data.Name} 创建完成,Trusted Launch状态:{vm.Data.SecurityProfile.TrustedLaunchConfiguration.Enabled}");

关键说明

  • Trusted Launch的配置集中在VirtualMachineSecurityProfile.TrustedLaunchConfiguration属性,设置Enabled = true即可启用,同时可按需配置安全启动和vTPM开关。
  • 必须使用支持Trusted Launch的Gen2虚拟机SKU,Gen1虚拟机不兼容该特性。
  • 从Java SDK转换时需注意:C# SDK采用属性赋值而非链式调用结构,Java中的withTrustedLaunchConfiguration对应C#中直接赋值SecurityProfile.TrustedLaunchConfiguration对象。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 04:42:39