Sub topic 1
Scripting Basics: Why Technicians Script
Prerequisites
You should know basic Windows and Linux command-line use, file paths, IP addresses, ports, and firewalls.
Why Technicians Script
A script is a text file containing commands that an operating system runs in sequence. Technicians use scripts to automate repeated work, reduce typing errors, and apply the same procedure to many computers. Common tasks include collecting system information, mapping network drives, and backing up files.
Recognizing Script Types by Extension
The file extension often identifies the scripting language and platform.
| Extension | Common script type | Typical platform |
|---|---|---|
| `.bat` | Windows batch script | Windows Command Prompt |
| `.ps1` | PowerShell script | Windows and other systems with PowerShell |
| `.sh` | Shell script | Linux and macOS |
| `.py` | Python script | Multiple platforms with Python installed |
The extension suggests how to run the file, but permissions, execution policy, and installed software can also matter.
Comments and Variables
A comment documents a script without being executed. Batch uses REM, PowerShell uses #, and shell and Python scripts also use #.
A variable stores a value that the script can reuse. In the example, $name and $count are variables.
# Check a workstation several times
$name = "workstation01"
$count = 3
$active = $true
foreach ($i in 1..$count) {
Write-Output "$name: check $i"
}
if ($active) {
Write-Output "The workstation is active."
}Environment Variables
An environment variable is a value supplied by the operating system or user. It can store configuration such as a search path, home folder, or username. Scripts can read these values instead of storing system-specific paths.
In Windows Command Prompt, display PATH with echo %PATH%. In Linux or macOS, use echo $PATH. PowerShell uses $env:PATH.
Basic Data Types
A string is text, such as "server01". An integer is a whole number, such as 3. A Boolean is either true or false. Scripts use these values in messages, calculations, and decisions.
Loops and Conditionals
A loop repeats commands. A conditional runs commands only when a test is true. In the example, foreach repeats the status check, and if tests $active.
Exercise
Which construct repeats a command, and which construct makes a decision?
Show answer
A loop repeats a command. A conditional, such as if, makes a decision based on a test.