Logische Operatoren in PowerShell
- PowerShell Logische Operatoren
-
der
-und
Operator in PowerShell -
der
-oder
Operator in PowerShell -
der
-xor
Operator in PowerShell -
der
-nicht
Operator in PowerShell

Logische Operatoren können verschiedene Bedingungen in eine einzige Bedingung umwandeln.
Dieser Artikel wird reale Beispiele diskutieren und logische Operatoren in Skripten mit PowerShell anwenden.
PowerShell Logische Operatoren
Logische Operatoren sind und
, oder
, xor
und nicht
oder !
.
der -und
Operator in PowerShell
Die Ausgabe ist wahr
, wenn $a
und $b
wahr
sind; andernfalls falsch
.
Wahrheitstabelle:
A | B | Ausgabe |
---|---|---|
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)
Der -und
Operator gibt wahr
zurück, wenn beide wahr sind. Im Allgemeinen werden die -und
Operatoren verwendet, wenn wir wollen, dass alle Bedingungen überprüft und erfüllt werden.
Hier ist ein Beispiel für beide Bedingungen, die erfüllt sein müssen.
$attendance = 102
$paid = "Y"
if ($attendance -gt 100 -and $paid -eq "Y") {
Write-Output "Allow for examination."
}
Ausgabe:
Allow for examination.
der -oder
Operator in PowerShell
Die Ausgabe ist falsch
, wenn $a
und $b
falsch
sind, im Vergleich zum -und
Operator.
Der -oder
Operator benötigt nur eine Variable, die wahr
ist, um wahr
auszugeben.
Wahrheitstabelle:
A | B | Ausgabe |
---|---|---|
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)
Der -oder
Operator gibt falsch
zurück, wenn beide Bedingungen falsch
sind. Im Allgemeinen werden die -oder
Operatoren verwendet, wenn man jede Bedingung als wahr
betrachtet.
$attendance = 99
$marks = 201
if ($attendance -gt 100 -or $marks -gt 200) {
Write-Output "Give five extra marks."
}
Ausgabe:
Give five extra marks.
der -xor
Operator in PowerShell
Der exklusive oder
oder -xor
ergibt wahr
, wenn nur eine von $a
oder $b
wahr
ist. Wenn beide Bedingungen wahr
sind, ergibt -xor
das Ergebnis falsch
.
Wahrheitstabelle:
A | B | Ausgabe |
---|---|---|
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
der -nicht
Operator in PowerShell
Der -nicht
Operator gibt das Gegenteil der Ausdrucksausgabe zurück. Wenn die Ausgabe des Ausdrucks wahr
ist, gibt der Operator ihn als falsch
zurück und umgekehrt.
-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
Das Ausrufungszeichen !
ist dasselbe wie der -nicht
Operator.
!('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