PowerShell(九) 条件控制、逻辑
• -eq 判断是否等于(equal)
• -lt 判断时候小于(less than)
• -gt 判断是否大于(greater than)
• -ge 判断是否大于或等于(greater of equal)
• -le 判断是否小于或等于(less or equal)
• -ne 判断是否不等于(no equal)
在之前的几个小节中,如果你注意的话,应该可以发现PowerShell对于大小写并不敏感,可是在某些时候我们却需要区分它们,因此,还可以使用诸如:
• -ieq (case-insensitive)
• -ceq (case-sensitive)
if (条件)
{代码}
elseif (条件)
{代码}
else (条件)
{代码}
else
{代码}
代码如下:
$n=10
if ($n -eq 1)
{"n=1"}
elseif ($n -ne 1)
{"n!=1,and n=$n"}
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"}
}
$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–"