Condition with a function call in PowerShell

后端 未结 2 1758
-上瘾入骨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"
    }
    
    0 讨论(0)
  • 2021-01-17 22:56

    If you want to compare the return value of a function in a conditional, you must group the function call (i.e. put it in parentheses) or (as @FlorianGerhardt suggested) assign the return value of the function to a variable and use that variable in the conditional. Otherwise the comparison operator and the other operand would be passed as arguments to the function (where in your case they're silently discarded). Your function then returns a result that is neither "" nor 0 nor $null, so it evaluates to $true, causing both messages to be displayed.

    This should do what you want:

    ...
    if ( (Get-Platform) -eq 'x64' ) {
      echo "64 bit platform"
    }
    ...
    

    BTW, you should avoid using separate if statements for conditions that are mutually exclusive. For a platform check an if..then..elseif

    $platform = Get-Platform
    if ($platform -eq "x64") {
      ...
    } elseif ($platform -eq "x86") {
      ...
    }
    

    or a switch statement

    Switch (Get-Platform) {
      "x86" { ... }
      "x64" { ... }
    }
    

    would be more appropriate.

    I'd also avoid echoing inside the function. Just return the value and do any echoing that might be required in with the returned value. Anything echoed inside the function will also be returned to the caller.

    One last note: personally I'd rather not rely on the existence of a particular folder or environment variable for determining the operating system architecture. Using WMI for this task deems me a lot more reliable:

    function Get-Platform {
      return (gwmi Win32_OperatingSystem).OSArchitecture
    }
    

    This function will return a string "32-Bit" or "64-Bit", depending on the operating system architecture.

    0 讨论(0)
提交回复
热议问题