Scriptin' with Bash and PowerShell

Skip to content

This here’s a machine-translated text that might contain errors!

If ya done the same task fer the third time, it’s high time to automate it. Scriptin’ is all about lettin’ the machine do the dull work fer ya, faster and without forgettin’ a thing.

Why Automate?

Some tasks in IT operations are done often:

  • Updatin’ servers
  • Creatin’ users
  • Backin’ things up
  • Restartin’ services after an update
  • Checkin’ if services are runnin’

Dooin’ these by hand every time takes time, and it’s easy to forget a step. A script does the same thing every time, without complainin’.

Bash: Scriptin’ in Linux

Bash be the shell (the terminal) in most Linux distros, and it can run scripts too. A Bash script is nuthin’ more than a plain ol’ text file with commands that run one after another.

Yer First Script

Make a file called oppdater.sh:

#!/bin/bash
# Update that list o' packages and install them updates
echo "Oppdaterer systemet..."
sudo apt update
sudo apt upgrade -y
echo "Ferdig!"

Make that file executable and run it:

# Make that script run, partner
chmod +x oppdater.sh
# Kick off that updatein' business
./oppdater.sh

#!/bin/bash

That first line there’s called a shebang. It tells the operatin’ system to run the file with Bash. Without it, the system ain’t got a clue which program’s supposed to read the script.

Variables and Input

#!/bin/bash
USERNAME=$1

if [ -z "$USERNAME" ]; then
    echo "Use: ./create_user.sh <username>"
    exit 1
fi

sudo adduser "$USERNAME"
sudo usermod -aG sudo "$USERNAME"
echo "User $USERNAME created with sudo access."

Now, this here script takes a username as an argument ($1 is the first argument, ya see). If ya run ./lag_bruker.sh ola, it’ll go and create the user ola and give ‘em sudo access.

Handy Bash Concepts

Concept Example What it does
Variable NAVN="Ola" Holds onto a value
Argument $1, $2 Input from the command line
If-check if [ -f "fil.txt" ]; then Checks if a file’s out there
For-loop for f in *.log; do Does somethin’ for every matchin’ file
Pipe cat logg.txt \| grep "ERROR" Sends one command’s output to another

PowerShell: Scriptin’ in Windows

PowerShell’s Windows’ answer to Bash, but it’s more object-oriented. It works with objects instead of just text, makin’ it powerful for managin’ Windows systems.

Example: Check Disk Space

Now, listen up, partner. Checkin’ yer disk space is mighty important. If yer hard drive’s full, things ain’t gonna run smooth. Here’s how ya do it:

  • Windows: Right-click on yer drive in File Explorer and select “Properties.” It’ll tell ya how much space ya got used and how much is free.
  • macOS: Click the Apple menu, then “About This Mac,” then “Storage.” That’ll show ya what’s takin’ up space.
  • Linux: Open a terminal and type df -h. That’ll give ya a list of yer disks and how full they are.

Keep an eye on that space, and ya won’t run into trouble.

# Check if there's enough room in the saddle
Get-PSDrive -PSProvider FileSystem | Select-Object Name, 
    @{Name="Brukt (GB)"; Expression={[math]::Round($_.Used / 1GB, 2)}},
    @{Name="Ledig (GB)"; Expression={[math]::Round($_.Free / 1GB, 2)}}

Example: Installin’ software with Winget

# Install them many programs all at once
winget install Mozilla.Firefox
winget install Microsoft.VisualStudioCode
winget install Git.Git
winget install Python.Python.3.12

This here’s a whole lot faster than downloadin’ and installin’ ever’thin’ by hand. Ya can save the list in a script and run it ever’ time ya set up a new machine.

Easy Task 1 - Build an Update Script

Write a Bash script that:

  1. Updates the package list
  2. Installs all available updates
  3. Restarts Nginx (or another service you’re runnin’)
  4. Prints a message when it’s done

Run it on one of yer VMs and see if it works right.

Easy Task 2 - Automate Machine Setup

Create a PowerShell script (.ps1) or a simple list of winget install commands for all the programs ya need on a fresh PC. Consider:

  • Which programs do ya always install?
  • Can ya also configure some settings with the script?

Next time ya set up a machine, just run the script instead of spendin’ an hour on manual installation.

Summin’ It Up

  • If ya find yerself doin’ somethin’ more’n twice, reckon ya oughta automate it.
  • Bash (Linux) and PowerShell (Windows) are the most common tools for scriptin’.
  • Bash scripts are just text files with commands that run one after the other.
  • PowerShell works with objects and is mighty powerful for Windows managin’.
  • Variables, arguments, if-checks, and loops are the main buildin’ blocks.