PowerShell Basics: Do Loop

Exported on 27-Aug-2021 15:14:59

POWERSHELL DO LOOP ON ATTUNE

This is a Blueprint on Attune of how to run a Do-While or Do-Until Loop in PowerShell

The Do keyword works with the While or the Until keyword to execute the statements in a script block, subject to the specified condition.

Unlike the While loop, the script block in a Do loop always runs at least once.

In the Do-While loop and Do-Until loop, the condition is evaluated after the script block has run.


Parameters

Name Type Script Reference Default Value Comment
Attune Node Windows Server attuneNode This is my Attune Node
Attune Node Credentials Windows OS Credential attuneNodeCredentials

1 - Do Until

Below shows the basic Else statement syntax:

powershell do {<statement list>} until (<condition>)


The Statement List (\): This is where all PowerShell commands are been written for the function to execute.

The condition Section (\): This evaluates the condition in the Until section to either True or False; If false the loop continues, If true it breaks out of the loop.


The example in this Blueprint runs the commands in the statement list first and then checks the condition (if the value of the variable "i" is equal to the total number of strings in the array [$array]).

The loop keeps running until the condition becomes true


The connection details have changed from the last step.

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Attune Node
  2. Login as user {Attune Node Credentials}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
#Region Do Until
#==============================================================================
# The variable array ($array) is declared and holds an Array of strings.
$array = @("A", "T", "T", "U", "N", "E")

# Creates a variable "i" and assigns a numeric value of 0 to it.
$i = 0

# Do-Until Statement
do {
  # This echos out the present value of the string in the array when the condition is satisfied.
  # `n - it creates a new line after the message.
  # This is a print to screen CMDLET [Write-Host].
  Write-Host $array[$i] `n

  # Suspends the activity in the script for 1 second
  Start-Sleep -s 1

  # Increment the present value of $i by 1 ($i++ is the same as "$i = $i + 1"}).
  $i++
}
until ($i -eq $array.Count)
#==============================================================================
#EndRegion Do Until

2 - Do While

Below shows the basic Else statement syntax:

powershell do {<statement list>} while (<condition>)


The Statement List (\): This is where all PowerShell commands are been written for the function to execute.

The condition Section (\): This evaluates the condition to either True or False; If true the loop continues, If false it breaks out of the loop.


The example in this Blueprint runs the statement list first and then checks the condition (if the value of the variable "i" is less than total number of strings in the array [$array]).

The loop keeps running until the condition becomes false.


Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Attune Node
  2. Login as user {Attune Node Credentials}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
#Region Do While
#==============================================================================
# The variable array ($array) is declared and holds an Array of strings.
$array = @("S", "E", "R", "V", "E", "R", "T", "R", "I", "B", "E")

# Creates a variable "i" and assigns a numeric value of 0 to it.
$i = 0

# Do-While Statement
do {
  # This echos out the present value of the string in the array when the condition is satisfied.
  # `n - it creates a new line after the message.
  # This is a print to screen CMDLET [Write-Host].
  Write-Host $array[$i] `n

  # Suspends the activity in the script for 1 second
  Start-Sleep -s 1

  # Increment the present value of $i by 1 ($i++ is the same as "$i = $i + 1"}).
  $i++

  # The condition for the statement list to keep running is declared here.
}while ($i -lt $array.Count)
#==============================================================================
#EndRegion Do While