c# - 以编程方式调整 Azure 虚拟机大小的语法

标签 c# azure

使用 C#,如何调整已停止的 Azure 虚拟机的大小? Microsoft 文档不存在或无法找到。在我的 .Net Standard 2.0 类库中,我引用 Nuget 包 Microsoft.Azure.Management.Fluent 1.37.1 并尝试了下面的代码。

using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
...

// get Azure client
string clientId = ...;
string clientSecret = ...;
string subscriptionId = ...;
string tenantId = ...;
AzureEnvironment env = ...;
AzureCredentials creds = new AzureCredentialsFactory().FromServicePrincipal(clientId, clientSecret, tenantId, env);
IAzure azure = Microsoft.Azure.Management.Fluent.Azure
                   .Configure()
                   .WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
                   .Authenticate(creds)
                   .WithSubscription(subscriptionId);

// get virtual machine
string resourceGroupName = "MyResourceGroup";
string machineName = "MyHostname";
IVirtualMachine vm = await azure.VirtualMachines.GetByResourceGroupAsync(resourceGroupName, machineName)

// resize virtual machine
VirtualMachineSizeTypes size = ...;
vm.Inner.HardwareProfile.VmSize = size;
vm.Inner.Validate();
vm.Update();  // nothing happens, virtual machine does not resize

在最后一行,vm.Update() 不执行任何操作。在 Azure 门户中,我可以看到即使多次刷新门户后,虚拟机也没有调整大小。

最佳答案

Azure API 的可发现性非常糟糕。我终于可以调整大小了。替换最后 3 行

vm.Inner.HardwareProfile.VmSize = size;
vm.Inner.Validate();
vm.Update();

VirtualMachineUpdate parameters = new VirtualMachineUpdate()
{
    HardwareProfile = new HardwareProfile(size),
};
VirtualMachineInner response = await azure.VirtualMachines.Inner.UpdateAsync(resourceGroupName, vm.Name, parameters);

关于c# - 以编程方式调整 Azure 虚拟机大小的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66656822/

相关文章:

azure - 是否有内置方法可以将集合复制到同一区域中的 "follower"集合?

asp.net - 如何设置 Visual Studio 以包含 Azure namespace ?

powershell - 如何使用 Function Apps 在 Azure 中使用 PowerShell 登录 AzureRmAccount

c# - 如何在 Xamarin.forms 中的框架边缘显示阴影?

c# - 如何在DataTemplate中访问动态控件WPF

azure static web app + github = 无法合并任何 PR,获取 "deployment_token was not provided",但在对 main 进行正常提交时会进行部署

azure - 使用 Azure AD 帐户将 Azure 逻辑应用连接到 SQL

c# - 调用基类的方法

c# - 仅编写静态方法是否等同于 C# 中的无副作用编程?

c# - 最小起订量:无法转换到界面