使用if条件在PowerShell中进行比较

时间:2018-06-15 03:03:41

标签: powershell powershell-v3.0

我试图从系统中获取.Net版本并尝试检查我的系统是否具有大于或等于4.6的.Net版本。下面是代码。从代码我能够检索发布版本。我在if条件下面临问题。

$myArray = @()

$myArray = @(Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
    Get-ItemProperty -Name Release,Release -EA 0 |
    Where { $_.PSChildName -match '^(?!S)\p{L}' } |
    Select Release)
Write-Output $myArray[0]
Write-Output $myArray[1]

$var1 = 393295
foreach ($var in $myArray) {
    Write-Output $var
    if ($var -ge $var1) {
        Write-Output same
    } else {
        Write-Output NOT same
    }
}

我收到此错误:

Cannot compare "@{Release=461808}" to "393295" because the objects are not the
same type or the object "@{Release=461808}" does not implement "IComparable".
At C:\Users\Desktop\DotNet.ps1:13 char:9
+     if ($var -ge $var1) {
+         ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : PSObjectCompareTo

1 个答案:

答案 0 :(得分:3)

foreach行更改为

foreach($var in $myArray.Release)