当前位置: 首页 > Microsoft Azure > 正文

使用PowerShell 收集Azure VM Image信息

我们在使用Azure PowerShell自动化创建虚拟机的时候,需要知道很多参数才能继续进行,例如:ImagePublisher,ImageOffer,ImageSku,这个时候就需要事先知道这些信息,因此为了自己的方便简单整理一些使用PowerShell 收集Azure VM的信息,也供大家参考

首先使用您的有效Azure帐户登陆



获取当前订阅下的虚拟机的基本信息

Get-AzureRmVM select Name, ResourceGroupName, Location,@{Name=“VmSize”; Expression={$_.HardwareProfile.VmSize}},@{Name=“Sku”; Expression={$_.StorageProfile.ImageReference.Sku}},@{Name=“OsType”; Expression={$_.StorageProfile.OsDisk.OsType}} |Format-Table


列出当前订阅下所有的ResourceGroup

Get-AzureRmResourceGroup select ResourceGroupName,Location, ProvisioningState


列出某个ResourceGroup中的资源

Get-AzureRmResource where {$_.ResourceGroupName.Equals(“TingCloud”)}select Name, Location, ResourceType, Kind, Sku ft


查看当前订阅下存储账号的情况

Get-AzureRmStorageAccount select StorageAccountName,ResourceGroupName, Location, Kind, @{Name=“Type”; Expression={$_.Sku.Tier}} ft


通过Get-AzureRmVMImagePublisher查看各个区域提供VM image的publisher


之后可以通过Get-AzureRmVMImageOffer,针对每个不同的publisher查看能提供哪些Offer,

Get-AzureRmVMImageOffer -Location “China East 2” -PublisherName “MicrosoftWindowsServer”


Get-AzureRmVMImageOffer -Location “China East 2” -PublisherName “OpenLogic”


查看区域内可用的VM镜像

Get-AzureRmVMImageSku -Location “China East 2” -PublisherName “MicrosoftWindowsServer” -Offer “WindowsServer”


Get-AzureRmVMImageSku -Location “China East 2” -PublisherName “OpenLogic” -Offer
 “CentOS”


导出所有可用VM镜像名称

param (


[parameter(Mandatory =
$false)]


$LocationName
=
“ChinaEast2”,


[parameter(Mandatory =
$false)]


$ExportTo
=
[Environment]::GetFolderPath(“Desktop”) +
“\”
+
$LocationName
+
“-VMImage-“
+ $(Get-Date
-Format
“yyyyMMdd-HHmmss”) +
“.csv”

)

function
Check-AzureRmLocation()

{


param

(


[string]$LocationName = $(throw
“Parameter missing: -LocationName LocationName”)

)


Write-Host
$(Get-Date) * Checking location $LocationName
-ForegroundColor
Cyan


$Location
=
Get-AzureRmLocation
|
Where-Object { $_.Location -eq $LocationName }


If (-not ($Location))

{


Write-Host
$(Get-Date) * The location”
$LocationName
“does not exist.”
-ForegroundColor
Red


return
$false

}


Else

{


Write-Host
$(Get-Date) * Location $LocationName exists”
-ForegroundColor
Cyan


return
$true

}

}

$LocationExist
=
Check-AzureRmLocation
-LocationName
$LocationName

if ($LocationExist
-eq
$true)

{


write-host
$(Get-Date) * Begin to collect VM Image information..Please wait”
-ForegroundColor
‘Cyan’


[pscustomobject[]]$VMImageObjects =
$null


if (Test-Path
$ExportTo)

{


Clear-Content
$ExportTo
-Force

}


$Error.clear()


try

{


$Publishers
= (Get-AzureRmVMImagePublisher
-Location
$LocationName
-ErrorAction
Stop).PublisherName


$TotalPublisherCounts
=
$Publishers.count


$PublisherCount
=
1


foreach ($Publisher
in
$Publishers)

{


Write-Progress
-Activity (“Current Publisher: $Publisher) -Status
“Searching $PublisherCount Publisher, Total Publisher: $TotalPublisherCounts
-PercentComplete ($PublisherCount/ $TotalPublisherCounts

*
100) -Id
1


$Offers
= (Get-AzureRmVMImageOffer
-Location
$LocationName
-PublisherName
$Publisher
-ErrorAction
Stop).offer


$TotalOfferCounts
=
$Offers.count


if ($TotalOfferCounts
-eq
0)

{


$PublisherCount++

}


else

{


$OfferCount
=
1


foreach ($Offer
in
$Offers)

{


Write-Progress
-Activity (“Current Offer: $Offer) -Status
“Searching $OfferCount Offer, Total Offer: $TotalOfferCounts
-PercentComplete ($OfferCount/ $TotalOfferCounts

*
100) -Id
2
-ParentId
1


$Skus
=
Get-AzureRmVMImageSku
-Location
$LocationName
-PublisherName
$Publisher
-Offer
$Offer
-ErrorAction
Stop


$TotalSkuCounts
=
$Skus.count


if ($TotalSkuCounts
-eq
0)

{


$OfferCount++

}


else

{


$SkuCount
=
1


foreach ($Sku
in
$Skus)

{


$VMImageObject
=
New-Object
-TypeName
psobject


$VMImageObject
|
Add-Member
-MemberType
NoteProperty
-Name
LocationName
-Value
$Sku.Location


$VMImageObject
|
Add-Member
-MemberType
NoteProperty
-Name
PublisherName
-Value
$Sku.PublisherName


$VMImageObject
|
Add-Member
-MemberType
NoteProperty
-Name
OfferName
-Value
$Sku.Offer


$VMImageObject
|
Add-Member
-MemberType
NoteProperty
-Name
SkuName
-Value
$Sku.skus


$VMImageObjects
+=
$VMImageObject


Write-Progress
-Activity (“Current Sku: $($Sku.skus)) -Status
“Searching $SkuCount Sku, Total Sku: $TotalSkuCounts
-PercentComplete ($SkuCount/ $TotalSkuCounts

*
100) -id
3
-ParentId
2


Start-Sleep
-Milliseconds
500


$SkuCount++

}


$OfferCount++

}

}


$PublisherCount++

}

}


if ($VMImageObjects
-ne
$null)

{


$VMImageObjects
|
Export-Csv
-LiteralPath
$ExportTo
-NoTypeInformation
-Force
-ErrorAction
Stop


Write-Host
$(Get-Date) * Export successfully, Please check $ExportTo
-ForegroundColor
Cyan

}


else

{


Write-Host
$(Get-Date) * Something wrong”
-ForegroundColor
Red

}

}


catch

{


Write-Warning
$Error[0].Exception.Message

}

}


最后会将文件输出到桌面,Azure VM Image列表如下

使用PowerShell 收集Azure VM Image信息 – 副本.ps1

本文固定链接: http://365vcloud.net/2018/07/11/using-powershell-to-get-azure-vm-image-information/ | Eric的学习之路

该日志由 TingXu 于2018年07月11日发表在 Microsoft Azure 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 使用PowerShell 收集Azure VM Image信息 | Eric的学习之路
关键字: