All posts by Don Matteo

lebt in Zürich, ist System Engineer MCP bei A-Enterprise GmbH. Mitglied des UNBLOG Network. Author und Blogger zu den Themen, Linux und Open Source. Tutorials für Windows, VMware, Synology, Fortinet.

PowerShell Hands-On, for loop while loop $array

for loop and while loop in $array are often used including when working with PowerShell scripts. In this tutorial you will show most used example.

PowerShell Hands-On, for loop while loop $array

In the following four examples where array values are created in different loops, these are called up again using the ID

an array can look like this:

$array = @("seattle","paris","bangkok","tokio")
Write-Host $array[0,1,2,3]

Output values with For Loop Array:

The length of the array, or the number of stored values, is read out with $array.length. The variable ($i) serves as a counter to count when to exit the loop. A start value is assigned to the counter ($i=0).The start value should increase by 1 each time the loop passes ($i++) until the final value is reached. The final value is the length of the array ($array.length). When checking the final value, there is a condition: as long as $i is less than the number of values ($i -lt $array.length).

For loop

for ($i=0; $i -lt $array.length; $i++){
  Write-Host $array[$i] 
}

The For loop: for ($i=0; $i -lt $array.length; $i++)
Start value $i=0; The variable $i starts with a value of 0
is $i smaller (-lt) $i -lt $array.length condition: the For loop is executed as long as this condition is met: as long as the variable $i is less than $array.length, so as long as $i is less than 4. The action at the loop pass: $i++ means to increase the value of the variable $i by 1, with each pass of the loop $i increases by 1: 0 .. 1 .. 2 .. 3 …

while loop

$i=0
while ($i -lt $array.length){
  Write-Host $array[$i] 
  $i++
}

Example with starting value $i defined before the loop ($i=0)
while ($i -lt $array.length)

Within while is the condition for the loop pass, which loop wid does not leave as long as it is fulfilled:
$i -lt $array.length … as long as $i is smaller $array.length
The variable $i is incremented by 1 within the loop: $i++

Endless Loop

while can be used for an infinite loop as follows: with break, the infinite loop can be exited again. The following example goes through the loop until break is executed, this happens when $i is no longer less than 10:

$i=0
while($true) {
  $i++
  write-host $i
  if ($i -ge 10) {break}
}

do loop

$i=0
do{
  Write-Host $array[$i] 
  $i++
} while ($i -lt $array.length)

Foreach

foreach ($i in $array){
  Write-Host $i 
}

foreach ($i in $array) call all values of the array ($array). The variable $i contains the currently read value for each pass.

Operator Variations:
-gt greater than
-igt greater than, case-insensitive
-cgt greater than, case-sensitive
-ge greater than or equal
-ige greater than or equal, case-insensitive
-cge greater than or equal, case-sensitive
-lt less than
-ilt less than, case-insensitive
-clt less than, case-sensitive
-le less than or equal
-ile less than or equal, case-insensitive
-cle less than or equal, case-sensitive

Conclusion

In this tutorial you learn, how to use for loop and while loop in $array they are often used when working in PowerShell scripting.

Linux Splash Screen Linux Logo

Linuxlogo is a Linux command-line utility that creates a colored ANSI image of the distribution logo as a splash screen with the most important system information.

Creating a Linux Logo splash screen

Here, the file issue.net or motd makes sense to output a splash screen of the host, motd stands for (“message of the day”). These files, which are used by the login process, are located under the configuration directory /etc and output a message after a successful login – but before the start of the respective login shell.

# redhat CentOS
$ sudo yum -y install linux_logo
# Debian Ubuntu
$ sudo apt-get -y install linuxlogo
# Fedora
$ sudo dnf -y install linux_logo

The autostart configuration rc.local generates the necessary entries during the boot process,

$ sudo vi /etc/rc.d/rc.local
# Debian & Ubuntu
$ sudo vi /etc/rc.local

for the console login with the file issue and the remote login issue.net.

if [ -f /usr/bin/linux_logo ]; then
     echo "" > /etc/issue
     /usr/bin/linux_logo -L debian_banner -u >> /etc/issue
     echo >> /etc/issue
Fi
if [ -f /usr/bin/linux_logo ]; then
     echo "" > /etc/issue.net
     /usr/bin/linux_logo -L debian_banner -a -u >> /etc/issue.net
     echo >> /etc/issue.net
Fi

On Debian and Ubuntu run the following command:

$ /usr/bin/linuxlogo -L debian_banner_2 -u

Make rc.local be executable.

$ sudo chmod +x /etc/rc.d/rc.local

In order for the splash screen to appear in the SSH terminal during remote login, you have to activate the banner of the SSH daemon.

$ sudo vi /etc/ssh/sshd_config

