Azure Powershell获取指定订阅下的虚拟机信息(ASM)

 

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

 

详情脚本:

# 登陆Azure Account
Add-AzureAccount -Environment AzureChinaCloud

# 设置订阅ID
$sub = "******"
Select-AzureSubscription -SubscriptionName $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 13 ; $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) = "配置信息"
$sheet.cells.item(1,9) = "系统"
$sheet.cells.item(1,10) = "DNS域名"
$sheet.cells.item(1,11) = "公网IP"
$sheet.cells.item(1,12) = "公共端口"
$sheet.cells.item(1,13) = "私有端口"

# 定义变量
$vms = Get-AzureVM
$vmCount = $vms.Count

# 提取变量Value
for($i=0; $i -lt $vmCount; $i++)
 {
    $vm = $vms[$i];
    $sheet.cells.item($x,1) = $vm.name
    $sheet.cells.item($x,2) = "虚拟机(经典)"
    $sheet.cells.item($x,3) = $vm.status
    $sheet.cells.item($x,4) = $vm.ServiceName
    $uri=$vm.VM.OSVirtualHardDisk.MediaLink.AbsoluteUri
    $location=Get-AzureDisk | Where-Object {$_.MediaLink -eq $uri}| Select-Object Location        
    $sheet.cells.item($x,5) = $location.Location
    $sheet.cells.item($x,6) = $sub
    $sheet.cells.item($x,7) = $vm.IpAddress
    $sheet.cells.item($x,8) = $vm.InstanceSize        
    $sheet.cells.item($x,9) = $vm.VM.OSVirtualHardDisk.OS
    $sheet.cells.item($x,10) = $vm.ServiceName+".chinacloudapp.cn"
    $endpoints = Get-AzureEndpoint -VM $vm
    $endpointsCount = $endpoints.Count
    for($j=0; $j -lt $endpointsCount; $j++)
      {
        $endpoint = $endpoints[$j];
        $sheet.cells.item($x,11) = $endpoint.vip
        $sheet.cells.item($x,12) = $endpoint.port
        $sheet.cells.item($x,13) = $endpoint.localport

        $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]$file="Azure-Classic-VMs.csv"
) 

add-azureaccount -Environment AzureChinaCloud

$subs = Get-AzureSubscription 
$vmobjs = @()

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

    try
    {
        Select-AzureSubscription -SubscriptionId $sub.SubscriptionId -ErrorAction Continue
    
        $vms = Get-AzureVm
        $svcs = Get-AzureService

        foreach ($vm in $vms)
        {
            $service = $svcs | where-object { $_.ServiceName -eq $vm.ServiceName }

            $vmInfo = [pscustomobject]@{
                'Subscription'=$sub.SubscriptionName
                'Mode'='Classic'
                'Name'=$vm.Name
                'ServiceName' = $vm.ServiceName
                'Location' = $service.Location
                'VMSize' = $vm.InstanceSize
                'Status' = $vm.Status
                'AvailabilitySet' = $vm.AvailabilitySetName}
        
            $vmobjs += $vmInfo

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

 

转载于:https://www.cnblogs.com/stonehe/p/8395721.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值