ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

PowerShell - Loops

2022-01-28 15:31:40  阅读:174  来源: 互联网

标签:10 ++ Number Write Host Loops PowerShell Loop


PowerShell - Loops

If Statement
# If statement with Zero else Statement
$Number = 16
if ($Number -gt 10)
{ Write-Host "The value $Number is gerater than 10"}

# If statement with ONE else Statement
$Number = 8
if ($Number -gt 10)
{ Write-Host "The value $Number is greater than 10"}
else
{ Write-Host "The value $Number is less than or equal to 10"}

#####################################################################
# If Statement with ZERO elseif Statement
$Number = 16
if ($Number -gt 10)
{ Write-Host "The value $Number is greater than 10"}
else
{ Write-Host "The value $Number is less than or equal to 10"}

# If Statement with ONE elseif statement
$Number = 16
if ($Number -gt 10)
{ Write-Host " Number is greater than 10"}
elseif ($Number -eq 10)
{ Write-Host " Number si equal to 10"}
else
{ Write-Host " Number is less than or equal to 10"}

# Ternary Operater (Introduced In PowerShell 7.0)
2 -eq 3 ? "This is True":"This is False"
Foreach Loop
####################################################################
#*************** PowerShell Foreach Loop ***************************
####################################################################

# Running foreach Loop as a command
$fruits = "Apple", "Orange", "KiWi"; foreach ($Item in $fruits){"This is $Item"}
# Running foreach Loop in a Script
$fruits = "Apple", "Orange", "KiWi"
foreach ($Item in $fruits)
{
"This is $Item"
}
# Using foreach against Command Output
foreach ($Service in Get-Service)
{
"This is $(($Service).displayname)"
}
While Loop
####################################################################
#*************** PowerShell While Loop ***************************
####################################################################
# Using while loop to return Items of any Array or Collection
$List = 15..18
$i = 0
While ($i -le $list.count) {$list[$i];$i++}
# Using PowerShell While Loop in Interactive way
$i = 0
$answer = Read-Host -Prompt "Write further Count ? => Yes or No"
while($answer -like "Yes"){$i;$answer = Read-Host -Prompt "Enter";$i++}

image-20220128144349183

Do Loop
####################################################################
#*************** PowerShell Do Loop ********************************
####################################################################
#*************** Do-While Loop *************************************
####################################################################

# Using do-while loop to read contents of an Array or Collection
$list = 1..8
$i = 0
do{$List[$i];$i++} while ($i -le $list.count)
# Using Do-while loop Interactively
$i = 0
do{$i;$i++;$answer = Read-Host -Prompt "Write further Count ? => Yes or No"} while($answer -like "Yes")

image-20220128145950785

#*************** Do-Until Loop *************************************
####################################################################

# Using do-until loop to read contents of an Array or Collection
$list = 1..8
$i = 0
do{$List[$i];$i++} until ($i -eq $list.count)
# Using Do-until loop Interactively
$i = 0
do{$i;$i++;$answer = Read-Host -Prompt "Write further Count ? => Yes or No"} until ($answer -like "no")
For Loop
# For Loop
# Syntax ==> for (<Init>; <Condition>; <Repeat>) {<Statement list>}
for ($i = 0;$I -le 8;$i++) {$i}

# Different Variables in Statement list
$Count = 0
for ($i = 0;$I -le 8;$i++) {"$Count Apple"; $Count++}

# Using multiple commands in init and repeat portion, Usinf multiple Conditions
for (($i = 0), ($m = 100);$I -le 100 -and $m -ge 100;$i++, $m++) {"$i is $m"}

# Some Fun
$i = 0
for(;;) {$i;$i++}

# For Loop with Interaction
for(($i = 0), ($answer = Read-Host -Prompt "Write further Count ? => Yes or No");$answer -like "Yes";$i++)
{$i;$answer = Read-Host -Prompt "Write further Count ? => Yes or No"}

image-20220128152731723

标签:10,++,Number,Write,Host,Loops,PowerShell,Loop
来源: https://www.cnblogs.com/keepmoving1113/p/15852879.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有