gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:04:50 25 4
gpt4 key购买 nike

我试图从系统中获取.Net版本,并尝试检查我的系统是否具有.NET版本大于或等于4.6。下面是代码。从代码中,我可以检索发行版本。我在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)

关于powershell - 使用if条件在PowerShell中进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50868564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com