这种语言的东西真的很奇怪。我试图执行一个函数并使用它的结果值作为条件。这是我的代码:在PowerShell中调用函数的条件
function Get-Platform()
{
# Determine current Windows architecture (32/64 bit)
if ([System.Environment]::GetEnvironmentVariable("ProgramFiles(x86)") -ne $null)
{
echo "x64"
return "x64"
}
else
{
echo "x86"
return "x86"
}
}
if (Get-Platform -eq "x64")
{
echo "64 bit platform"
}
if (Get-Platform -eq "x86")
{
echo "32 bit platform"
}
预期输出是这样的:
x64
64 bit platform
但实际产量是这样的:
64 bit platform
32 bit platform
这是怎么回事?这怎么解决?我无法在网络上找到使用if
条件中的函数的任何示例。在Powershell中可能吗?我在Windows 7上没有特别的设置,所以我拥有PS版本。
谢谢您的完整解释。在函数名称周围使用圆括号确实可以调用函数,所以这看起来像是解决这些问题的通用方法。比使用其他变量名称更简单。 函数中的回显(由Write-Host替代为功能)仅用于跟踪目的。这是一个测试用例脚本。虽然我现在看到以下输出:“x64”,“64位平台”,“x64”。 WMI调用似乎返回一个本地化的字符串,对我来说它是“64位”。其他支票在其他地方看起来很广泛。 – ygoe 2013-04-11 07:38:21
根据操作系统架构的不同,OSArchitecture会返回一个字符串“32位”或“64位”,因此您必须调整条件。 – 2013-04-11 11:19:49