分类 PowerShell 版本

23
在PowerShell中,如果我有一个包含版本信息的字符串列表,如“3.0.1.1”,“3.2.1.1”等,我应该如何对其进行排序,以便与C#中的System.Version相同?
5个回答

33
PS C:\> $ver="3.0.1.1","3.2.1.1"
PS C:\> $ver|%{[System.Version]$_}|sort

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      1      1
3      2      1      1

12

将其转换为版本并按此方式排序:

$list = "3.0.1.1","3.2.1.1" 
$sorted = $list | %{ new-object System.Version ($_) } | sort

7
一个版本字符串可以被转换成一个版本对象,sort-object 可以传递一个脚本块并对结果进行排序。
PS C:\Users\me> "3.11.0.1", "3.0.1.1", "3.2.1.1" | sort
3.0.1.1
3.11.0.1
3.2.1.1

PS C:\Users\me> "3.11.0.1", "3.0.1.1", "3.2.1.1" | sort {[version] $_}
3.0.1.1
3.2.1.1
3.11.0.1

(增加了额外的版本字符串,以使示例更有意义。)

2
# I needed to sort historical versions (Octopus) with varying decimal formats.
# Try # this (it is easy to add to a more complex expression sort)
# Special Case "3.00.1.10.1.10" and "3.0.1.10.1.10" required the double sort
# to work correctly
    $vers = @()`enter code here`
    $vers +=  @( "3.1.60",      "3.1.52","3.1.51")
    $vers +=  @( "3.00.46",     "3.00.36","3.50.2145.11")
    $vers +=  @( "3.50.2145.10","3.50.2145.9")
    $vers +=  @( "3.50.2145.8", "3.50.2145.7")
    $vers +=  @( "3.50.2145.6", "3.50.2145.5")
    $vers +=  @( "3.50.2145.4", "3.50.2145.3")
    $vers +=  @( "3.50.2145.2", "3.50.2145.1")
    $vers +=  @( "3.50.2145",   "3.50.2143")
    $vers +=  @( "3.50.2141",    "3.50.2135")    
    $vers +=  @( "3.0.1.10.1.1", "3.00.1.10.1.10")
    $vers +=  @( "2.1.3.4",      "3.0","3.")
    $vers +=  @( "3.0.1.10.1.100","3.0.1.10.1.10")
    $mySortAsc = @{Expression={ [regex]::Replace($_ ,'\d+', { $args[0].Value.PadLeft(20,'0') }) };Descending=$false}
    $mySortDesc = @{Expression={ [regex]::Replace($_ ,'\d+', { $args[0].Value.PadLeft(20,'0') }) };Descending=$true}    
    $nl = [Environment]::NewLine
    Write-Output ($nl + "Ascending Sort" + $nl);
    $vers | Sort-Object | Sort-Object $mySortAsc
    Write-Output ($nl + "Descending Sort" + $nl);
    $vers | Sort-Object -Descending | Sort-Object $mySortDesc
<# Result
Ascending Sort

2.1.3.4
3.
3.0
3.0.1.10.1.1
3.0.1.10.1.10
3.00.1.10.1.10
3.0.1.10.1.100
3.00.36
3.00.46
3.1.51
3.1.52
3.1.60
3.50.2135
3.50.2141
3.50.2143
3.50.2145
3.50.2145.1
3.50.2145.2
3.50.2145.3
3.50.2145.4
3.50.2145.5
3.50.2145.6
3.50.2145.7
3.50.2145.8
3.50.2145.9
3.50.2145.10
3.50.2145.11

Descending Sort

3.50.2145.11
3.50.2145.10
3.50.2145.9
3.50.2145.8
3.50.2145.7
3.50.2145.6
3.50.2145.5
3.50.2145.4
3.50.2145.3
3.50.2145.2
3.50.2145.1
3.50.2145
3.50.2143
3.50.2141
3.50.2135
3.1.60
3.1.52
3.1.51
3.00.46
3.00.36
3.0.1.10.1.100
3.00.1.10.1.10
3.0.1.10.1.10
3.0.1.10.1.1
3.0
3.
2.1.3.4
#>

2
虽然这段代码可能回答了问题,但提供关于它如何以及为什么解决问题的额外上下文会提高答案的长期价值。 - Alexander
这个非常好用!我的版本名称中包含数字前面的“V”,因此我没有适当的System.Versions。但是正则表达式似乎不在意这一点。太棒了! - Master Azazel

1
只是为了补充另一种情况:PowerShell将这种单个数字版本“2”视为无效。必须在末尾添加“.0”以创建版本对象进行排序:
if($version  -match '^\d$')
{
  $version = $version + '.0'
}
New-Object System.Version $version

.NET System.Version类声明版本号由两到四个组件组成:major.minor[.build[.revision]]。 - Major Malfunction

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接