-
Notifications
You must be signed in to change notification settings - Fork 17
/
azure-az-image-build-match.ps1
141 lines (113 loc) · 4.83 KB
/
azure-az-image-build-match.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
138
139
140
141
<#
.SYNOPSIS
powershell script to match local / given windows image build number with matching azure image skus
.LINK
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-az-image-build-match.ps1" -outFile "$pwd\azure-az-image-build-match.ps1";
.\azure-az-image-build-match.ps1 -location {{ location }} -build {{ build }}
.DESCRIPTION
.NOTES
File Name : azure-az-image-build-match.ps1
Author : jagilber
Version : 200801
History :
.EXAMPLE
.\azure-az-image-build-match.ps1
query local machine for build number and use defaults for location, publisher, and imagesku
.EXAMPLE
.\azure-az-image-build-match.ps1 -location eastus
query local machine for build number using location eastus and use defaults for publisher, and imagesku
.EXAMPLE
.\azure-az-image-build-match.ps1 -build 2004
use build number 2004 and use defaults for location, publisher, and imagesku
#>
[cmdletbinding()]
param(
[int]$build , #= 1803, #= 1903, #= 2004,
[string]$location = "westus",
[string]$publisher = "MicrosoftWindowsServer", #"Canonical"
[string]$offerName = "WindowsServer", #"UbuntuServer"
[string]$imagesku = "2022-Datacenter" #"2016-Datacenter-with-containers" #"18.04-LTS"
)
$error.Clear()
set-strictMode -Version 3.0
$PSModuleAutoLoadingPreference = 2
$ErrorActionPreference = 'continue'
function main() {
if (!(connect-az)) { return }
if (!$build) {
$build = [convert]::toint32((Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId)
write-host "using local machine build $build"
}
enumerate-containerRegistry $build
$skus = $null
foreach ($offerInfo in (get-azvmimageoffer -Location $location -PublisherName $publisher | ? Offer -imatch $offerName)) {
$offer = $offerInfo.Offer
write-host "checking sku $($publisher) $($offer) $($imageSku)" -ForegroundColor Magenta
write-host "Get-azVMImageSku -Location $location -PublisherName $publisher -Offer $offer"
$skus += Get-azVMImageSku -Location $location -PublisherName $publisher -Offer $offer
Write-Verbose "all skus:"
foreach ($sku in $skus) {
Write-Verbose ($sku.Id)
}
write-host "filtered skus:" -ForegroundColor Yellow
foreach ($sku in $skus) {
if ($sku.Id -match "(^|\D)$build(\D|$)" -or $sku.Id -imatch $imagesku) {
Write-Verbose ($sku | fl * | out-string)
$image = Get-AzVMImage -Location $location -PublisherName $publisher -Offer $offer -Skus $sku.Skus -ErrorAction SilentlyContinue
if ($image) {
write-host "Get-AzVMImage -Location $location -PublisherName $publisher -Offer $offer -Skus $($sku.Skus)"
write-host "image: $($image | fl * | out-string)" -ForegroundColor Magenta
}
else {
Write-Verbose "no image"
}
}
else {
Write-Verbose "$($sku | convertto-json) does not match $build. skipping"
}
}
}
}
function connect-az() {
$moduleList = @('az.accounts','az.compute')
foreach($module in $moduleList) {
write-host "checking module $module" -ForegroundColor Yellow
if(!(get-module -name $module -listavailable)) {
write-host "installing module $module" -ForegroundColor Yellow
install-module $module -force
import-module $module
if(!(get-module -name $module -listavailable)) {
return $false
}
}
}
if(!(@(Get-AzResourceGroup).Count)) {
$error.clear()
Connect-AzAccount
if ($error -and ($error | out-string) -match '0x8007007E') {
$error.Clear()
Connect-AzAccount -UseDeviceAuthentication
}
}
return $null = get-azcontext
}
function enumerate-containerRegistry($build) {
$mcrRepositories = Invoke-RestMethod 'https://mcr.microsoft.com/v2/_catalog'
write-verbose "mcr repositories: $($mcrRepositories.Repositories | fl * | out-string)"
write-verbose "dotnet repositories: $($mcrRepositories.Repositories -match 'dotnet' | fl * | out-string)"
$serverRepos = $mcrRepositories.Repositories -match 'windows.+server'
write-verbose "server repositories: $($serverRepos | fl * | out-string)"
foreach($serverRepo in $serverRepos) {
write-host "repo tags for repo: $serverRepo" -ForegroundColor Cyan
$repoTags = Invoke-RestMethod "https://mcr.microsoft.com/v2/$serverRepo/tags/list"
foreach($tag in $repoTags.tags){
if($tag -match "(^|\D)$build(\D|$)") {
write-host "`t$tag"
}
else {
write-verbose "`t$tag"
}
}
}
}
main