在 PowerShell 中使用 if 条件进行比较
Using if condition to compare in PowerShell
我正在尝试从系统中获取 .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
将 foreach
行更改为
foreach($var in $myArray.Release)
我正在尝试从系统中获取 .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
将 foreach
行更改为
foreach($var in $myArray.Release)