逻辑判断
先来简单介绍最基本的逻辑判断:
"codetitle">复制代码 代码如下:
"MaRui" -eq "marui" <enter>
结果:"True"
"MaRui" -ieq "MARUI" <enter>
结果:"True"
"MaRui" -ceq "MARUI" <enter>
结果:"False"
逻辑运算
"codetitle">复制代码 代码如下:
if (条件)
{代码}
elseif (条件)
{代码}
else (条件)
{代码}
else
{代码}
例如:
复制代码 代码如下:
$n=10
if ($n -eq 1)
{"n=1"}
elseif ($n -ne 1)
{"n!=1,and n=$n"}
下面的脚本是用来查看本机操作系统的,使用了"if"语句。请复制下面的脚本并且保存为"OS.ps1",用PowerShell运行试试。
复制代码 代码如下:
$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–"
在我的计算机上的运行结果:
"switch"语句
基本语法:
复制代码 代码如下:
switch (表达式)
{
(表达式) {代码}
值 {代码}
default {默认执行代码}
}
例如,我们可以查询"domainRole"的值来判断计算机在当前Active Directory域中担任的角色,试试下面的代码:
复制代码 代码如下:
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"}
}
判断语句为表达式的例子:
复制代码 代码如下:
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"}
}
运行结果:
这节就说到这里吧,下一节将讲一讲循环语句的使用。
先来简单介绍最基本的逻辑判断:
"codetitle">复制代码 代码如下:
"MaRui" -eq "marui" <enter>
结果:"True"
"MaRui" -ieq "MARUI" <enter>
结果:"True"
"MaRui" -ceq "MARUI" <enter>
结果:"False"
逻辑运算
"codetitle">复制代码 代码如下:
if (条件)
{代码}
elseif (条件)
{代码}
else (条件)
{代码}
else
{代码}
例如:
复制代码 代码如下:
$n=10
if ($n -eq 1)
{"n=1"}
elseif ($n -ne 1)
{"n!=1,and n=$n"}
下面的脚本是用来查看本机操作系统的,使用了"if"语句。请复制下面的脚本并且保存为"OS.ps1",用PowerShell运行试试。
复制代码 代码如下:
$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–"
在我的计算机上的运行结果:
"switch"语句
基本语法:
复制代码 代码如下:
switch (表达式)
{
(表达式) {代码}
值 {代码}
default {默认执行代码}
}
例如,我们可以查询"domainRole"的值来判断计算机在当前Active Directory域中担任的角色,试试下面的代码:
复制代码 代码如下:
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"}
}
判断语句为表达式的例子:
复制代码 代码如下:
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"}
}
运行结果:
这节就说到这里吧,下一节将讲一讲循环语句的使用。
标签:
条件控制,逻辑
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com
暂无“探索PowerShell(九) 条件控制、逻辑”评论...