PowerShell Basics: If Statement (If,Else & Elseif)

Exported on 11-Nov-2021 12:13:43

POWERSHELL IF STATEMENT ON ATTUNE

This is a Blueprint on Attune of how to run an IF statement in PowerShell

The IF Statement is used to run a block of code if a specified condition evaluates to true.

One or more conditions can also be specified to run if the latter evaluates to false.

Finally, an additional code block can be specified to run if another condition evaluates to true.

Parameters

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

1 - If

Below shows the basic IF Else statement syntax:


if (<test condition 1>)
    {<statement list 1>}

When an IF statement is run, the conditional expression is evaluated as either true or false.

If is true, runs, and it exits the If statement.

If is false, does not run, and it exits the If statement.

The IF Statement above in this Attune Blueprint contains a single command and does not contain any Else statements or any ElseIF statements.

This is the simplest form of the IF Statement.

It checks if the condition [the value of the Variable A ($A) is greater than 3] is satisfied and echos out the text [Output: (The value 5 is greater than 3.)] written in front of the CMDLET "Write-Host".

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 IF
#==============================================================================
# Creates a variable "A" and assigns a numeric value of 5 to it.
$A = 5

# This is an if statement block that checks if the value of the variable A is greater than 3.
if ($A -gt 3) {
    # If the condition is satisfied it will print the text in front of the CMDLET (Write-Host) to the screen.

    # This is a print to screen CMDLET [Write-Host].
    Write-Host "The value $A is greater than 3."
}
#==============================================================================
#EndRegion IF

2 - Else

Below shows the basic Else statement syntax:


if (<test condition 1>)
    {<statement list 1>}
[else
    {<statement list 2>}]

When an IF statement is run, the conditional expression is evaluated as either true or false.


If <test condition 1> is true, <statement list 1> runs, and it exits the If statement.

If <test condition 1> is false, the <statement list 2> runs, and it exits the If statement.

In the example above, if the condition [the value of the Variable A ($A) is greater than 3] evaluates to true then, the statement in the IF block runs.

However, if the Variable A ($A) is less than or equal to 3 or is not an existing variable (the condition is not satsified) then, the If statement does not display a message.

By adding an Else statement, a message is displayed [Output: (The value 3 is less than or equal to 3 or is not created or is not initialized.)]

When the Variable A ($A) is less than or equal to 3 or the condition in the IF block is not satisfied.

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 Else
#==============================================================================
# Creates a variable "A" and assigns a numeric value of 3 to it.
$A = 3

# This is an IF-Else statement that checks if the value of the variable A is greater than 3.
if ($A -gt 3) {
    # If the condition is satisfied it will print the text in front of the CMDLET (Write-Host) to the screen.

    # This is a print to screen CMDLET [Write-Host].
    Write-Host "The value $A is greater than 3."
}
# This is an else statement that runs if no condition is satisfied.
else {
    # If the condition is not satisfied it will print the text in front of the CMDLET (Write-Host) to the screen.

    # This is a print to screen CMDLET [Write-Host].
    Write-Host ("The value $A is less than or equal to 3 or" + " is not created or is not initialized.")
}
#==============================================================================
#EndRegion Else

3 - ElseIf

Below shows the basic ElseIF statement syntax:


if (<test condition 1>)
    {<statement list 1>}
[elseif (<test condition 2>)
    {<statement list 2>}]
[else
    {<statement list 3>}]

When an IF statement is run, the conditional expression is evaluated as either true or false.

If is true, runs, and it exits the If statement.

If is false, the condition specified by the conditional statement is evaluated.

If is true, runs, and it exits the If statement.

If both and evaluate to false, the code block runs, and it exits the If statement.

Multiple ElseIF statements can be used to create a series of conditional tests. Each test is run only if all the previous tests are false.

To further explain the script in this Attune Blueprint, you can use the ElseIF statement to display a message [Output: (The value 3 is equal to 3.)] when the condition [value of Variable A ($A) is equal to 3] is satisfied.

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 ElseIf
#==============================================================================
# Creates a variable "A" and assigns a numeric value of 3 to it.
$A = 3

# This is an if statement block that checks if the value of the variable A is greater than 3.
if ($A -gt 3) {
    # If the condition is satisfied it will print the text in front of the CMDLET (Write-Host) to the screen.

    # This is a print to screen CMDLET [Write-Host].
    Write-Host "The value $A is greater than 3."
}
# This is an ElseIF statement block that checks if the value of the variable A is equal to 3.
elseif ($A -eq 3) {
    # If the condition is satisfied it will print the text in front of the CMDLET (Write-Host) to the screen.

    # This is a print to screen CMDLET [Write-Host].
    Write-Host "The value $A is equal to 3."
}
# This is an else statement block that runs if neither of the above conditions is satisfied.
else {
    # If neither of the above conditions is satisfied it will print the text in front of the CMDLET (Write-Host) to the screen.

    # This is a print to screen CMDLET [Write-Host].
    Write-Host ("The value $A is less than 3 or" + " was not created or initialized.")
}
#==============================================================================
#EndRegion ElseIF