我是Powershell的新手,正在开发一个为特定MSI编写deployemnt包的项目。
我想要添加的其中一项是确认在继续之前安装了先决条件。
下面的代码就是我拼凑在一起的代码。它在初始检查时失败。
我正在使用它来确定是否安装了.Net 4.6.2:
Get-ChildItem "hklm:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemPropertyValue -Name Release | % { $_ -ge 394802 }
如果我在安装了.Net 4.6.2的系统上运行它。我得到了“真实”的回应。
当我尝试将其包装在IF语句中时,我收到此错误:
ForEach-Object : Cannot bind parameter 'RemainingScripts'. Cannot convert the "-like" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Users\bconway.TILLSTER\Desktop\EI4 Updater PowerShell Script\prereq test.ps1:4 char:118
+ ... t-ItemPropertyValue -Name Release | % { $_ -ge 394802 } -like 'True')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand
这是我写的代码:
#Check to see that .Net 4.6.2 is installed
#Get-ChildItem "hklm:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemPropertyValue -Name Release | % { $_ -ge 394802 }
If (Get-ChildItem "hklm:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemPropertyValue -Name Release | % { $_ -ge 394802 } -like 'True')
{
Write-Host "Confirmed that .Net 4.6.2 is installed!"
#LogWrite "Confirmed that .Net 4.6.2 is installed!"
}
else
{
Write-Host ".Net 4.6.2 is NOT installed! Aborting Update!"
#LogWrite ".Net 4.6.2 is NOT installed! Aborting Update!"
exit
}
与往常一样,任何帮助将不胜感激!
答案 0 :(得分:2)
您必须删除-like 'True'
,因为它是错误消息中提到的错误来源。
您还可以简化代码:
Get-ChildItem "hklm:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemPropertyValue -Name Release | ForEach-Object {
switch ($_ -ge 394802) {
$true { Write-Host "Confirmed that .Net 4.6.2 is installed!" }
$false { Write-Host ".Net 4.6.2 is NOT installed! Aborting Update!"}
}
}
答案 1 :(得分:0)
添加括号
If (Get-ChildItem "hklm:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemPropertyValue -Name Release | % { $_ -ge 394802 })
' -like true,不需要'正如Jeroen Mostert指出的那样
If ("Get-ItemPropertyValue -Path 'hklm:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' -Name Release" -ge 394802){}
您可以跳过Get-ChildItem cmdlet
{{1}}