about_Logical_Operatorsabout_Logical_Operators
KRÓTKI OPISSHORT DESCRIPTION
Opisuje operatory, które łączą się z instrukcjami w programie PowerShell.Describes the operators that connect statements in PowerShell.
DŁUGI OPISLONG DESCRIPTION
Operatory logiczne programu PowerShell łączą wyrażenia i instrukcje, co pozwala na przetestowanie wielu warunków przy użyciu jednego wyrażenia.The PowerShell logical operators connect expressions and statements, allowing you to use a single expression to test for multiple conditions.
Na przykład poniższa instrukcja używa operatora and oraz operatora OR, aby połączyć trzy instrukcje warunkowe.For example, the following statement uses the and operator and the or operator to connect three conditional statements. Instrukcja ma wartość true tylko wtedy, gdy wartość $a jest większa niż wartość $b, a $a lub $b jest mniejsza niżThe statement is true only when the value of $a is greater than the value of $b, and either $a or $b is less than 20.
($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
Program PowerShell obsługuje następujące operatory logiczne.PowerShell supports the following logical operators.
OperatorOperator | OpisDescription | PrzykładExample |
---|---|---|
-and |
Koniunkcja logiczna i.Logical AND. PRAWDA, gdy obaTRUE when both | (1 -eq 1) -and (1 -eq 2) |
instrukcje mają wartość TRUE.statements are TRUE. | False |
|
-or |
Logiczne OR.Logical OR. Wartość TRUE, gdyTRUE when either | (1 -eq 1) -or (1 -eq 2) |
instrukcja ma wartość TRUE.statement is TRUE. | True |
|
-xor |
Logiczne wyłącznych lub.Logical EXCLUSIVE OR. Wartość TRUE, gdyTRUE when | (1 -eq 1) -xor (2 -eq 2) |
tylko jedna instrukcja ma wartość TRUEonly one statement is TRUE | False |
|
-not |
Nie ma logicznego.Logical not. Negacja instrukcjiNegates the statement | -not (1 -eq 1) |
poniżej.that follows. | False |
|
! |
Tak samo jak -not Same as -not |
!(1 -eq 1) |
False |
Uwaga:Note:
W poprzednich przykładach użyto również operatora porównania równości -eq
.The previous examples also use the equal to comparison operator -eq
. Aby uzyskać więcej informacji, zobacz about_Comparison_Operators.For more information, see about_Comparison_Operators. W przykładach użyto również wartości logicznych liczb całkowitych.The examples also use the Boolean values of integers. Liczba całkowita 0 ma wartość FALSE.The integer 0 has a value of FALSE. Wszystkie inne liczby całkowite mają wartość TRUE.All other integers have a value of TRUE.
Składnia operatorów logicznych jest następująca:The syntax of the logical operators is as follows:
<statement> {-AND | -OR | -XOR} <statement>
{! | -NOT} <statement>
Instrukcje używające operatorów logicznych zwracają wartości logiczne (TRUE lub FALSE).Statements that use the logical operators return Boolean (TRUE or FALSE) values.
Operatory logiczne programu PowerShell obliczają tylko te instrukcje, które są wymagane do określenia wartości prawdy instrukcji.The PowerShell logical operators evaluate only the statements required to determine the truth value of the statement. Jeśli lewy operand w instrukcji zawierającej operator and ma wartość FALSE, prawy operand nie jest obliczany.If the left operand in a statement that contains the and operator is FALSE, the right operand is not evaluated.
Jeśli lewy operand w instrukcji, która zawiera instrukcję or ma wartość TRUE, prawy operand nie jest obliczany.If the left operand in a statement that contains the or statement is TRUE, the right operand is not evaluated. W związku z tym można użyć tych instrukcji w taki sam sposób, jak w przypadku użycia If
instrukcji.As a result, you can use these statements in the same way that you would use the If
statement.
ZOBACZ RÓWNIEŻSEE ALSO
about_Operatorsabout_Operators
Porównanie-obiektCompare-Object