Logical Judgment
Let's begin by simply introducing the most basic logical judgments:
-eq judgment is equal to (equal)
-lt judgment time less than (less than)
-GT judgment is greater than (greater than)
-ge is judged to be greater than or equal to (greater of equal)
-le is judged to be less than or equal to (less or equal)
-ne judgment is not equal to (no equal)
In the previous sections, if you look at it, you should be able to see that PowerShell is not sensitive to capitalization, but at some point we need to differentiate them, so we can also use things like:
-ieq (case-insensitive)
-CEQ (case-sensitive)
The former is case insensitive, while the latter is case-sensitive.
Example:
Copy Code code as follows:
"Marui"-eq "Marui" <enter>
Result: "True"
"Marui"-ieq "Marui" <enter>
Result: "True"
"Marui"-ceq "Marui" <enter>
Result: "False"
Logical operations
-and and
-or or
-not Non
• ! Non -
condition
If, else, ElseIf statements
Basic syntax:
Copy Code code as follows:
if (condition)
Code
ElseIf (condition)
Code
else (condition)
Code
Else
Code
For example:
Copy Code code as follows:
$n =10
if ($n-eq 1)
{"N=1"}
ElseIf ($n-ne 1)
{"N!=1,and n= $n"}
The following script is used to view the native operating system, using the "if" statement. Please copy the following script and save it as "Os.ps1" and try the PowerShell run.
Copy Code code as follows:
$ComputerName = (Get-wmiobject-class win32_computersystem). Name
$OS _version = (get-wmiobject-class win32_operatingsystem-computer $ComputerName). Version
if (($OS _version-eq "5.1.2600")-or ($OS _version-eq "5.2.3790"))
{
Write-host "Computer Name:" $ComputerName
if ($OS _version-eq "5.1.2600")
{
Write-host "Os:windows XP"
}
ElseIf ($OS _version-eq "5.2.3790")
{
Write-host "Os:windows 2003"
}
}
ElseIf (($OS _version-eq "5.0.2195")-or ($OS _version-eq "6.1.7600"))
{
Write-host "Computer Name:" $ComputerName
if ($OS _version-eq "5.0.2195")
{
Write-host "Os:windows Server"
}
ElseIf ($OS _version-eq "6.1.7600")
{
Write-host "Os:windows 7"
}
}
Else
{
Write-host "$ComputerName is not supported."
}
"–end of report–"
Running results on my computer:
"switch" statement
Basic syntax:
Copy Code code as follows:
switch (expression)
{
(expression) Code
Value {code}
Default {Execute code as defaults}
}
For example, we can query the value of "DomainRole" to determine the role of the computer in the current Active Directory domain, and try the following code:
Copy Code code as follows:
Switch ((Get-wmiobject-class win32_computersystem). DomainRole)
{
0 {write-host "Standalone Workstation"}
1 {write-host "member Workstation"}
2 {write-host "Standalone Server"}
3 {write-host "member Server"}
4 {write-host "Backup Domain Controller"}
5 {write-host "Primary Domain Controller"}
Default {write-host "Cannot determine domain role"}
}
An example of a judgment statement as an expression:
Copy Code code as follows:
Switch (100)
{
(99 + 1) {write-host "99+1=100"}
(1 + 100) {write-host "1+100=100"}
(50*2) {write-host "50*2=100"}
(33.333*3) {write-host "33.333*3=100"}
}
Run Result:
Here's the section, and the next section will talk about the use of circular statements.