英文:
how to convert three decimal into 2 decimal of versions list in a file using powershell
问题
我有一个 PowerShell 脚本,它正在收集虚拟机镜像版本列表,并按降序排序,以获得最高的值:
$imagegallery = Get-AzGallery -ResourceGroupName rg-shared-inva-eastus
Write-Output $imagegallery.Name
$imagegallerydefinitioninfo = @("PeriscopeMQ","PeriscopeApp","PeriscopeWeb")
foreach ($imagedefinition in $imagegallerydefinitioninfo) {
$imagegalleryimageversion = Get-AzGalleryImageVersion -GalleryName $imagegallery.Name -ResourceGroupName $imagegallery.ResourceGroupName -GalleryImageDefinitionName $imagedefinition
Write-Output $imagegalleryimageversion.Tags.PeriscopeVmImageVersion | Sort-Object -Descending { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(2)}) }
}
输出是:
5.11.4
5.11.3
5.11.2
5.11.1
5.11.0
5.10.56
5.10.55
5.10.9
5.10.8
5.10.7
5.10.6
5.9.98
5.9.97
5.9.96
5.9.93
5.8.90
我想将其转换为带有两位小数的格式,如下:
5.11
5.10
5.9
5.8
并找到最高版本,即 5.11
,跳过 02
,并删除第三个版本。
有人能帮忙吗?
谢谢!
英文:
I have a PowerShell script which is collecting vm image version lists and sorting into descending order with the highest value:
$imagegallery = Get-AzGallery -ResourceGroupName rg-shared-inva-eastus
Write-Output $imagegallery.Name
$imagegallerydefinitioninfo = @("PeriscopeMQ","PeriscopeApp","PeriscopeWeb")
foreach ($imagedefinition in $imagegallerydefinitioninfo) {
$imagegalleryimageversion = Get-AzGalleryImageVersion -GalleryName $imagegallery.Name -ResourceGroupName $imagegallery.ResourceGroupName -GalleryImageDefinitionName $imagedefinition
Write-Output $imagegalleryimageversion.Tags.PeriscopeVmImageVersion |Sort-Object -Descending { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(2)}) }
}
Output is :
5.11.4
5.11.3
5.11.2
5.11.1
5.11.0
5.10.56
5.10.55
5.10.9
5.10.8
5.10.7
5.10.6
5.9.98
5.9.97
5.9.96
5.9.93
5.8.90
I want to convert this into 2 places decimals like:
5.11
5.10
5.9
5.8
and, find the highest version which is 5.11
and skip the 02
, and delete the third version.
Can anyone please help me with this?
Thanks
答案1
得分: 2
使用.ToString(int fieldCount)
重载方法来仅打印 [version]
(System.Version
) 实例的前两个字段 (.Major
和 .Minor
);一个简单的例子:
# -> '1.2'
([version] '1.2.3.4').ToString(2)
请注意,您可以按原样对 [version]
实例进行排序,这使您可以将 .ToString()
调用应用于您的 Sort-Object
调用的结果;如果 .PeriscopeVmImageVersion
属性值是版本字符串而不是 [version]
实例,您可以将其转换为 [version[]]
:
[version[]] $imagegalleryimageversion.Tags.PeriscopeVmImageVersion |
Sort-Object -Descending |
ForEach-Object ToString 2 |
Get-Unique
注:
ForEach-Object ToString 2
是 简化的语法,等同于ForEach-Object { $_.ToString(2) }
Write-Output
故意 未被使用;该命令依赖于 PowerShell 在第一个管道段中的 隐式输出行为,这既更简洁又更高效 (并且不更改语法要求)。有关更多信息,请参阅 此答案。
(很少使用的)Get-Unique
命令用于仅输出共享相同主要和次要值的版本的 一个 字符串。
要在 所有循环迭代(在所有图像库定义上)应用排序和格式化,使用以下方法(请注意切换到 ForEach-Object
并使用自动的 $_
变量来引用当前管道输入对象):
$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
要满足您的最终要求,我理解为:
-
在每组共享相同主要和次要字段值的版本号中,找到 最高 版本。
-
从代表最高版本号的两个组中,移除每个组的最高版本,代表要 保留 的图像。
-
以降序输出其余版本。
$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 # ... 随后按需要进行处理。
-
Group-Object
用于按共享的主要和次要字段值 分组 版本号。 -
For-EachObject ToString
将[version]
对象打印为包含 所有 字段的字符串,以便您可以验证是否报告了正确的版本;您可能希望原样捕获[version]
输出,以便在 VM 移除命令中使用它。
使用您提供的示例输入,这实际上从输入中删除了以下两个版本号:5.11.4
和 5.10.56
。
英文:
<!-- language-all: sh -->
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
<sup>Note: <br>* ForEach-Object ToString 2
is simplified syntax for: ForEach-Object { $_.ToString(2) }
<br>* 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.</sup>
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.<sup>Tip of the hat to Andrew Morton.</sup>
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论