Azure Powershell获取指定订阅下的虚拟机信息(ARM)
为方便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账号下的所有订阅进行统计,可参考如下脚本的遍历订阅方法,参考链接:
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"
Azure Powershell获取指定订阅下的虚拟机信息(ARM)的更多相关文章
- Azure Powershell获取指定订阅下的虚拟机信息(ASM)
为方便Azure用户导出已创建虚拟机的相关信息,特编写如下脚本: 详情脚本: # 登陆Azure Account Add-AzureAccount -Environment AzureChinaClo ...
- [转]C# 获取指定目录下所有文件信息、移动目录、拷贝目录
原文:http://blog.csdn.net/vchao13/article/details/6200255 1.获取指定目录下所有文件信息 /// <summary> /// 返回指定 ...
- C# 获取指定目录下所有文件信息
/// <summary> /// 返回指定目录下所有文件信息 /// </summary> /// <param name="strDirectory&quo ...
- 获取指定订阅下所有Azure ARM虚拟机配置(CPU核数,内存大小,磁盘信息)的使用情况
脚本内容: <# .SYNOPSIS This script grab all ARM VM VHD file in the subscription and caculate VHD size ...
- C# 获取指定目录下所有文件信息、移动目录、拷贝目录
/// <summary> /// 返回指定目录下的所有文件信息 /// </summary> /// <param name="strDirectory&qu ...
- c# 获取指定目录下的所有文件并显示在网页上
参考文献: FileInfo 的使用 https://msdn.microsoft.com/zh-cn/library/system.io.fileinfo_methods(v=vs.110).as ...
- 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本
摘自:http://blog.csdn.net/forandever/article/details/5711319 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本 ...
- PHP 获取指定目录下所有文件(包含子目录)
PHP 获取指定目录下所有文件(包含子目录) //glob — 寻找与模式匹配的文件路径 $filter_dir = array('CVS', 'templates_c', 'log', 'img', ...
- TDirectory.GetFileSystemEntries获取指定目录下的目录和文件
使用函数: System.IOUtils.TDirectory.GetFileSystemEntries 所有重载: class function GetFileSystemEntries(const ...
随机推荐
- 《algorithms Unlocked》读书笔记3——计数排序
<Algorithms Unlocked>是 <算法导论>的合著者之一 Thomas H. Cormen 写的一本算法基础,算是啃CLRS前的开胃菜和辅助教材.如果CLRS的厚 ...
- windows 线程
在windows中进程只是一个容器,用于装载系统资源,它并不执行代码,它是系统资源分配的最小单元,而在进程中执行代码的是线程,线程是轻量级的进程,是代码执行的最小单位. 从系统的内核角度看,进程是一个 ...
- [译]what is bootstrap
Question:Bootstrap的定义?有什么用?如何助力前端开发? Answers: 它是一个在用HTML,CSS和javascript创建网站和网页应用的时候可以用到的基础内容. More ...
- Python面试题解答
1. 一个谜题 >>> t = (1, 2, [30, 40]) >>> t[2] += [50, 60] 到底会发生下面 4 种情况中的哪一种? a. t变成(1 ...
- python 单下划线/双下划线使用总结
文章转自:http://blog.csdn.net/pfm685757/article/details/45918575
- MIPI协议-DSI
对于现代的智能手机来说,其内部要塞入太多各种不同接口的设备,给手机的设计 和元器件选择带来很大的难度.下图是一个智能手机的例子,我们可以看到其内部存储.显示.摄像.声音等内部接口都是各不相同的.即使以 ...
- MongoDB批量操作及与MySQL效率对比
本文主要通过批量与非批量对比操作的方式介绍MongoDB的bulkWrite()方法的使用.顺带与关系型数据库MySQL进行对比,比较这两种不同类型数据库的效率.如果只是想学习bulkWrite()的 ...
- 移动端APP列表点透事件处理方法
关于点透事件这里不再赘述,如果不清楚的可以上网搜一搜,或者看小火柴的这篇文章. 这里是自己在做移动端时,在列表滑动的时候,遇到的点透问题.出现这个问题的来由是因为在转场的时候,各个手机的转场效果不一样 ...
- ECJTUACM16 Winter vacation training #5 题解&源码
A------------------------------------------------------------------------------------------- 题目链接:ht ...
- Codeforces Round #356 (Div. 1) C. Bear and Square Grid
C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...