Use the .ToString(int fieldCount)
overload to print [version]
(System.Version
) instances only by their first two fields (.Major
and .Minor
); a simple example:
# -> '1.2'
([version] '1.2.3.4').ToString(2)
Note that you can sort [version]
instances as-is, which allows you to apply the .ToString()
call to the result of your Sort-Object
call; if the .PeriscopeVmImageVersion
property values are version strings rather than [version]
instances, you can cast to [version[]]
:
[version[]] $imagegalleryimageversion.Tags.PeriscopeVmImageVersion |
Sort-Object -Descending |
ForEach-Object ToString 2 |
Get-Unique
Note:
* ForEach-Object ToString 2
is simplified syntax for: ForEach-Object { $_.ToString(2) }
* Write-Output
is intentionally not used; the command relies on PowerShell's implicit output behavior in the first pipeline segment, which is both more concise and efficient (and doesn't change the syntax requirements). For background information, see this answer.
The (rarely used) Get-Unique
cmdlet is used to output only one string for each group of versions sharing the same major and minor values.Tip of the hat to Andrew Morton.
To apply the sorting and formatting across all loop iterations (across all image-gallery definitions), use the following (note the switch to ForEach-Object
and the use of the automatic $_
variable to refer to the pipeline input object at hand:
$imagegallerydefinitioninfo |
ForEach-Object {
$imagegalleryimageversion = Get-AzGalleryImageVersion -GalleryName $imagegallery.Name -ResourceGroupName $imagegallery.ResourceGroupName -GalleryImageDefinitionName $_
[version[]] $imagegalleryimageversion.Tags.PeriscopeVmImageVersion
} |
Sort-Object -Descending |
ForEach-Object ToString 2 |
Get-Unique
To address your final requirement, which I understand to be the following:
In each group of version numbers sharing the same major and minor field values, find the highest one.
From the 2 groups representing the highest version numbers overall, remove the highest version each, representing the images to keep.
Output the remaining versions in descending order.
$groupIndex = 0
$imagegallerydefinitioninfo |
ForEach-Object {
$imagegalleryimageversion = Get-AzGalleryImageVersion -GalleryName $imagegallery.Name -ResourceGroupName $imagegallery.ResourceGroupName -GalleryImageDefinitionName $_
[version[]] $imagegalleryimageversion.Tags.PeriscopeVmImageVersion
} |
Group-Object -Property Major, Minor |
Sort-Object -Descending { $_.Group[0] } |
ForEach-Object {
if (++$groupIndex -le 2) {
$_.Group | Sort-Object -Descending | Select-Object -Skip 1
} else {
$_.Group | Sort-Object -Descending
}
} |
ForEach-Object ToString # ... process further as needed.
Group-Object
is used to group the version numbers by shared major and minor field values.
For-EachObject ToString
prints the [version]
objects as strings with all fields, so you can verify that the correct versions are reported; you may want to capture the [version]
output as-is, so you can use it in a VM removal command.
With your sample input, this in effect eliminates the following two version numbers from the input: 5.11.4
and 5.10.56
.