Function Call Condition in PowerShell

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

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" } 

Expected Result:

 x64 64 bit platform 

But the actual conclusion is this:

 64 bit platform 32 bit platform 

What's going on here? How can this be fixed? I could not find examples on the Internet that use functions inside the if condition. Is this even possible in Powershell? I am in Windows 7 without special configuration, so I have a PS version with it.

+6
source share
2 answers

If you want to compare the return value of a function in a conditional expression, you should 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 this variable in the conditional expression. Otherwise, the comparison operator and the other operand are passed as arguments to the function (where in your case they are silently discarded). Then your function returns a result that is neither "" , nor 0 , nor $null , so it evaluates to $true , causing both messages to display.

This should do what you want:

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

By the way, you should avoid using separate if for conditions that are mutually exclusive. For platform check if..then..elseif

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

or << 28>

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

will be more appropriate.

I would also avoid echo inside the function. Just return the value and perform any echo that may be required with the return value. Everything that is reflected inside the function will also be returned to the caller.

Last note: I personally would prefer not to rely on the existence of a specific folder or environment variable to determine the architecture of the operating system. Using WMI for this task makes me much more reliable:

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

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

+16
source

I think you are comparing a function, not the result of a function. Also somehow the echo is not working properly in the 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" } 
+3
source

All Articles