-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathazure-az-get-vmImages.ps1
137 lines (115 loc) · 4.36 KB
/
azure-az-get-vmImages.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<#
script to enumerate os versions from azure
iwr "https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-az-get-vmImages.ps1" -out "$pwd\azure-az-get-vmImages.ps1";
.\azure-az-get-vmImages.ps1
.\azure-az-get-vmImages.ps1 -location eastus -showDetail > c:\temp\output.json
#>
param(
[string]$location = "eastus",
[string]$vmSize = "Basic_A1",
[string]$publisher = "MicrosoftWindowsServer", #"Canonical"
[string]$offer = "WindowsServer", #"UbuntuServer"
[string]$imagesku = "2022-Datacenter", #"18.04-LTS"
[switch]$showDetail,
[switch]$listPublishers,
[switch]$listOffers,
[switch]$listSkus,
[switch]$listSizes
)
write-host "checking location $($location)"
$locations = Get-azLocation
if (!($locations | Where-Object Location -Like $location) -or !$location) {
$locations.Location
write-warning "location: $($location) not found. supply -location using one of the above locations and restart script."
return
}
if ($showDetail) {
$locations.location
}
write-host "checking publisher $($publisher)"
write-host "Get-azVMImagePublisher -Location $location"
$publishers = Get-azVMImagePublisher -Location $location
if (!($publishers | Where-Object PublisherName -Match $publisher)) {
$publishers
write-warning "publisher: $($publisher) not found. supply -location using one of the above locations and restart script."
return
}
$publisherName = ($publishers | Where-Object PublisherName -Match $publisher)[0].PublisherName
if ($showDetail) {
$publishers | Format-List *
}
if($listPublishers) {
$publishers | sort-object PublisherName | Format-Table -Property PublisherName
return
}
write-host "checking vm size $($vmSize) in $($location)"
write-host "Get-azVMSize -Location $location"
$vmSizes = Get-azVMSize -Location $location
if (!($vmSizes | Where-Object Name -Like $vmSize)) {
$vmSizes
write-warning "vmSize: $($vmSize) not found in $($location). correct -vmSize using one of the above options and restart script."
return
}
if ($showDetail) {
$vmSizes | Format-List *
}
if($listSizes) {
$vmSizes | Format-Table -Property Name
return
}
write-host "checking offer $($offer)"
write-host "Get-azVMImageOffer -Location $location -PublisherName $publisherName"
$offers = Get-azVMImageOffer -Location $location -PublisherName $publisherName
if (!($offers | Where-Object Offer -Like $offer)) {
$offers
write-warning "offer: $($offer) not found in $($location). correct -offer using one of the above options and restart script."
return
}
if ($showDetail) {
$offers | sort-object Offer | Format-Table -Property Offer
}
if($listOffers) {
$offers | Format-Table -Property Offer
return
}
write-host "checking sku $($publisherName) $($offer) $($imageSku)"
write-host "Get-azVMImageSku -Location $location -PublisherName $publisherName -Offer $offer"
if ($showDetail) {
$skus = Get-azVMImageSku -Location $location -PublisherName $publisherName -Offer $offer
}
else {
$skus = Get-azVMImageSku -Location $location -PublisherName $publisherName -Offer $offer | Where-Object Skus -Like $imageSKU
}
if (!($skus | Where-Object Skus -Like $imageSKU)) {
$skus
write-warning "image sku: $($imageSku) not found in $($location). correct -imageSKU using one of the above options and restart script."
return
}
if ($showDetail) {
$skus | Format-List *
}
if($listSkus) {
$skus | Format-Table -Property Skus
return
}
foreach ($sku in $skus) {
write-host "checking sku image $($publisherName) $($offer) $($imageSku) $($sku.skus)"
write-host "Get-azVMImage -Location $location -PublisherName $publisherName -Offer $offer -skus $($sku.skus)" -ForegroundColor Cyan
$imageSkus = Get-azVMImage -Location $location -PublisherName $publisherName -Offer $offer -skus $sku.skus
$orderedSkus = [collections.generic.list[version]]::new()
$orderedList = [collections.arraylist]::new()
foreach ($image in $imageSkus) {
[void]$orderedSkus.Add([version]::new($image.Version))
}
$orderedSkus = $orderedSkus | Sort-Object
foreach ($orderedSku in $orderedSkus) {
$sku = $imageSkus | where-object Version -ieq $orderedSku.ToString()
[void]$orderedList.Add($sku)
}
if ($showDetail) {
$orderedList | Format-List *
}
else {
$orderedList | format-table -Property Version, Skus, Offer, PublisherName
}
}