0

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?

andrestascon
  • 64
  • 1
  • 8
  • 1
    Related: [PowerShell's parsing modes: argument (command) mode vs. expression mode](https://stackoverflow.com/q/48776180/7571258) – zett42 Jun 10 '22 at 09:44

1 Answers1

3

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
}
boxdog
  • 7,894
  • 2
  • 18
  • 27
  • Yeah okey I have to take care with that. My real problem had one argument for both checkA and checkB functions so I had a bit of a mess. However wrapping it up in functions seems to work. Thanks! – andrestascon Jun 10 '22 at 07:47