0
0
PowerShellscripting~30 mins

Why best practices improve reliability in PowerShell - See It in Action

Choose your learning style9 modes available
Why Best Practices Improve Reliability
📖 Scenario: You are working as a system administrator. You want to create a simple PowerShell script that processes a list of server names and checks if they are reachable. To make your script reliable, you will follow best practices step-by-step.
🎯 Goal: Build a PowerShell script that stores server names, sets a timeout value, checks each server's reachability using a loop, and finally prints the reachable servers.
📋 What You'll Learn
Create a list of server names in a variable called $servers with exact values
Create a variable called $timeout with the value 2
Use a foreach loop with variable $server to check reachability using Test-Connection with -Count 1 and -TimeoutSeconds $timeout
Store reachable servers in a list called $reachableServers
Print the list of reachable servers using Write-Output
💡 Why This Matters
🌍 Real World
System administrators often need to check if servers are online before running maintenance scripts. Using best practices like setting timeouts and looping through servers reliably helps avoid script failures.
💼 Career
This project teaches foundational scripting skills used in IT automation, monitoring, and troubleshooting tasks common in many technical jobs.
Progress0 / 4 steps
1
Create the list of servers
Create a variable called $servers and assign it an array with these exact server names: 'server1', 'server2', 'server3'.
PowerShell
Need a hint?

Use @() to create an array in PowerShell.

2
Set the timeout value
Create a variable called $timeout and set it to 2 (seconds).
PowerShell
Need a hint?

Just assign the number 2 to $timeout.

3
Check server reachability
Create an empty array called $reachableServers. Use a foreach loop with variable $server to iterate over $servers. Inside the loop, use Test-Connection with -Count 1 and -TimeoutSeconds $timeout to check if $server is reachable. If reachable, add $server to $reachableServers.
PowerShell
Need a hint?

Use -Quiet with Test-Connection to get a simple True/False result.

4
Print reachable servers
Use Write-Output to print the $reachableServers array.
PowerShell
Need a hint?

Use Write-Output $reachableServers to display the list.