Sub topic 7
What scripts are used for in support work
This chapter assumes you can read the loops, conditionals and environment variables from the earlier chapters. It shows where those pieces appear in real support tasks, so you can match a task to a script on the exam.
The common use cases
Support work is full of tasks that are repetitive, must be done the same way every time, and are often done on many machines. These are the tasks scripts handle best.
| Use case | What the script does | Typical script type |
|---|---|---|
| Drive mapping | Connects a user to a network share at logon | Batch (.bat) |
| Backups | Copies files to another location on a schedule | Shell (.sh), PowerShell (.ps1) |
| Information gathering | Collects OS version, disk space and user name | PowerShell, Python |
| Installing software or updates | Runs an installer silently on many machines | PowerShell, batch |
| Restarting services | Stops and starts a stalled service | PowerShell, shell |
| Account setup | Creates users and sets permissions | PowerShell, shell |
| Initiating updates | Starts OS or application patching | Any, often through RMM |
Drive mapping at logon
A logon script runs when a user signs in. Mapping a network drive is the classic example. The if statement checks the exit code, so the user or technician learns whether the mapping worked.
@echo off
REM Map a network drive and confirm it
net use Z: \\fileserver\shared /persistent:yes
if %ERRORLEVEL% EQU 0 (
echo Drive Z: mapped.
) else (
echo Mapping failed.
)Backups with a loop
A loop repeats one action for every file. This script copies each text file from a user's Documents folder to a backup location.
#!/bin/bash
# Copy text files from Documents to a backup folder
src="$HOME/Documents"
dest="/mnt/backup"
for f in "$src"/*.txt; do
cp "$f" "$dest"
done
echo "Backup finished for user $USER"Run on a schedule (cron on Linux, Task Scheduler on Windows), it becomes an unattended backup.
Information gathering
Environment variables make a script portable, because the same code works on any machine. This PowerShell script builds a small inventory report.
# Collect basic inventory into a text file
$report = "$env:USERPROFILE\Desktop\inventory.txt"
"Computer: $env:COMPUTERNAME" | Out-File $report
"User: $env:USERNAME" | Out-File $report -Append
Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version | Out-File $report -Append
Get-PSDrive -PSProvider FileSystem | Select-Object Name, Free | Out-File $report -AppendHow a script fits into a support workflow
Why this matters beyond one machine
Scripts save time and reduce human error because they apply the same steps every time. They also scale. A script that takes seconds on one PC can be pushed to hundreds through a remote monitoring and management (RMM) platform, which a later chapter covers. That scale is also the risk. A faulty script can damage many systems quickly, which is the subject of the next chapter.
Exercise
A company wants every employee's PC to connect to the shared finance folder on the file server each time the user signs in. Which script use case is this, and which script type fits a Windows-only environment?
Show answer
This is drive mapping, run as a logon script. A Windows batch file (.bat) using the net use command fits well. A PowerShell script (.ps1) would also work.
Exercise
A script copies files with a for loop and runs every night at 2 a.m. Which use case is this, and which tool schedules it on Linux?
Show answer
It is a backup. On Linux, cron schedules it. On Windows, Task Scheduler does the same job.