파워쉘의 논리 연산자
- PowerShell 논리 연산자
-
PowerShell에서
-and
연산자 -
PowerShell에서
-or
연산자 -
PowerShell에서
-xor
연산자 -
PowerShell에서
-not
연산자

논리 연산자는 다양한 조건을 하나의 조건으로 변환할 수 있습니다.
이 기사에서는 실제 사례를 논의하고 PowerShell 스크립트에서 논리 연산자를 적용합니다.
PowerShell 논리 연산자
논리 연산자는 and
, or
, xor
및 not
또는 !
입니다.
PowerShell에서 -and
연산자
출력은 $a
와 $b
가 true
일 경우 true
이고, 그렇지 않으면 false
입니다.
진리표:
A | B | 출력 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 0 |
0 | 1 | 0 |
1 | 1 | 1 |
$a = 0
$b = 0
$a -and $b # false (if both variables are false)
$a = 1
$b = 0
$a -and $b # false (if any of the variables are false)
$a = 1
$b = 1
$a -and $b # true (if both variables are true)
-and
연산자는 두 가지가 모두 true일 때만 true
를 반환합니다. 일반적으로 -and
연산자는 모든 조건이 확인되고 충족되기를 원하는 경우에 사용됩니다.
충족되어야 하는 두 조건의 예는 다음과 같습니다.
$attendance = 102
$paid = "Y"
if ($attendance -gt 100 -and $paid -eq "Y") {
Write-Output "Allow for examination."
}
출력:
Allow for examination.
PowerShell에서 -or
연산자
출력은 $a
와 $b
가 false
일 경우 false
입니다. 이는 -and
연산자와 비교됩니다.
-or
연산자는 true
를 출력하기 위해 하나의 변수가 true
일 필요만 있습니다.
진리표:
A | B | 출력 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
$a = 0
$b = 0
$a -or $b # false (if both conditions are false)
$a = 1
$b = 0
$a -or $b # true (if any of the variables are true)
$a = 1
$b = 1
$a -or $b # true (if both of the variables are true)
-or
연산자는 두 조건이 모두 false
일 때만 false
를 반환합니다. 일반적으로 -or
연산자는 어떤 조건이든 true
로 간주할 때 사용됩니다.
$attendance = 99
$marks = 201
if ($attendance -gt 100 -or $marks -gt 200) {
Write-Output "Give five extra marks."
}
출력:
Give five extra marks.
PowerShell에서 -xor
연산자
배타적 or
또는 -xor
는 $a
또는 $b
중 하나만 true
일 때 true
가 됩니다. 두 조건이 모두 true
일 경우 -xor
는 false
의 결과를 제공합니다.
진리표:
A | B | 출력 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 0 |
('a' -eq 'A') -xor ('a' -eq 'z') # true as one of them is true
('a' -eq 'A') -xor ('Z' -eq 'z') # false as both of them is true
('a' -eq 's') -xor ('Z' -eq 'p') # false as both of them are false
PowerShell에서 -not
연산자
-not
연산자는 표현식 출력의 반대를 반환합니다. 표현식의 출력이 true
인 경우, 연산자는 이를 false
로 반환하고 그 반대도 마찬가지입니다.
-not ('a' -eq 'a') # false as the output of expression is true
-not ('v' -eq 'a') # true as output expression is false
-not ('v' -eq 'V') # false as output expression is true
-not ('V' -eq 'V1') # true as output expression is false
느낌표 !
는 -not
연산자와 동일합니다.
!('a' -eq 'a') # false as the output of expression is true
!('v' -eq 'a') # true as output expression is false
!('v' -eq 'V') # false as output expression is true
!('V' -eq 'V1') # true as output expression is false
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn