PowerShell编程:条件、循环、工作流及.NET交互全解析
1. 条件语句
在PowerShell中,条件语句能够改变脚本的执行流程,主要包含if、elseif、else和switch语句。
1.1if、elseif和else语句
语法结构如下:
if(condition) { statement block } elseif(condition) { statement block } else { statement block }若条件评估为$true,PowerShell会执行对应的语句块,然后在if/elseif/else语句列表末尾继续执行。即便语句块仅含一条语句,也需用大括号将其括起来。
示例代码:
$textToMatch = Read-Host "Enter some text" $matchType = Read-Host "Apply Simple or Regex matching?" $pattern = Read-Host "Match pattern" if($matchType -eq