比较Powershell中的System.Version

Tom*_*mek 3 powershell

我有一种情况,我必须根据比较版本在脚本中做出决定。请考虑以下示例:

PS C:\>
[version]$SomeVersion='1.1.1'
[version]$OtherVersion='1.1.1.0'

PS C:\> $SomeVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
1      1      1      -1      

PS C:\> $OtherVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
1      1      1      0       

PS C:\>$SomeVersion -ge $OtherVersion
False
Run Code Online (Sandbox Code Playgroud)

比较类型为System.Version的对象时,我想省略修订版,但
我找不到任何明智的方法。
有没有?

注意-我已经尝试做过:

PS C:\> ($scriptversion |select major,minor,build) -gt ($currentVersion|select major,minor,build)

Cannot compare "@{Major=1; Minor=1; Build=1}" to "@{Major=1; Minor=1; 
Build=1}" because the objects are not the same type or the object "@{Major=1; 
Minor=1; Build=1}" does not implement "IComparable".
At line:1 char:1
+ ($scriptversion |select major,minor,build) -gt ($currentVersion |sele ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : PSObjectCompareTo
Run Code Online (Sandbox Code Playgroud)

当我尝试用0覆盖修订版本号时,它表示它是只读属性。

Bac*_*its 5

使用三个参数System.Version构造函数创建具有相关属性的新实例:

[Version]::new($scriptversion.Major,$scriptversion.Minor,$scriptversion.Build) -gt [Version]::new($currentVersion.Major,$currentVersion.Minor,$currentVersion.Build)
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用以下详细方式New-Object

$NormalizedScriptVersion = New-Object -TypeName System.Version -ArgumentList $scriptversion.Major,$scriptversion.Minor,$scriptversion.Build
$NormalizedCurrentVersion = New-Object -TypeName System.Version -ArgumentList $currentVersion.Major,$currentVersion.Minor,$currentVersion.Build

$NormalizedScriptVersion -gt $NormalizedCurrentVersion 
Run Code Online (Sandbox Code Playgroud)

使用任何您认为更可维护的。