Scriptin' with Bash and PowerShell,

Skip to content

Avast ye! This be a machine-translated text, an’ it may contain errors, aye!

If ye’ve done the same task for the third time, ‘tis time to automate it. Scriptin’ be about lettin’ the machine do the dreary work for ye, faster and without forgettin’ a thing.

Why Automate?

Some tasks in IT operations be done often:

  • Update the servers
  • Create users
  • Take backup
  • Restart services after an update
  • Check that services be runnin’

Do’n these by hand each time takes time, an’ it be easy to forget a step. A script does the same each time, without complainin’.

Bash: Scriptin’ in Linux

Bash be the shell (the terminal, aye) in most Linux distributions, an’ it can also run scripts. A Bash script be nothin’ more than a text file with commands that be run in order, savvy?

Yer First Script

Create a file named oppdater.sh:

#!/bin/bash
# Update the booty list and install the upgrades
echo "Oppdaterer systemet..."
sudo apt update
sudo apt upgrade -y
echo "Ferdig!"

Make the file executable and run it, ye scallywag:

chmod +x update.sh # Make the script runnable, matey!
./update.sh # Run the script, arrr!

#!/bin/bash

The first line be called a shebang, aye. It tells the operating system that the file be runnin’ with Bash. Without it, the system knows not which program to interpret the script with.

Variables and Booty

A ship needs its stores, aye? Variables be like the hold o’ yer vessel – they keep the treasures ye need for yer calculations. Input, then, be the goods ye load onto the ship from port, the information ye give the vessel to work with.

Think o’ it this way: ye might have a variable for the number o’ cannons (antallKanoner), and ye’d input the actual count – say, 10 – when ye prepare for a skirmish. Or a variable for the amount o’ grog (grogBeholdning), and ye input how many barrels ye’ve got aboard.

These variables and inputs be crucial for any calculation or task yer ship – or yer program – needs to perform. Without ‘em, ye’d be sailin’ blind!

#!/bin/bash
USERNAME=$1

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

sudo adduser "$USERNAME"
sudo usermod -aG sudo "$USERNAME"
echo "User $USERNAME be created with sudo-access, arrr!"

Here be the script takin’ a username as an argument ($1 be the first argument, ye see). If ye run ./lag_bruker.sh ola, it’ll create the user ola an’ grant ‘em sudo access, aye.

Useful Bash Concepts, Ye Scallywag

Concept Example What it does, Arrr!
Variable NAVN="Ola" Stores a treasure value
Argument $1, $2 Input from the command line, matey
If-check if [ -f "fil.txt" ]; then Checks if a file be found in the depths
For-loop for f in *.log; do Does somethin’ for every file that matches
Pipe cat logg.txt \| grep "ERROR" Sends output from one command to another, shiver me timbers

PowerShell: Scriptin’ in Windows

PowerShell be Windows’ answer to Bash, but ‘tis more object-oriented, aye. It works with objects instead o’ just text, makin’ it powerful for managin’ Windows systems.

Example: Check the disk space, ye scallywag!

# Check if there be free booty on the disk
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’ the booty with Winget

# Install several programs at once, aye!
winget install Mozilla.Firefox
winget install Microsoft.VisualStudioCode
winget install Git.Git
winget install Python.Python.3.12

Aye, this be much swifter than downloadin’ an’ installin’ everythin’ by hand. Ye can save the list in a script an’ run it each time ye be settin’ up a new vessel.

Easy Task 1 - Craft an Updatin’ Script

Write a Bash script that:

  1. Updates the package list
  2. Installs all available updates
  3. Restarts Nginx (or another service ye be runnin’)
  4. Prints a message when ‘tis done

Run it on one o’ yer VMs and see that it works, aye.

Easy Task 2 - Automate Machine Setup

Create a PowerShell script (.ps1) or a simple list o’ winget install commands for all the programs ye need on a new PC. Consider this, ye scallywags:

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

Next time ye be settin’ up a machine, just run the script instead o’ spendin’ an hour on manual installation.

Summary

  • If ye be doin’ somethin’ more than twice, consider automatizin’ it
  • Bash (Linux) and PowerShell (Windows) be the most common tools for scriptin’
  • Bash scripts be text files with commands that be run in order
  • PowerShell works with objects and be powerful for Windows administration
  • Variables, arguments, if checks, and loops be the most important buildin’ blocks