Operatori Logici in PowerShell

  1. Operatori Logici di PowerShell
  2. l’operatore -and in PowerShell
  3. l’operatore -or in PowerShell
  4. l’operatore -xor in PowerShell
  5. l’operatore -not in PowerShell
Operatori Logici in PowerShell

Gli operatori logici possono convertire varie condizioni in un’unica condizione.

Questo articolo discuterà esempi del mondo reale e applicherà operatori logici in uno script con PowerShell.

Operatori Logici di PowerShell

Gli operatori logici sono and, or, xor e not o !.

l’operatore -and in PowerShell

L’output è true se $a e $b sono true; altrimenti, false.

Tabella della Verità:

A B Output
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)

L’operatore -and restituisce true solo quando entrambi sono veri. In generale, gli operatori -and sono usati dove vogliamo che tutte le condizioni siano verificate e soddisfatte.

Ecco un esempio di entrambe le condizioni che devono essere soddisfatte.

$attendance = 102
$paid = "Y"
if ($attendance -gt 100 -and $paid -eq "Y") {
    Write-Output "Allow for examination."
}

Output:

Allow for examination.

l’operatore -or in PowerShell

L’output è false se $a e $b sono false, rispetto all’operatore -and.

L’operatore -or ha bisogno solo di una variabile per essere true per produrre un output true.

Tabella della Verità:

A B Output
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)

L’operatore -or restituisce false solo quando entrambe le condizioni sono false. In generale, gli operatori -or sono usati quando si considera qualsiasi condizione come true.

$attendance = 99
$marks = 201
if ($attendance -gt 100 -or $marks -gt 200) {
    Write-Output "Give five extra marks."
}

Output:

Give five extra marks.

l’operatore -xor in PowerShell

L’esclusivo or o -xor risulta true se solo uno tra $a o $b è true. Se entrambe le condizioni sono true, -xor produce un risultato di false.

Tabella della Verità:

A B Output
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

l’operatore -not in PowerShell

L’operatore -not restituisce l’opposto dell’output dell’espressione. Se l’output dell’espressione è true, l’operatore restituirà false e viceversa.

-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

Il punto esclamativo ! è lo stesso dell’operatore -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
Ti piacciono i nostri tutorial? Iscriviti a DelftStack su YouTube per aiutarci a creare altre guide video di alta qualità. Iscriviti
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn