Tag Archives: PowerShell Programming language

PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Platforms are Windows PowerShell .NET Framework, Implementation language on C#.

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.

PowerShell remoting over SSH Posh-SSH

As Microsoft announced, SSH will be supported by PowerShell Posh-SSH remoting in Windows 10

This is a third-party solution called Posh-SSH. To use SSH remoting in PowerShell, you must first install the Posh-SSH module from the PowerShell Gallery. It is required that Windows 10 is used, or that the Windows Management Framework 5 is installed.

Install-Module Posh-SSH

Posh SSH can be installed from PowerShell as follows as Administrator.

Install-Module Posh-SSH
Get-Command -Module Posh-SSH
New-SSHSession -ComputerName "203.0.113.4" -Credential (Get-Credential)
Invoke-SSHCommand -Index 0 -Command "uname -a"

This makes it possible to execute commands on the Linux host and copy files.

Posh-SSH is a PowerShell module for session remoting and automating tasks against the system using the SSH protocol (OpenSSH). The module supports only a subset of the capabilities defined in RFCs https://de.wikipedia.org/wiki/Secure_Shell.

PowerShell remoting over SSH

PowerShell remoting normally uses WinRM for connection negotiation and data transport. SSH is now available for Linux and Windows platforms and allows true multiplatform PowerShell remoting.

WinRM for PowerShell

WinRM provides a robust hosting model for PowerShell remote sessions. SSH-based remoting doesn’t currently support remote endpoint configuration and Just Enough Administration (JEA).

PowerShell SSH Posh-SSH remoting

SSH remoting lets you do basic PowerShell session remoting between Windows and Linux computers. SSH remoting creates a PowerShell host process on the target computer as an SSH subsystem. Eventually we’ll implement a general hosting model, similar to WinRM, to support endpoint configuration and JEA.

PowerShell SSH cmdlets

The New-PSSession, Enter-PSSession, and Invoke-Command cmdlets now have a new parameter set to support this new remoting connection.

To create a remote session, you specify the target computer with the HostName parameter and provide the user name with UserName. When running the cmdlets interactively, you’re prompted for a password. You can also use SSH key authentication using a private key file with the KeyFilePath parameter. Creating keys for SSH authentication varies by platform.

PowerShell 6 or higher, and SSH must be installed on all computers. Install both the SSH client (ssh.exe) and server (sshd.exe) so that you can remote to and from the computers. OpenSSH for Windows is now available in Windows 10 build 1809 and Windows Server 2019. For more information, see Manage Windows with OpenSSH. For Linux, install SSH, including sshd server, that’s appropriate for your platform. You also need to install PowerShell from GitHub to get the SSH remoting feature. The SSH server must be configured to create an SSH subsystem to host a PowerShell process on the remote computer. And, you must enable password or key-based authentication.