Skip to content

Commit 4d3e825

Browse files
authored
Retrieve Hyper-V Generation (of Azure VMs) (AutomatedLab#1647)
* Retrieve Hyper-V Generation * Improve info if cache is not available * Use switch instead of dictionary to get Azure AL OS Name * Ensure AzureImageName is used correctly * Move logic to AzOsImg * Validate generation * Assume correct imageRef with new generation prop * Move Az img version to getter * Counting nothing
1 parent df6f4e4 commit 4d3e825

File tree

8 files changed

+411
-156
lines changed

8 files changed

+411
-156
lines changed

AutomatedLabCore/functions/Azure/Add-LabAzureSubscription.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ Get/Set/Register/Unregister-PSFConfig -Module AutomatedLab -Name AutoSyncLabSour
420420
}
421421
catch
422422
{
423-
Write-ScreenInfo 'Querying available operating system images from Azure'
423+
Write-ScreenInfo 'No OS cache available. Querying available operating system images from Azure. This takes some time, as we need to query for the VM Generation as well. The information will be cached.'
424424
$global:cacheVmImages = Get-LabAzureAvailableSku -Location $DefaultLocationName
425425
$vmImages = $global:cacheVmImages
426426
}

AutomatedLabCore/functions/Azure/Get-LabAzureAvailableSku.ps1

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
}
2424
$publishers = Get-AzVMImagePublisher -Location $azLocation.Location
2525

26+
# For each publisher, get latest image AND REQUEST THAT SPECIFIC IMAGE to be able to see the Hyper-V generation. Marvelous.
2627
$publishers |
2728
Where-Object PublisherName -eq 'MicrosoftWindowsServer' |
2829
Get-AzVMImageOffer |
2930
Get-AzVMImageSku |
3031
Get-AzVMImage |
3132
Group-Object -Property Skus, Offer |
32-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
33+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
3334

3435
# Linux
3536
# Ubuntu - official
@@ -41,7 +42,7 @@
4142
Where-Object Skus -notmatch 'arm64' |
4243
Get-AzVMImage |
4344
Group-Object -Property Skus, Offer |
44-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
45+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
4546
# RedHat - official
4647
$publishers |
4748
Where-Object PublisherName -eq 'RedHat' |
@@ -51,7 +52,7 @@
5152
Where-Object Skus -notmatch '(RAW|LVM|CI)' |
5253
Get-AzVMImage |
5354
Group-Object -Property Skus, Offer |
54-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
55+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
5556
# CentOS - Roguewave, sounds slightly suspicious
5657
$publishers |
5758
Where-Object PublisherName -eq 'OpenLogic' |
@@ -60,15 +61,15 @@
6061
Get-AzVMImageSku |
6162
Get-AzVMImage |
6263
Group-Object -Property Skus, Offer |
63-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
64+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
6465
# Kali
6566
$publishers |
6667
Where-Object PublisherName -eq 'Kali-Linux' |
6768
Get-AzVMImageOffer |
6869
Get-AzVMImageSku |
6970
Get-AzVMImage |
7071
Group-Object -Property Skus, Offer |
71-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
72+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
7273

7374
# Desktop
7475
$publishers |
@@ -77,7 +78,7 @@
7778
Get-AzVMImageSku |
7879
Get-AzVMImage |
7980
Group-Object -Property Skus, Offer |
80-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
81+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
8182

8283
# SQL
8384
$publishers |
@@ -87,7 +88,7 @@
8788
Get-AzVMImage |
8889
Where-Object Skus -in 'Standard','Enterprise' |
8990
Group-Object -Property Skus, Offer |
90-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
91+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
9192

9293
# VisualStudio
9394
$publishers |
@@ -97,7 +98,7 @@
9798
Get-AzVMImage |
9899
Where-Object Offer -eq 'VisualStudio' |
99100
Group-Object -Property Skus, Offer |
100-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
101+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
101102

102103
# Client OS
103104
$publishers |
@@ -107,7 +108,7 @@
107108
Get-AzVMImage |
108109
Where-Object Offer -eq 'Windows' |
109110
Group-Object -Property Skus, Offer |
110-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
111+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
111112

112113
# Sharepoint 2013 and 2016
113114
$publishers |
@@ -117,5 +118,5 @@
117118
Get-AzVMImage |
118119
Where-Object Offer -eq 'MicrosoftSharePointServer' |
119120
Group-Object -Property Skus, Offer |
120-
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 }
121+
ForEach-Object { $_.Group | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Get-AzVmImage }
121122
}

