Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can someone tell me what’s going on?

>Solution :

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading