Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine multiple conditions in if-statement

How do you chain 4 conditions together when you want either ONE set or the OTHER set of 2 condiitions to be true?

To be more precise I want to do:

If User is logged in AND Operating system version is Windows 10

OR

User is logged in AND LogonUI process is not running

Don't bother with the commands, they all work correctly when isolated, my issue is chaining them together.

For example I have :

if (
        (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
        -and`
        (Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"
    )
    { echo do X }

which is working fine. I want to add within that same if the other block of conditions. I tried this, but it's not working:

if (
        (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
        -and`
        (Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"`
        -or`
        (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
        -and -not`
        (Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)
    )
    { echo do X }

How do you chain multiple "blocks" like this? I know I could do two different IF, I had it working but isn't there a way to chain it all together in one IF? (the IF contains a lot of code and i don't want to duplicate it with two IF)

like image 250
Rakha Avatar asked May 02 '18 12:05

Rakha


People also ask

How do I combine multiple if conditions?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False)

Can you include multiple conditions in an if statement?

A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.

Can you combine multiple IF statements in Excel?

As a worksheet function, the IF function can be entered as part of a formula in a cell of a worksheet. It is possible to nest multiple IF functions within one Excel formula. You can nest up to 7 IF functions to create a complex IF THEN ELSE statement.

Can you combine 2 if statements?

Nested IF statement to check multiple logical tests If you want to evaluate multiple logical tests within a single formula, then you can nest several functions one into another. Such functions are called nested IF functions.


1 Answers

Put each set of conditions in parentheses:

if ( (A -and B) -or (C -and D) ) {
    echo do X
}

If either the first or the second set of conditions must be true (but not both of them) use -xor instead of -or:

if ( (A -and B) -xor (C -and D) ) {
    echo do X
}

Replace A, B, C, and D with the respective expressions.

like image 53
Ansgar Wiechers Avatar answered Sep 19 '22 15:09

Ansgar Wiechers