AutomatedLabCore/functions/Core/Get-LabAvailableOperatingSystem.ps1

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@
6060
$cachedOsList = New-Object $type
6161
foreach ($os in $cachedSkus)
6262
{
63-
# Converting ToLower() as Azure Stack Hub images seem to mix case
64-
# building longer SKU to take care of bad naming conventions with the linux images
65-
$osname = '{0}_{1}' -f $os.Skus, $os.PublisherName
66-
$cachedOs = [AutomatedLab.OperatingSystem]::new($osname.ToLower(), $true)
63+
$cachedOs = [AutomatedLab.OperatingSystem]::new($os.AutomatedLabOperatingSystemName)
6764
if ($cachedOs.OperatingSystemName) {$cachedOsList.Add($cachedOs)}
6865
}
6966

@@ -78,9 +75,7 @@
7875

7976
foreach ($sku in $skus)
8077
{
81-
# Converting ToLower() as Azure Stack Hub images seem to mix case
82-
$osname = '{0}_{1}' -f $sku.Skus, $sku.PublisherName
83-
$azureOs = [AutomatedLab.OperatingSystem]::new($osname.ToLower(), $true)
78+
$azureOs = [AutomatedLab.OperatingSystem]::new($sku.AutomatedLabOperatingSystemName)
8479
if (-not $azureOs.OperatingSystemName) { continue }
8580

8681
$osList.Add($azureOs )

AutomatedLabWorker/functions/AzureWorkerVirtualMachines/Get-LWAzureSku.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,26 @@
124124
}
125125
else
126126
{
127-
$vmImageName = (New-Object AutomatedLab.OperatingSystem($machine.OperatingSystem)).AzureImageName
128-
if (-not $vmImageName)
127+
$vmImage = $lab.AzureSettings.VmImages |
128+
Where-Object { $_.AutomatedLabOperatingSystemName -eq $Machine.OperatingSystem.OperatingSystemName -and $_.HyperVGeneration -eq "V$($Machine.VmGeneration)" } |
129+
Select-Object -First 1
130+
131+
if (-not $vmImage)
129132
{
130133
throw "There is no Azure VM image for the operating system '$($Machine.OperatingSystem)'. The machine cannot be created. Cancelling lab setup."
131134
}
132135

133-
$vmImage = $lab.AzureSettings.VmImages |
134-
Where-Object { "$($_.Skus)_$($_.PublisherName)" -eq $vmImageName } |
135-
Select-Object -First 1
136-
137-
$offerName = $vmImageName = ($vmImage).Offer
136+
$offerName = ($vmImage).Offer
138137
$publisherName = ($vmImage).PublisherName
139138
$skusName = ($vmImage).Skus
139+
$version = $vmImage.Version
140140
}
141141

142142
Write-PSFMessage -Message "We selected the SKUs $skusName from offer $offerName by publisher $publisherName"
143143
@{
144144
offer = $offerName
145145
publisher = $publisherName
146146
sku = $skusName
147-
version = 'latest'
147+
version = if ($version) { $version } else { 'latest' }
148148
}
149149
}

AutomatedLabWorker/functions/AzureWorkerVirtualMachines/New-LabAzureResourceGroupDeployment.ps1

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -670,31 +670,13 @@
670670
$vmSize = Get-LWAzureVmSize -Machine $Machine
671671
$imageRef = Get-LWAzureSku -Machine $machine
672672

673-
if (($Machine.VmGeneration -eq 2 -and $vmSize.Gen2Supported) -or ($vmSize.Gen2Supported -and -not $vmSize.Gen1Supported))
674-
{
675-
$pattern = '{0}(-g2$|gen2|-gensecond$)' -f $imageRef.sku # Yes, why should the image names be consistent? Also of course we don't need a damn VMGeneration property...
676-
$newImage = $lab.AzureSettings.VMImages | Where-Object { $_.PublisherName -eq $imageref.Publisher -and $_.Offer -eq $imageref.Offer -and $_.Skus -match $pattern }
677-
if (-not $newImage)
678-
{
679-
throw "Selected VM size $vmSize for $Machine only suppports G2 VMs, however no matching Generation 2 image was found for your selection: Publisher $($imageRef.publisher), offer $($imageRef.offer), sku $($imageRef.sku)!"
680-
}
681-
682-
$imageRef = @{
683-
publisher = $newImage.PublisherName
684-
version = $newImage.Version
685-
offer = $newImage.Offer
686-
sku = $newImage.Skus
687-
}
688-
}
689-
690673
if (-not $vmSize)
691674
{
692675
throw "No valid VM size found for '$Machine'. For a list of available role sizes, use the command 'Get-LabAzureAvailableRoleSize -LocationName $($lab.AzureSettings.DefaultLocation.Location)'"
693676
}
694677

695678
Write-ScreenInfo -Type Verbose -Message "Adding $Machine with size $vmSize, publisher $($imageRef.publisher), offer $($imageRef.offer), sku $($imageRef.sku)!"
696679

697-
$machNet = Get-LabVirtualNetworkDefinition -Name $machine.Network[0]
698680
$machTemplate = @{
699681
name = $machine.ResourceName
700682
tags = @{

0 commit comments

Comments
 (0)