-
Notifications
You must be signed in to change notification settings - Fork 17
/
azure-az-get-vmssImages.ps1
160 lines (134 loc) · 6.35 KB
/
azure-az-get-vmssImages.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<#
.LINK
[net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-az-get-vmssImages.ps1" -outFile "$pwd/azure-az-get-vmssImages.ps1";
./azure-az-get-vmssImages.ps1 -resourceGroupName <resource group name> -nodeTypeName <node type name>
#>
param(
[Parameter(Mandatory = $true)]
[string]$resourceGroupName,
[string]$clusterName = $resourceGroupName,
[string]$nodeTypeName,
[string]$publisherName = 'MicrosoftWindowsServer',
[string]$offer = 'WindowsServer',
[string]$sku,
[int]$instanceId = -1
)
function main() {
if (!(check-module)) { return }
$targetImageReference = $latestVersion = [version]::new(0, 0, 0, 0)
$isLatest = $false
$versionsBack = 0
$location = (Get-AzResourceGroup -Name $resourceGroupName).Location
$cluster = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.ServiceFabric/clusters -ResourceName $clusterName -ExpandProperties -ErrorAction SilentlyContinue
if (!$cluster) {
write-host "checking for managed cluster"
$cluster = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.ServiceFabric/managedclusters -ResourceName $clusterName -ExpandProperties
$resourceGroupName = "SFC_$($cluster.Properties.clusterid)"
$nodeTypes = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Compute/virtualMachineScaleSets -ExpandProperties
$cluster.Properties | Add-Member -MemberType NoteProperty -Name 'nodeTypes' -Value $nodeTypes
}
if (!$cluster) {
write-error "cluster not found. specify -clusterName`r`n$($error | out-string)"
exit
}
if (!$nodeTypeName) {
write-host "node type name not specified. using first node type name: $($cluster.Properties.nodeTypes[0].name)" -ForegroundColor Yellow
$nodeTypeName = $cluster.Properties.nodeTypes[0].name
}
$vmssHistory = @(Get-AzVmss -ResourceGroupName $resourceGroupName -Name $nodeTypeName -OSUpgradeHistory)[0]
if ($vmssHistory) {
$targetImageReference = $vmssHistory.Properties.TargetImageReference
}
else {
write-host "vmssHistory not found. checking current image reference"
$vmssHistory = Get-AzVmss -ResourceGroupName $resourceGroupName -Name $nodeTypeName
$targetImageReference = $vmssHistory.VirtualMachineProfile.StorageProfile.ImageReference
}
$targetImageReference = $targetImageReference | convertto-json | convertfrom-json
if (!$targetImageReference -or $instanceId -gt -1) {
write-host "checking instance $instanceId"
$vmssVmInstance = get-azvmssvm -ResourceGroupName $resourceGroupName -VMScaleSetName $nodeTypeName -InstanceId $instanceId
$targetImageReference = $vmssVmInstance.StorageProfile.ImageReference
}
elseif (!$targetImageReference.ExactVersion) {
if ($instanceId -lt 0) { $instanceId = 0 }
write-host "targetImageReference ExactVersion not found. checking instance $instanceId"
$vmssVmInstance = get-azvmssvm -ResourceGroupName $resourceGroupName -VMScaleSetName $nodeTypeName -InstanceId $instanceId
$targetImageReference.ExactVersion = @($vmssVmInstance.StorageProfile.ImageReference.ExactVersion)[0]
}
if (!$targetImageReference) {
write-error "current vm image version not found."
#return
}
else {
write-host "current running image on node type: " -ForegroundColor Green
$targetImageReference
$publisherName = $targetImageReference.Publisher
$offer = $targetImageReference.Offer
$sku = $targetImageReference.Sku
$runningVersion = ($targetImageReference.ExactVersion, $targetImageReference.Version | select-object -first 1)
if ($runningVersion -ieq 'latest') {
write-host "running version is 'latest'"
$isLatest = $true
$runningVersion = [version]::new(0, 0, 0, 0)
}
}
write-host "Get-AzVmImage -Location $location -PublisherName $publisherName -offer $offer -sku $sku" -ForegroundColor Cyan
$imageSkus = Get-AzVmImage -Location $location -PublisherName $publisherName -offer $offer -sku $sku
$orderedSkus = [collections.generic.list[version]]::new()
foreach ($image in $imageSkus) {
[void]$orderedSkus.Add([version]::new($image.Version))
}
$orderedSkus = $orderedSkus | Sort-Object
write-host "available versions: " -ForegroundColor Green
$orderedSkus.foreach{ $psitem.ToString() }
foreach ($sku in $orderedSkus) {
if ([version]$sku -gt [version]$runningVersion) { $versionsBack++ }
if ([version]$latestVersion -lt [version]$sku) { $latestVersion = $sku }
}
write-host
if ($isLatest) {
write-host "published latest version: $latestVersion running version: 'latest'" -ForegroundColor Cyan
}
elseif ($versionsBack -gt 1) {
write-host "published latest version: $latestVersion is $versionsBack versions newer than current running version: $runningVersion" -ForegroundColor Red
}
elseif ($versionsBack -eq 1) {
write-host "published latest version: $latestVersion is one version newer than current running version: $runningVersion" -ForegroundColor Yellow
}
else {
write-host "current running version: $runningVersion is same or newer than published latest version: $latestVersion" -ForegroundColor Green
}
}
function check-module() {
$error.clear()
get-command Connect-AzAccount -ErrorAction SilentlyContinue
if ($error) {
$error.clear()
write-warning "azure module for Connect-AzAccount not installed."
if ((read-host "is it ok to install latest azure az module?[y|n]") -imatch "y") {
$error.clear()
install-module az.accounts
install-module az.compute
install-module az.resources
import-module az.accounts
import-module az.compute
import-module az.resources
}
else {
return $false
}
if ($error) {
return $false
}
}
if(!(get-azResourceGroup)){
Connect-AzAccount
}
if(!@(get-azResourceGroup).Count -gt 0){
return $false
}
return $true
}
main