Logical judgment
First, let's briefly introduce the most basic logic judgment:
•-Eq determines whether it is equal to (equal)
•-Lt is less than (less)
•-Gt determine whether the value is greater than (greater)
•-Ge determines whether the value is greater than or equal to (greater of equal)
•-Le determines whether it is less than or equal to (less or equal)
•-Ne determines whether it is not equal to (no equal)
In the previous sections, If you note that PowerShell is not case sensitive, we need to distinguish them in some cases. Therefore, you can also use:
•-Ieq (case-insensitive)
•-Ceq (case-sensitive)
The former is case-insensitive, while the latter is case-sensitive.
Example:Copy codeThe Code is as follows: "MaRui"-eq "marui" <enter>
Result: "True"
"MaRui"-ieq "MARUI" <enter>
Result: "True"
"MaRui"-ceq "MARUI" <enter>
Result: "False"
Logical operation
•-And
•-Or
•-Not non
•! Non
Condition
If, else, elseif statements
Basic Syntax:Copy codeThe Code is as follows: if (condition)
{Code}
Elseif (condition)
{Code}
Else (condition)
{Code}
Else
{Code}
For example:Copy codeThe Code is 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 local operating system and uses the "if" statement. Copy the following script and save it as "OS. ps1". Run it with PowerShell.Copy codeThe Code is 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 2000 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 codeThe Code is as follows: switch (expression)
{
(Expression) {code}
Value {code}
Default {default Code Execution}
}
For example, we can query the value of "domainRole" to determine the role of the computer in the current Active Directory domain. Try the following code:Copy codeThe Code is 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 an expression used to judge a statement:Copy codeThe Code is 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 "}
}
Running result:
This section introduces the usage of loop statements.