Condition with a function call in PowerShell

后端 未结 2 1774
-上瘾入骨i
-上瘾入骨i 2021-01-17 22:37

Something is really weird with this language. I\'m trying to execute a function and use its result value as condition. This is my code:

function Get-Platform         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 22:47

    I think you are comparing a function and not the function result. Also somehow the echo does not work as expected in a function. I usually use Write-Host.

    Here is my solution to your problem:

    function Get-Platform()
    {
        # Determine current Windows architecture (32/64 bit)
        if ([System.Environment]::GetEnvironmentVariable("ProgramFiles(x86)") -ne $null)
        {
            Write-Host("x64")
            return "x64"
        }
        else
        {
            Write-Host("x86")
            return "x86"
        }
    }
    
    $platform = Get-Platform
    
    if ($platform -eq 'x64')
    {
        echo "64 bit platform"
    }
    
    if ($platform -eq 'x86')
    {
        echo "32 bit platform"
    }
    

提交回复
热议问题