Operadores Lógicos no PowerShell
- Operadores Lógicos do PowerShell
-
o Operador
-andno PowerShell -
o Operador
-orno PowerShell -
o Operador
-xorno PowerShell -
o Operador
-notno PowerShell
Operadores lógicos podem converter várias condições em uma única condição.
Este artigo discutirá exemplos do mundo real e aplicará operadores lógicos em script com PowerShell.
Operadores Lógicos do PowerShell
Os operadores lógicos são and, or, xor e not ou !.
o Operador -and no PowerShell
A saída é true se $a e $b forem true; caso contrário, false.
Tabela Verdade:
| A | B | Saída |
|---|---|---|
| 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)
O operador -and retorna true apenas quando ambos são verdadeiros. Em geral, os operadores -and são usados onde queremos que todas as condições sejam verificadas e atendidas.
Aqui está um exemplo de ambas as condições que precisam ser atendidas.
$attendance = 102
$paid = "Y"
if ($attendance -gt 100 -and $paid -eq "Y") {
Write-Output "Allow for examination."
}
Saída:
Allow for examination.
o Operador -or no PowerShell
A saída é false se $a e $b forem false, em comparação com o operador -and.
O operador -or precisa apenas de uma variável para ser true para retornar true.
Tabela Verdade:
| A | B | Saída |
|---|---|---|
| 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)
O operador -or retorna false apenas quando ambas as condições são false. Em geral, os operadores -or são usados quando consideramos qualquer condição como true.
$attendance = 99
$marks = 201
if ($attendance -gt 100 -or $marks -gt 200) {
Write-Output "Give five extra marks."
}
Saída:
Give five extra marks.
o Operador -xor no PowerShell
O or exclusivo ou -xor resulta em true se apenas uma de $a ou $b for true. Se ambas as condições forem true, -xor resulta em false.
Tabela Verdade:
| A | B | Saída |
|---|---|---|
| 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
o Operador -not no PowerShell
O operador -not retorna o oposto da saída da expressão. Se a saída da expressão for true, o operador retornará false, e vice-versa.
-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
O ponto de exclamação ! é o mesmo que o operador -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