为方便Azure用户导出已创建虚拟机的相关信息,特编写如下脚本:

 

详情脚本:

# 登陆Azure Account
Add-AzureRmAccount -EnvironmentName AzureChinaCloud

# 设置订阅ID
$sub = "******"
Select-AzureRmSubscription -Subscription $sub

# 设置Excel格式
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.add()
$sheet = $workbook.worksheets.Item(1)
$sheet.cells.item(1,1) = "Test"
$excel.Visible = $true

for($b = 1 ; $b -le 14 ; $b++)
{
 $sheet.cells.item(1,$b).font.bold = $true
 #$sheet.cells.item(1,$b).borders.LineStyle = $lineStyle::xlDashDot
 $sheet.cells.item(1,$b).borders.ColorIndex = $colorIndex::xlColorIndexAutomatic
 $sheet.cells.item(1,$b).borders.weight = $borderWeight::xlMedium
}
  
$x = 2
$lineStyle = "microsoft.office.interop.excel.xlLineStyle" -as [type]
$colorIndex = "microsoft.office.interop.excel.xlColorIndex" -as [type]
$borderWeight = "microsoft.office.interop.excel.xlBorderWeight" -as [type]
$chartType = "microsoft.office.interop.excel.xlChartType" -as [type]
$sheet.cells.item(1,1) = "名称"
$sheet.cells.item(1,2) = "类型"
$sheet.cells.item(1,3) = "状态"
$sheet.cells.item(1,4) = "资源组"
$sheet.cells.item(1,5) = "位置"
$sheet.cells.item(1,6) = "订阅ID"
$sheet.cells.item(1,7) = "内网IP"
$sheet.cells.item(1,8) = "内网IP保留状态"
$sheet.cells.item(1,9) = "配置信息"
$sheet.cells.item(1,10) = "系统"
$sheet.cells.item(1,11) = "DNS域名"
$sheet.cells.item(1,12) = "公网IP"
$sheet.cells.item(1,13) = "公网IP保留状态"
$sheet.cells.item(1,14) = "目标端口"

# 定义变量
$vms = Get-AzureRmVM -Status
$vmCount = $vms.Count
$nics = Get-AzureRmNetworkInterface
$pips = Get-AzureRmPublicIpAddress
$nsgs = Get-AzureRmNetworkSecurityGroup
Write-Host "done"

$currentSub = (Get-AzureRmContext).Subscription.Name

# 定义方法
Function GetResourceNameFromResourceId($resourceId)
{
    return $resourceId.Substring($resourceId.LastIndexOf('/') + 1);
}

# 提取变量Value
for($i=0; $i -lt $vmCount; $i++)
 {
    $vm = $vms[$i];
    $nicResourceId = $vm.NetworkProfile.NetworkInterfaces[0].Id
    $nicInterfaces = GetResourceNameFromResourceId($nicResourceId)
    #$nic = Get-AzureRmNetworkInterface -Name $nicInterfaces -ResourceGroupName $vm.ResourceGroupName
    $nic = $nics | where {$_.Name -eq  $nicInterfaces -and $_.ResourceGroupName -eq $vm.ResourceGroupName}
    $ipResourceId = $nic.IpConfigurations[0].PublicIpAddress.Id
    $ipAddress = GetResourceNameFromResourceId($ipResourceId)
    $pip = $pips | where {$_.Name -eq $ipAddress -and $_.ResourceGroupName -eq $vm.ResourceGroupName}
    $sheet.cells.item($x,1) = $vm.name
    $sheet.cells.item($x,2) = "虚拟机(ARM)"
    $sheet.cells.item($x,3) = $vm.PowerState
    $sheet.cells.item($x,4) = $vm.ResourceGroupName        
    $sheet.cells.item($x,5) = $vm.Location
    $sheet.cells.item($x,6) = $currentSub
    $sheet.cells.item($x,7) = $nic.IpConfigurations[0].PrivateIpAddress
    $sheet.cells.item($x,8) = $nic.IpConfigurations[0].PrivateIpAllocationMethod        
    $sheet.cells.item($x,9) = $vm.HardwareProfile.VmSize
    $sheet.cells.item($x,10) = $vm.StorageProfile.OsDisk.OsType.ToString()
    $sheet.cells.item($x,11) = $pip.DnsSettings.Fqdn
    $sheet.cells.item($x,12) = $pip.IpAddress
    $sheet.cells.item($x,13) = $pip.PublicIpAllocationMethod

    $nsgResourceId = $nic.NetworkSecurityGroup[0].Id
    $nsgName = GetResourceNameFromResourceId($nsgResourceId)
    #$nsg = Get-AzureRmNetworkSecurityGroup -Name $nsgName -ResourceGroupName $vm.ResourceGroupName
    $nsg = $nsgs | where {$_.Name -eq  $nsgName -and $_.ResourceGroupName -eq $vm.ResourceGroupName}
    
    $ports = $nsg.SecurityRules.DestinationPortRange
    $portCount = $ports.Count
    
    for($j=0; $j -lt $portCount; $j++)
      {
        $port = $ports[$j];
        $sheet.cells.item($x,14) = $port
        
        $x++
      }
 } 
$range = $sheet.usedRange
$range.EntireColumn.AutoFit() | out-null

$uri=$vm.VM.OSVirtualHardDisk.MediaLink.AbsoluteUri
$location=Get-AzureDisk | Where-Object {$_.MediaLink -eq $uri}| Select-Object Location

 

输出项及格式见下:

 

备注:

1.运行脚本期间可能会出现如下报错,忽略即可,不影响最终的结果统计。

2.该脚本运行前,需要事先定义好订阅

3.该脚本输出的为一个Excel表格,输出期间不要关闭正在运行的Excel

4.如果需要对指定Azure账号下的所有订阅进行统计,可参考如下脚本的遍历订阅方法,参考链接:

https://blogs.msdn.microsoft.com/tomholl/2016/11/22/dumping-details-of-azure-virtual-machines-to-csvexcel/

param(
    [string]$tenantId="",
    [string]$file="Azure-ARM-VMs.csv"
) 

Add-AzureRmAccount -EnvironmentName AzureChinaCloud

Get-AzureRmSubscription
$vmobjs = @()

foreach ($sub in $subs)
{
    
    Write-Host Processing subscription $sub.SubscriptionName

    try
    {

        Select-AzureRmSubscription -SubscriptionId $sub.SubscriptionId -ErrorAction Continue

        $vms = Get-AzureRmVm 

        foreach ($vm in $vms)
        {
            $vmInfo = [pscustomobject]@{
                'Subscription'=$sub.SubscriptionName
                'Mode'='ARM'
                'Name'=$vm.Name
                'ResourceGroupName' = $vm.ResourceGroupName
                'Location' = $vm.Location
                'VMSize' = $vm.HardwareProfile.VMSize
                'Status' = $null
                'AvailabilitySet' = $vm.AvailabilitySetReference.Id }
        
            $vmStatus = $vm | Get-AzureRmVM -Status
            $vmInfo.Status = $vmStatus.Statuses[1].DisplayStatus

            $vmobjs += $vmInfo

        }  
    }
    catch
    {
        Write-Host $error[0]
    }
}

$vmobjs | Export-Csv -NoTypeInformation -Path $file
Write-Host "VM list written to $file"

 

posted on 2018-02-01 00:56  铭的随记  阅读(518)  评论(0编辑  收藏  举报