PS E:\> "MaRui" -eq "marui" True PS E:\> "MaRui" -ieq "MARUI" True PS E:\> "MaRui" -ceq "MARUI" False |
例如:复制代码
- if (条件)
- {代码}
- elseif (条件)
- {代码}
- else (条件)
- {代码}
- else
- {代码}
下面的脚本是用来查看本机操作系统的,使用了"if"语句。请复制下面的脚本并且保存为"OS.ps1",用PowerShell运行试试。复制代码
- $n=10
- if ($n -eq 1)
- {"n=1"}
- elseif ($n -ne 1)
- {"n!=1,and n=$n"}
"switch"语句复制代码
- $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–"
例如,我们可以查询"domainRole"的值来判断计算机在当前Active Directory域中担任的角色,试试下面的代码:复制代码
- switch (表达式)
- {
- (表达式) {代码}
- 值 {代码}
- default {默认执行代码}
- }
判断语句为表达式的例子:复制代码
- 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"}
- }
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |