【问题标题】:PowerShell Azure IaaS export of Virtual Machine, Virtual Network and Storage for DR用于灾难恢复的虚拟机、虚拟网络和存储的 PowerShell Azure IaaS 导出
【发布时间】:2014-07-17 15:35:34
【问题描述】:

是否有人知道从 Azure IaaS 虚拟机实施中检索以下信息的方法和/或示例代码?

  1. 关联的虚拟网络,按名称 - 不是子网;我已经可以使用它了,因为它不是订阅中的所有虚拟网络唯一的,所以不能使用。
  2. VM 附加磁盘的存储帐户

关于问题的其他上下文,我们正在尝试生成用于 DR 的 XML,这将允许我们在主数据中心发生故障时在辅助数据中心上重建 IaaS 实施。我们已经设法检索了大部分(如果不是全部)我们需要的信息,但找不到从 VM(或反之亦然)或存储帐户/容器获取 VNet 的方法( s) 用于 VM 的磁盘。

【问题讨论】:

    标签: azure azure-storage azure-virtual-machine azure-powershell azure-virtual-network


    【解决方案1】:

    我会回答你问题的第二部分。使用 powershell 很容易。假设 vm 名称为“testvm”,服务名称为“testservice”。 Powershell 获取存储帐户名称-

     $disk = Get-AzureVM -ServiceName "testservice" –Name "testvm" | Get-AzureOSDisk
     $mediaLink = $disk.MediaLink
     $storageAccountName = $mediaLink.Host.Split('.')[0]
     $storageAccountName 
    

    希望这能回答你的问题。

    【讨论】:

      【解决方案2】:

      在 (2) 上,您不能为每个磁盘提取 MediaLocation 值吗?

      <PersistentVMRole xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <RoleName>name-of-the-virtual-machine</RoleName>
        <RoleType>PersistentVMRole</RoleType>
        <VMImage>name-of-vm-image</VMImage>
        <MediaLocation>path-to-vhds</MediaLocation>
      

      这将使用 GET 来完成:

      https://management.core.windows.net/<subscription-id>/services/hostedservices/<cloudservice-name>/deployments/<deployment-name>/roles/<role-name>
      

      -马特

      【讨论】:

      • 谢谢马特 - 我想知道这是否是唯一的方法,但希望该值出现在返回的对象属性中,而不是必须手动从字符串的一部分中提取它。我认为这将是目前唯一的选择。
      【解决方案3】:

      我知道这个问题很老,但没有完整的答案。我使用这个 PowerShell 脚本将单个订阅中的 VM 的 XML 数据收集到一个文件中。本质上,它获取所有内容,然后进行过滤。

      $VMs = Get-AzureRmVM
      $NICs = Get-AzureRmNetworkInterface
      $VNETs = Get-AzureRmVirtualNetwork
      
      $AzureVMInventory = foreach ($VM in $VMs) {
      
          $NIC = $NICs | Where { $_.Id -eq $VM.NetworkProfile.NetworkInterfaces.Id }
          $Subnet = $VNETs.Subnets | Where { $_.Id -eq $NIC.IPConfigurations.Subnet.Id }
          $VNET = $VNETs | Where {$_.Subnets.Id -eq $NIC.IPConfigurations.Subnet.Id}
      
          $Property = [ordered]@{
              Name = $VM.OSProfile.ComputerName
              VMSize = $VM.HardwareProfile.VmSize
              ResourceGroup = $VM.ResourceGroupName
              OSDisk = $VM.StorageProfile.OsDisk.Vhd.Uri
              DataDisk = $VM.StorageProfile.DataDisks.Vhd.Uri -join "#"
              IPAddresses = $NIC.IpConfigurations.PrivateIPAddress
              IPAllocationMethod = $NIC.IpConfigurations.PrivateIPAllocationMethod
              nicName = $NIC.Name
              vNet = $VNET.Name
              Subnet = $Subnet.Name
              Location = $NIC.Location
          }
      
          New-Object -TypeName PSObject -Property $Property
      }
      
      $XML = $AzureVMInventory | ConvertTo-Xml -Depth 99
      
      # Inject XSLT processing into the XML
      $ProcessInstruction = $XML.CreateProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="style.xsl"')
      $XML.InsertBefore($ProcessInstruction, $XML.DocumentElement)
      
      $XML.Save("$PSScriptRoot\ProductionVMs.xml")
      

      由于阅读 XML 是一场噩梦,我插入了一个流程指令并调用了一个 XSL 样式表,以便当浏览器打开 XML 文件时,它可以被格式化为 HTML 并作为表格可读。这适合上面生成的 XML,但有一些特定的代码用于我的目的。

      <?xml version="1.0" encoding="ISO-8859-1"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output encoding="ISO-8859-1"/>
      
          <xsl:template name="tokenizeString">
              <!--passed template parameter -->
              <xsl:param name="list"/>
              <xsl:param name="delimiter"/>
              <xsl:choose>
                  <xsl:when test="contains($list, $delimiter)">                
                          <!-- get everything in front of the first delimiter -->
                          <xsl:value-of select="substring-before($list,$delimiter)"/>
                      <br />
                      <xsl:call-template name="tokenizeString">
                          <!-- store anything left in another variable -->
                          <xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
                          <xsl:with-param name="delimiter" select="$delimiter"/>
                      </xsl:call-template>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:choose>
                          <xsl:when test="$list = ''">
                              <xsl:text/>
                          </xsl:when>
                          <xsl:otherwise>
                                  <xsl:value-of select="$list"/>
                              <br />
                          </xsl:otherwise>
                      </xsl:choose>
                  </xsl:otherwise>
              </xsl:choose>
          </xsl:template> 
      
       <xsl:template match="/">
      
      <html>
      <head>
      <title>Azure Virtual Machines</title>
      <link rel="stylesheet" href="style.css" type="text/css" />
      </head>
      <body>
          <table>
          <tr>
            <th>Name</th>
            <th>IP Addresses</th>
            <th>IP Allocation Method</th>
            <th>Virtual Network</th>
            <th>Subnet</th>
            <th>VMSize</th>
            <th>Resource Group</th>
            <th>Network Interface</th>
            <th>Location</th>
            <th>OS Disk</th>
            <th>Data Disks</th>
          </tr>
          <xsl:for-each select="//Objects/Object">
          <xsl:sort select="Property[@Name='Name']" order="ascending" data-type="text"/>
              <xsl:variable name="datadisks" select="Property[@Name='DataDisk']"/>
              <tr>
                  <td><xsl:value-of select="Property[@Name='Name']" /></td>
                  <td><xsl:value-of select="Property[@Name='IPAddresses']" /></td>
                  <td><xsl:value-of select="Property[@Name='IPAllocationMethod']" /></td>
                  <td><xsl:value-of select="Property[@Name='vNet']" /></td>
                  <td><xsl:value-of select="Property[@Name='Subnet']" /></td>
                  <td><xsl:value-of select="Property[@Name='VMSize']" /></td>
                  <td><xsl:value-of select="Property[@Name='ResourceGroup']" /></td>
                  <td><xsl:value-of select="Property[@Name='nicName']" /></td>
      
                      <!-- <td><xsl:value-of select="Property[@Name='Location']" /></td> -->
                      <xsl:choose>
                          <xsl:when test="Property[@Name='Location'] = 'northeurope'">
                              <td class="loc_ne">North Europe</td>
                          </xsl:when>
                          <xsl:when test="Property[@Name='Location'] = 'westeurope'">
                              <td class="loc_we">West Europe</td>
                          </xsl:when>
                          <xsl:otherwise>
                              <td class="loc_err">NOT EUROPE</td>
                          </xsl:otherwise>
                      </xsl:choose>
      
                  <td><xsl:value-of select="Property[@Name='OSDisk']" /></td>
                  <td>
                      <!-- <xsl:value-of select="Property[@Name='DataDisk']" /> -->
                      <xsl:choose>
                          <xsl:when test="boolean(Property[@Name='DataDisk'])">
                              <xsl:call-template name="tokenizeString">
                                  <xsl:with-param name="list" select="Property[@Name='DataDisk']"/>
                                  <xsl:with-param name="delimiter" select="'#'"/>
                              </xsl:call-template>
                          </xsl:when>
                          <xsl:otherwise/>
                      </xsl:choose>
                  </td>
              </tr>
          </xsl:for-each> 
          </table>
      
      </body>
      </html>
      
      </xsl:template>
      
      </xsl:stylesheet>
      

      最后一点 CSS 让它整洁美观。

      body * {
          font-family: 'Segoe UI', Arial, Helvetica, sans-serif;
          font-size: 9pt;
      }
      table {
          width: 100%;
      }
      table, th, td {
          border: 1px solid rgba(0,188,242,0.2);
          border-collapse: collapse;
          text-align: center;
      }
      th {
          background: rgb(0,32,80);
          color: #fff;
          font-size: 11pt;
      }
      td {
          width: 10%;
          padding: 2px;
      }
      td.loc_ne {
          background: rgba(0,188,242,0.5);
      }
      td.loc_we {
          background: rgba(0,127,0,0.5);
      }
      td.loc_err {
          background: rgba(219,20,61,0.5);
      }
      tr:nth-child(even) {background: rgba(0,188,242,0.5);}
      tr:nth-child(odd) {background: #fff}
      tr:hover {background-color: #ddd}
      

      由于该对象是一个 PowerShell 对象,它不必导出为 XML,它可以导出为 JSON、CSV 等。通过在 PowerShell 脚本底部标记一些额外的代码,您可以导入如果您需要共享单个文件,则直接将 XSL 导出为纯 HTML。

      # Convert to HTML (if required)
      $XSLT = New-Object System.Xml.Xsl.XslCompiledTransform;
      $XSLT.Load("style.xsl");
      $XSLT.Transform("VMs.xml", "VMs.html");
      

      【讨论】:

        猜你喜欢
        • 2013-03-04
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-15
        • 2013-01-29
        • 2014-11-25
        • 1970-01-01
        相关资源
        最近更新 更多