Remove the hash (#) line (about 108) and add issue.net.

# no default banner path
Banner /etc/issue.net

The change takes effect when the SSH daemon is restarted.

# CentOS 7
$ sudo systemctl restart sshd
# CentOS 6
$ sudo service sshd restart

Where, for legal reasons, a notice is intended to warn against unauthorized access to the console, the /etc/motd file can produce the following output:

$ sudo vi /etc/motd
##############################################################################
# Unauthorized access to this system is prohibited !             #
#                                  ******                                    #
# This system is actively monitored and all connections may be logged.    #
# By accessing this system, you consent to this monitoring.          #
##############################################################################

It is also a good idea to output the splash screen with Linux logo from the ~/.bashrc, here is an example of the system-wide bash configuration of the redhat distribution.

$ sudo vi /etc/bashrc

Insert the line at the end of the ~/.bashrc file.

if [ -f /usr/bin/linux_logo ]; then linux_logo -L redhat -u; Fi

Linuxlogo can be extended with the following line in ~/.bashrc

if [ -f /usr/bin/linuxlogo ]; then linuxlogo ;hostnamectl status | grep "Operating System";fi
uname -sor

Linux Logo Parameters

Distributions:

/usr/bin/linux_logo -L list

Available Built-in Logos:
        Num Type Ascii Name Description
        1 Banner Yes bsd_banner FreeBSD logo
        2 Classic Yes irix Irix logo
        3 Classic Yes bsd FreeBSD logo
        4 Banner Yes solaris The Default Banner Logos
        5 Classic Yes aix AIX logo
        6 Classic Yes classic The Default Classic Logo
        7 Banner Yes debian_banner Debian Banner (white)
        8 Banner Yes mandrake_banner Mandrake(TM) Linux Banner
        9 Banner Yes pld PLD Linux banner
        10 Banner Yes suse SUSE Logo
        11 Banner Yes sourcemage_ban Source Mage GNU/Linux banner
        12 Classic Yes gnu_linux Classic GNU/Linux
        13 Classic Yes core Core Linux Logo
        14 Classic Yes debian Debian Swirl Logos
        15 Banner Yes slackware Slackware Logo
        16 Banner Yes mandriva Mandriva(TM) Linux Banner
        17 Banner Yes redhat RedHat Banner (white)
        18 Banner Yes ubuntu Ubuntu Logo
        19 Classic Yes debian_old Debian Old Penguin Logos
        20 Banner Yes sme SME Server Banner Logo
        21 Banner Yes sourcemage Source Mage GNU/Linux large
        22 Banner Yes mandrake Mandrakelinux(TM) Banner
        23 Banner Yes banner The Default Banner Logo
        24 Classic Yes classic-simp Classic No Dots Or Letters
        25 Classic Yes classic-nodots The Classic Logo, No Periods
        26 Banner Yes banner-simp Simplified Banner Logo

Do "linux_logo -L num" where num is from above to get the appropriate logo.
Remember to also use -a to get ascii version.

Linux Logo Help Screen

/usr/bin/linux_logo -h

Linux Logo Version 5.11 using libsysinfo 0.2.1
        by Vince Weaver <vince@deater.net>
   Newest Versions at:
      https://www.deater.net/weave/vmwprod/linux_logo
      https://metalab.unc.edu/pub/Linux/logos/penguins

Usage: /usr/bin/linux_logo [-a] [-b] [-c] [-d] [-D file] [-e file] [-f] [-g]
                    [-h] [-i] [-k] [-l] [-n] [-o num] [-p] [-s] [-t str] [-u] [-v]
                    [-w Num] [-x] [-y] [-F format] [-L num | name | list | random_xy]
         [-a] -- Display on ascii-only logo
         [-b] -- Display a Banner Logo!
         [-c] -- Display a "Classic" type logo
         [-d] -- disable "prettying" of output
         [-D file]-- use custom logo from "file"
         [-e file]-- Use "file" instead of /proc/cpuinfo [for debugging]
         [-f] -- force the screen clear before drawing
         [-F format] Format output.  See README.
      B [-g] -- give system info only
         [-h] -- this help screen
         [-i] -- ignore ~/.linux_logo and /etc/linux_logo.conf
         [-k] -- keep sysinfo flushed-left (non-centered)
      B [-l] -- display logo only
      C [-o Num] -- offset output Num spaces to the right
         [-p] -- preserve cursor location
         [-s] -- skip Bogomips [speeds up on non-Linux platforms]
         [-t str] -- display user-supplied string
      * [-u] -- show uptime
         [-v] -- version information
         [-w Num] -- set width of screen to Num [default 80]
      * [-y] -- show load average
         [-L num | name | list | random_xy] -- multiple logo options.  Lake README

B=Banner mode only, C=Classic Mode Only *=Works Only in Linux