Home > Software engineering >  Second function inside an if with an -And doesn't get executed (Powershell)
Second function inside an if with an -And doesn't get executed (Powershell)

Time:06-10

I have the following functions

function checkA {
  Write-Host "Checking A"
  return $true
}

function checkB {
  Write-Host "Checking B"
  return $true
}

Now, in the main program I use both functions like:

if(checkA -And checkB){
  Write-Host "Checks are ok"
  return
}

I'm not getting the checkB Write-Host output and the IDE says the function is not even referenced when used like this.

Can someone tell me what's going on?

CodePudding user response:

PowerShell is treating the -And as a parameter to checkA and checkB as the value of the parameter. Since checkB is not recognised as a function here, it is never called.

If you wrap the function calls in brackets, it should work as expected:

if((checkA) -And (checkB)){
  Write-Host "Checks are ok"
  return
}
  • Related