Exercise: Rename the Telemetry

Write three small tools for Mission Control, a script that renames every .tmp file in a folder, one that pulls the warnings out of a flight log and a launch countdown, then run them on the real files while a Pester check tells you what's done.

Apprentice · PowerShell · Updated

It's launch day at Mission Control, and the ground station needs three small tools. The satellite's 30 telemetry files came in ending in .tmp instead of .log, the flight director wants every warning from last night's flight log in one short report, and the launch needs a countdown. You'll write each one as a script, a small application that works on whatever folder, file or number it's given, then run the first two on the real files. A Pester check tries each script on files of its own and tells you what's done.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-rename-the-telemetry, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder. Or clone it with Git.

The folder holds:

  • Rename-Telemetry.ps1, Get-Warnings.ps1 and Start-Countdown.ps1: the three scripts. Each starts with a comment that says exactly what it must do, and its param() line is already written. These are the files you finish.
  • telemetry: the 30 telemetry files, all ending in .tmp. One has a space in its name.
  • flight-log.txt: last night's flight log, with notes from the night shift and a few lines after the end.
  • README.md: the four jobs, and tips.
  • Check.Tests.ps1: the check. You don't need to change it.

What to do

Open the exercise folder in VS Code (Writing Scripts in VS Code), or open PowerShell 7 in it, and run every command from there. Then do the four jobs in the folder's README.md:

  1. Rename-Telemetry.ps1 -Path FOLDER renames every .tmp file in the folder it's given so it ends in .log, with a foreach loop. It outputs a line for each file, such as Renamed orbit-01.tmp to orbit-01.log, and ends with Files renamed: 30 (or however many it renamed). Run a second time, with nothing left to rename, it outputs Files renamed: 0 and no error.

  2. Get-Warnings.ps1 -Path LOGFILE reads the log one line at a time with a foreach loop, skips the notes that start with # with continue, stops at the line END OF FLIGHT with break, outputs every other line with WARNING in it exactly as written, and ends with Warnings found: 3 (or however many it found).

  3. Start-Countdown.ps1 -From NUMBER counts down with a for loop, from T-minus and the number it's given down to T-minus 1, then outputs Liftoff!.

  4. Use them. Try Rename-Telemetry.ps1 on a copy first (Copy-Item telemetry practice -Recurse, then .\Rename-Telemetry.ps1 -Path practice, then Remove-Item practice -Recurse). When the checks for a script pass, run it on the real files:

    .\Rename-Telemetry.ps1 -Path telemetry
    .\Get-Warnings.ps1 -Path flight-log.txt | Set-Content warnings.txt
    

Each script works on its -Path or -From (Parameters: param()), never on a folder or file name written into the script. Here is what the countdown should say when it's done:

.\Start-Countdown.ps1 -From 5
T-minus 5
T-minus 4
T-minus 3
T-minus 2
T-minus 1
Liftoff!

Check your work

The check uses Pester, PowerShell's test tool. If you haven't installed it yet, install it once:

Install-PSResource Pester

Then, from the exercise folder:

Invoke-Pester

At first almost every test fails. That's expected: each job you finish turns its tests green. The check tries your scripts on made-up files of its own, in a throwaway folder, and only the last two tests look at your telemetry folder and warnings.txt. A failing test says what to look at, for example Expected strings to be the same, because the check's log has two warnings, so the last line is Warnings found: 2. Count each warning you output with ++, and output the count after the loop, but they were different.

If PowerShell says a script can't be loaded because running scripts is disabled or the file is not digitally signed, Windows has marked the downloaded files as coming from the internet. From the exercise folder, run Get-ChildItem -Recurse | Unblock-File once, then try again (Why a Script Won't Run: The Execution Policy).

Hints

  • The renaming loop is the one from For Each Thing: foreach, with Get-ChildItem -Path $Path -Filter *.tmp as its list, so it works on any folder it's given.
  • Look before you rename: put -WhatIf on Rename-Item and run the script on your practice copy.
  • The counts start at 0 before the loop and grow with ++ inside it (Stopping Early: break and continue). A loop over an empty list makes no passes, which is what makes a second run safe.
  • Skip, then stop: in Get-Warnings.ps1, check for # with continue first, then for END OF FLIGHT with break, then for WARNING with -like '*WARNING*'.
  • A countdown's three parts go in the for loop's parentheses: a start, a test and a step, as in Until Something Is True: while, do and for.

When you're done

Every test is green, and Pester's last line says:

Tests Passed: 14, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

Now look back at Rename-Telemetry.ps1. It turns .tmp into .log and nothing else. What would you add to its param() so the same script could turn .JPG into .jpg in your photos folder, without writing a new one?

Want the whole mission? Play Mission Control from the start in Terminal Quest: play it on the game's page. It has the telemetry and the sensor check from this chapter, a beacon script that takes a callsign with param(), and the launch itself. One goal stops a runaway program with Get-Process and Stop-Process, which are new here; the Step by step hints show you how. The game keeps your place in this browser, so you can stop and come back.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-powershell-rename-the-telemetry), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (PowerShell works), send your work to your new repo. If you downloaded the ZIP:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-powershell-rename-the-telemetry.git
    git push -u origin main
    

    If you cloned the starter instead, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-powershell-rename-the-telemetry.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-powershell-rename-the-telemetry, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

You wrote three tools that do in a second what would take an hour by hand, on 30 files or 3,000, and they're safe to run again tomorrow: that's what a script that's an application looks like.

Key terms

Related