c# - 如何使用 C# 和 Azure .Net SDK 在 Azure 中克隆和管理虚拟机?

标签 c# azure virtual-machine

我已遵循 how to manage virtual machines using C# 上的指南并可以基于它创建一个示例程序。

但是,此外我还有一个用例,我们想要克隆多个虚拟机并使用用 C# 编写的控制台程序远程管理它们。我可以找到clone的方法通过 power shell 获得虚拟机,但我无法在任何地方找到如何创建克隆并最终在 Azure 上管理克隆的虚拟机。

任何人都可以指导我如何使用 .Net SDK 来实现此用例吗?

谢谢

最佳答案

我们可以用 Microsoft.Azure.Management.Fluent 来做到这一点和 WindowsAzure.Storage .

我为它创建了一个演示,它对我来说工作正常。以下是我的详细步骤。

正如您提到的link 。首先我们需要释放虚拟机。在此之前,我们需要对Azure中的操作资源进行身份验证。

1.使用认证文件获取Azure对象,如何创建认证文件请引用document 。在此之前我们需要注册一个Azure AD应用程序并为其分配相应的角色,更多详细信息请参阅document 。然后我们可以从Azure AD应用程序中获取clientId、 key ( key )和租户。文件格式如下

subscription=########-####-####-####-############
client=########-####-####-####-############
key=XXXXXXXXXXXXXXXX
tenant=########-####-####-####-############
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
graphURL=https\://graph.windows.net/

创建 Microsoft.Azure.Management.Fluent.Azure 对象

 var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"path of authentication file");

 var azure = Azure

                .Configure()

                .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC)

                .Authenticate(credentials)

                .WithDefaultSubscription()

2.使用C#代码释放虚拟机。

azure.VirtualMachines.PowerOff(resourcegroup,vmName);  //shutdown the VM
azure.VirtualMachines.Deallocate(resourcegroup, vmName); //deallocate the VM

3.通过代码获取VM磁盘Vhd uri信息

 var vm = azure.VirtualMachines.GetByGroup(resourcegroup, vmName);
 string vhdUri= vm.StorageProfile.OsDisk.Vhd.Uri;  //result :  https://storageaccountname.blob.core.windows.net/vhds/blob.vhd

4.从vhduri中我们可以获得存储帐户blobname

5.开始复制blob到目标容器,更多操作blob的细节请引用文档。

        var account = azure.StorageAccounts.GetByGroup(resourcegroup,storageaccount);

        var key = account.GetKeys()[0];

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connectionstring");
        // Create a CloudFileClient object for credentialed access to File storage.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference("tomvhds");
        CloudBlobContainer sourceBlobContainer = blobClient.GetContainerReference("vhds");
        container.CreateIfNotExists();
        CloudPageBlob destBlob = container.GetPageBlobReference(destinationBlob);
        CloudPageBlob sourcePageBlob = sourceBlobContainer.GetPageBlobReference(sourceblob);
        destBlob.StartCopy(sourcePageBlob);
        copyVhdurl =  destBlob.Uri.ToString();

6.创建用于创建虚拟机的磁盘

 var disk = azure.Disks.Define("diskname") 
               .WithRegion(location)
               .WithExistingResourceGroup(resourcegroup)
               .WithWindowsFromVhd(copyvhduri)
               .Create();

7.创建VM并从Azure门户检查

 var windowsVm = azure.VirtualMachines.Define(machinename)

                   .WithRegion(location)  //eastasia

                   .WithNewResourceGroup(resourcegroupName)

                   .WithNewPrimaryNetwork("10.0.0.0/28")

                   .WithPrimaryPrivateIpAddressDynamic()

                   .WithNewPrimaryPublicIpAddress("dnslab")

                   .WithSpecializedOsDisk(disk, OperatingSystemTypes.Windows)

                   .WithSize(VirtualMachineSizeTypes.StandardA0)

                   .Create();

enter image description here

enter image description here

packages.config 文件

 <?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.AppService.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Batch.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Cdn.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Compute.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Dns.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Graph.RBAC.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.KeyVault.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Network.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Redis.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.ResourceManager.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Sql.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.Storage.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.TrafficManager.Fluent" version="1.0.0-beta50" targetFramework="net452" />
  <package id="Microsoft.Data.Edm" version="5.8.2" targetFramework="net452" />
  <package id="Microsoft.Data.OData" version="5.8.2" targetFramework="net452" />
  <package id="Microsoft.Data.Services.Client" version="5.8.2" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.5" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.5" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.10" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
  <package id="System.ComponentModel.EventBasedAsync" version="4.0.11" targetFramework="net452" />
  <package id="System.Dynamic.Runtime" version="4.0.0" targetFramework="net452" />
  <package id="System.Linq.Queryable" version="4.0.0" targetFramework="net452" />
  <package id="System.Net.Requests" version="4.0.11" targetFramework="net452" />
  <package id="System.Spatial" version="5.8.2" targetFramework="net452" />
  <package id="WindowsAzure.Storage" version="8.1.1" targetFramework="net452" />
</packages>

关于c# - 如何使用 C# 和 Azure .Net SDK 在 Azure 中克隆和管理虚拟机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42933466/

相关文章:

c# - Entity Framework核心3.1.1多级继承

azure - Azure 事件中心事件属性如何序列化?

centos - 访问在虚拟机中运行的 http 服务器上的文档

powershell - 在不使用WMI的情况下获取操作系统

c# - 如何允许用户从列表框中复制项目并粘贴到窗体之外

c# - 使 div 元素与 PDF 输出中的 A4 大小相同

azure - 通过 Azure CLI 创建 Azure DevOps 组织

windows - 使用 Docker 在 Windows 上运行 Ubuntu 服务器并使用 IDE 进行开发的最佳方法是什么?

C# html 表单元素数组

c# - 如何在 Azure 搜索中允许自定义分析器使用通配符