PowerShell笔记

本系列是一个重新学习PowerShell的笔记,内容引用自PowerShell中文博客

ForEach-Object

ForEach-Object 会对管道传入的结果集内的元素进行逐个处理,循环内由$_表示当前元素

#可以对元素进行变更
PS C:PowerShell> $arr = 1..5                                                                                                                                                                PS C:PowerShell> $arr | ForEach-Object {"当前值:"+ $_}                                                                                                                                      当前值:1
当前值:2
当前值:3
当前值:4
当前值:5
PS C:PowerShell> $arr | ForEach-Object {$arr[$_-1] = $_+1}                                                                                                                                  PS C:PowerShell> $arr | ForEach-Object {"当前值:"+ $_}                                                                                                                                      当前值:2
当前值:3
当前值:4
当前值:5
当前值:6

#按规格输出
PS C:PowerShell> Get-WmiObject Win32_Service | ForEach-Object {"Name:"+ $_.DisplayName, ", Is ProcessId more than 100:" + ($_.ProcessId -gt 100)}
Name:1E Client , Is ProcessId more than 100:True
Name:Intel® SGX AESM , Is ProcessId more than 100:True
Name:AllJoyn Router Service , Is ProcessId more than 100:False
Name:Application Layer Gateway Service , Is ProcessId more than 100:False
Name:Application Host Helper Service , Is ProcessId more than 100:True
Name:Application Identity , Is ProcessId more than 100:False
Name:Application Information , Is ProcessId more than 100:True

foreach

Foreach-object 为cmdlet命令,使用在管道中,对管道结果逐个处理,foreach为遍历集合的关键字。
下面举两个例子:

$array=7..10
foreach ($n in $array)
{
    $n*$n
} 

"---------------------------------------------"

foreach($n in $array)
{
    if($n -gt 5)
    {
        $n
    }
}

PS C:> test.ps1                                                                                                                                                           49
64
81
100
---------------------------------------------
7
8
9
10

Do While

Do和While可能产生死循环,为了防止死循环的发生,你必须确切的指定循环终止的条件。指定了循环终止的条件后,一旦条件不满足就会退出循环。
继续与终止循环的条件,do-while()会先执行再去判断,能保证循环至少执行一次。

$num = 10
do {
    $num -= 1
    $num
} while ($num -gt 5)

PS C:> test.ps1                                                                                                                                                           9
8
7
6
5
#continue 跳过当前循环
$n = 1
while($n -lt 6)
{
    if($n -eq 4)
    {
        $n=$n+1
        continue 
    }
    else
    {
        $n
    }
    $n=$n+1
}
PS C:> test.ps1                                                                                                                                                           1
2
3

#break跳出循环
$n = 1
while($n -lt 6)
{
    if($n -eq 4)
    {
      break
    }
    $n
    $n++
}

PS C:> test.ps1                                                                                                                                                           1
2
3

For

如果你知道循环的确切次数可以使用For循环,For循环属于计数型循环,一旦达到最大次数,循环就会自动终止。下面的例子通过循环求1-100的数列和。

$sum=0
for($i=1;$i -le 100;$i++)
{
    $sum+=$i
}
$sum

For循环是特殊类型的While循环
在For循环开始的圆括号中,由分号隔开的语句为循环的控制条件,分别为:初始化,循环执行满足的条件,增量。
For循环的控制语句第一个和第三个可以为空:

$sum=0
$i=1
for(;$i -le 100;)
{
    $sum+=$i
    $i++
}
$sum

For循环的特殊应用
上面的For循环示例停留在数字层面上,其实While循环能办到的事,For循环也可以,只是可能有时不方便而已。例如判断域名的例子:

for($domain="";!($domain -like "www.*.*");$domain=Read-Host "Input domain")
{
    Write-Host -ForegroundColor "Green" "Please give a valid domain name."
}

PS C:> test.ps1                                                                                                                                                           Please give a valid domain name.
Input domain: wwww

下面的例子演示逐行读取文本文件

"11111" > C:PowerShell	est.txt
"22222" >> C:PowerShell	est.txt
"33333" >> C:PowerShell	est.txt
"44444" >> C:PowerShell	est.txt

for($file=[IO.File]::OpenText("C:PowerShell	est.txt") ; !($file.EndOfStream);$line=$file.ReadLine() )
{
    $line;
}
$file.Close()

PS C:PowerShell> test.ps1                                                                                                                                                 11111
22222
33333
PS C:PowerShell> ls                                                                                                                                                                         

    Directory: C:PowerShell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2021/9/14     13:51             58 test.txt

Switch

$nums = 10..7
Switch ($nums)
{
    {($_ % 2) -eq 0} {"$_ 偶数"}
    {($_ % 2) -ne 0} {"$_ 奇数"}
}

PS C:PowerShell> test.ps1                                                                                                                                                 10 偶数
9 奇数
8 偶数
7 奇数
原文地址:https://www.cnblogs.com/MerLin-LiuNian/p/15267284.html