• PowerShell Loops


    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

  • 相关阅读:
    修改MFC标题栏上的图标
    【转】子窗口刷新父窗口的问题
    水晶报表添加引用
    【转】MetadataType的使用,MVC的Model层数据验证
    poj 1556 The Doors 线段相交判断+最短路
    poj 1269 Intersecting Lines 求直线交点 判断直线平行共线
    string 函数操作
    poj 1066 Treasure Hunt 线段相交判断
    poj 1410 Intersection 线段相交判断
    poj 3347 Kadj Squares 扩大数据化整数
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/15852879.html
Copyright © 2020-2023  润新知