使用 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/