Sub topic 7
What scripts are used for in support work
This chapter assumes you can read a short script, which earlier chapters covered: comments, variables, loops and conditionals. Here you will match real support tasks to scripts. You will also see what each script type does well and where a script can cause harm.
The common use cases
The A+ Core 2 exam lists a fixed set of script use cases. They share one pattern: a task that is repetitive, must be done the same way every time, or must run on many machines.
| Use case | Typical script type | Example command |
|---|---|---|
| Basic automation | .bat, .ps1, .sh, .py | A loop that processes every file in a folder |
| Restarting machines | .bat, .ps1, .sh | `shutdown /r /t 60` or `sudo reboot` |
| Remapping network drives | .bat, .ps1 | `net use Z: \\fileserver\shared` |
| Installing applications | .ps1, .bat, .sh | `winget install`, `apt install` |
| Automated backups | .bat, .ps1, .sh | `robocopy`, `cp`, `rsync` |
| Gathering information | .ps1, .sh, .py | `Get-CimInstance`, `uname -a` |
| Initiating updates | .ps1, .sh | `apt upgrade`, update tools |
Scripts also feed other tools. An RMM platform, covered in a later chapter, often pushes a script to many endpoints and collects the results.
Basic automation
Basic automation means turning a manual sequence into a script. A technician who renames 200 files by hand will make mistakes. A loop does not. The Python example below renames every .log file in a folder so each name includes the date:
import os
from datetime import date
folder = os.path.join(os.path.expanduser("~"), "Logs")
stamp = date.today().isoformat()
for name in os.listdir(folder):
if name.endswith(".log"):
base = name[:-4]
new_name = f"{base}-{stamp}.log"
os.rename(os.path.join(folder, name), os.path.join(folder, new_name))
print("Renamed", name, "to", new_name)The loop covers repetition. The if filters on the file extension.
Restarting machines
Restarts are common after installs and updates. Windows uses shutdown. Using /t gives the user time to save work, and /c shows a message.
@echo off
REM Restart in 60 seconds and tell the user why
shutdown /r /t 60 /c "Maintenance restart in 60 seconds. Save your work."If you start one by mistake, shutdown /a aborts it during the countdown. On Linux, sudo reboot or sudo shutdown -r +1 does the same job.
Remapping network drives
A mapped drive is a drive letter that points to a shared folder on a file server. Logon scripts often remap drives so users always see the same letters. The net use command does this in a batch file. The /persistent:yes switch keeps the mapping after a restart.
PowerShell does the same with New-PSDrive. The -Persist switch makes the mapping visible in File Explorer and keeps it across restarts:
# Remap Z: only if it is missing
if (-not (Test-Path "Z:\")) {
New-PSDrive -Name "Z" -PSProvider FileSystem -Root "\\fileserver\shared" -Persist
} else {
Write-Output "Z: already mapped."
}The conditional prevents an error when the drive already exists. Checking the current state before changing it is a habit worth building.
Installing applications
An unattended install runs without prompts, so a script can install the same software on many machines. Windows 10 and 11 include the winget package manager:
# Silent install of Firefox, matching the exact package ID
winget install --id Mozilla.Firefox --exact --silentLinux package managers work the same way. On Debian-based systems, sudo apt install -y firefox installs without asking for confirmation. Silent switches such as /quiet or -y matter because a prompt would make the script hang while it waits for input.
Automated backups
A backup script copies important data to a second location. It can run on a schedule through Windows Task Scheduler or Linux cron. A good backup script checks whether the copy succeeded.
#!/bin/bash
# Back up Documents into a dated folder and report the result
src="$HOME/Documents"
dest="/mnt/backup/docs-$(date +%F)"
mkdir -p "$dest" || exit 1
if cp -r "$src"/. "$dest"; then
echo "Backup OK: $dest"
else
echo "Backup failed" >&2
exit 1
fiThree details make this script safer than a bare cp:
- The folder name includes the date, so one run does not overwrite the last.
mkdir -pcreates the destination if it is missing.- A non-zero exit code (
exit 1) tells any calling tool, such as a scheduler or RMM platform, that the run failed.
A backup you never test is only a hope. Periodically restore a file from the backup to confirm it works.
Gathering information
Information-gathering scripts collect facts about a machine: hostname, operating system, free disk space, last boot time. Run across a fleet, they produce an inventory without visiting each desk.
$os = Get-CimInstance Win32_OperatingSystem
$report = [PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
User = $env:USERNAME
OS = $os.Caption
FreeGB = [math]::Round((Get-PSDrive C).Free / 1GB, 1)
LastBoot = $os.LastBootUpTime
}
$report | Export-Csv -Path "$env:TEMP\inventory.csv" -NoTypeInformation
$reportThe script reads environment variables ($env:COMPUTERNAME, $env:USERNAME) and stores structured data in a variable. It then writes a CSV file that a spreadsheet can open. On Linux, uname -a, df -h and uptime give similar facts.
Initiating updates
Scripts can start OS and application updates at a planned time. On Debian-based Linux the sequence is:
sudo apt update
sudo apt upgrade -yapt update refreshes the list of available packages. apt upgrade installs the new versions. Scripted updates usually pair with a restart step and a log, because some updates only take effect after a reboot.
Risks to weigh before you run anything
The exam expects you to know three considerations. Each one ties back to a use case above.
- Unintentionally introducing malware. A script downloaded from an unknown source can contain a malicious payload. Run only scripts from a trusted source, and read the code first. If a script downloads and runs another file, check where that file comes from.
- Inadvertently changing system settings. A script runs with the permissions of whoever starts it. A cleanup script that deletes the wrong path, or a settings script that edits the registry, can change many machines at once. Test on one non-production machine first.
- Browser or system crashes due to mishandling of resources. A loop with no exit condition can use all the CPU. A script that opens files or connections without closing them can drain memory. A runaway process can freeze a workstation.
The risks are why the life cycle diagram has a test step. Scripts are fast, so a mistake spreads quickly.
Choosing a script type for a task
The right type depends on the platform and the job.
- Use .bat for simple tasks on Windows, such as drive mapping or a quick restart.
- Use .ps1 (PowerShell) for deeper Windows work, such as system information and application installs. It handles structured data well.
- Use .sh for Linux and macOS, such as backups and updates.
- Use .py when the same script must run on several operating systems, or the logic is complex.
Common mistakes
- Hard-coding values. A path like
C:\Users\Samfails on any other account. Use environment variables such as%USERPROFILE%or$HOME. - Ignoring exit codes. A script that always prints "Done" hides failures. Test the result of each important command.
- Running on production first. Always test on one safe machine.
- Leaving prompts in unattended scripts. An install that waits for a click never finishes. Use silent switches.
Exercise
A technician must make 40 laptops reconnect the user's shared drive at every logon. The drive letter is Z: and the share is \\fileserver\shared. Name the script use case, one suitable script type, and the first safety step before deploying it.
Show answer
The use case is remapping network drives. A batch (.bat) or PowerShell (.ps1) script works, for example net use Z: \\fileserver\shared /persistent:yes. Before deploying, test the script on one non-production machine to confirm it maps the drive and does not change other settings.
Exercise
A backup script prints "Backup complete" every night, but a user later finds the backup folder empty. Which scripting practice was missing, and what would fix it?
Show answer
The script did not check the result of the copy command. It should test the exit code (for example with an if statement around cp) and print an error message and exit with a non-zero code on failure. That way a scheduler or RMM tool can flag the failed run.
Exercise
Which of the three script risks applies when a script contains a while loop whose condition never becomes false and the workstation freezes?
Show answer
Browser or system crashes due to mishandling of resources. The endless loop consumes CPU and may exhaust memory. A loop needs a condition that eventually ends it.