Exercise: Check a Remote PC

Finish two tools for a small office's PCs, Test-PcConnection to say whether each PC answers and whether its remoting door is open, and Get-PcHealth to check free space and the print service right here for your own PC and through Invoke-Command only for another PC whose door is open, while a Pester check stands in for the other PCs so nothing needs remoting turned on.

Freelancer · PowerShell · Operating System · Updated

Back at the small office with three PCs, Monday morning starts the same way every week: is there room left on every drive, and is the print service running everywhere? A tool that answers both for every PC at once, and says plainly which PC it couldn't reach, turns a walk around the office into one line. The catch is that most PCs, your own included, don't have remoting turned on, and a tool that always sends its check through Invoke-Command fails on all of them. In this exercise you build the tool that a careful administrator would: it checks this PC right where it is, checks the door before it knocks on another PC, and skips a PC whose door is closed with a warning instead of a wall of red.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-check-a-remote-pc, 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:

  • OfficeCheck.ps1: the two tools, for you to finish. The health check itself, a script block in $check that reads the free space on C: and the print service's status, is written already.
  • office-pcs.txt: the office, one PC a line. Your own PC plays two of them, as localhost and 127.0.0.1. The third, meeting-room.invalid, plays the PC that's switched off: names that end in .invalid never exist.
  • Check.Tests.ps1: the check. You don't need to change it.

What to do

Open PowerShell 7 in the exercise folder, the one with office-pcs.txt in it, and open it in VS Code with code .. Load the tools and try Get-PcHealth's first try:

. .\OfficeCheck.ps1
Get-PcHealth -ComputerName localhost
OpenError: [localhost] Connecting to remote server localhost failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.

ComputerName FreeSpaceGB PrintSpooler
------------ ----------- ------------
localhost

It sent the check through remoting to the PC it's running on, remoting is off, and the row came back empty. Load the file again with . .\OfficeCheck.ps1 after every change. Then, one job at a time (the README in the folder has every detail):

  1. Test-PcConnection. For a PC's name, output one object with ComputerName, Answers (does a ping come back?) and RemotingOpen (is port 5985 open?), each from Test-Connection ... -Quiet, as in Is It the Network? Test-Connection and Friends.
  2. Get-PcHealth. For this PC (localhost, 127.0.0.1, . or $env:COMPUTERNAME), run the check right here with & $check. For another PC, ask Test-PcConnection first, and use Invoke-Command -ComputerName $name -ScriptBlock $check only when its remoting door is open; otherwise Write-Warning names the PC and it gets no row.
  3. Many PCs, from the pipeline. Both tools take several names, and take them from the pipeline, as in Taking Pipeline Input: a [string[]] parameter with [Parameter(ValueFromPipeline)], and the work in a process block.

Check your work

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

Install-PSResource Pester

Then, from the exercise folder:

Invoke-Pester

At first most tests fail: that's expected, and each job you finish turns its tests green. The check runs your tools on your own PC for real. For the other PCs it uses a Pester mock, a pretend Invoke-Command or Test-PcConnection that answers the way the real one would and remembers how it was called. That's how it can tell that your tool sent the check to front-desk, or skipped back-office, without connecting to anything or turning remoting on.

If PowerShell says the check can't be loaded because it 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.

Hints

  • This PC or another? $name -in 'localhost', '127.0.0.1', '.', $env:COMPUTERNAME answers True or False, capital letters or not.
  • Knock only on an open door. (Test-PcConnection -ComputerName $name).RemotingOpen reads one property of your first tool's answer.
  • Skip and move on. After the warning, continue jumps to the next name in the foreach.
  • A silent PC can be fine. Windows PCs often ignore pings, so Answers can be False for a PC that's on. That's why Get-PcHealth asks about the door, not the ping.

When you're done

The whole office, from the list:

Get-Content -Path .\office-pcs.txt | Test-PcConnection
ComputerName Answers RemotingOpen
------------ ------- ------------
localhost       True        False
127.0.0.1       True        False
meeting-roo…   False        False
Get-Content -Path .\office-pcs.txt | Get-PcHealth
WARNING: Skipped meeting-room.invalid. Its remoting door (port 5985) isn't open.

ComputerName FreeSpaceGB PrintSpooler
------------ ----------- ------------
localhost         805.90 Running
127.0.0.1         805.90 Running

Your free space will be your own drive's. RemotingOpen is True only where remoting is turned on. And the check is all green:

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

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-check-a-remote-pc), 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-check-a-remote-pc.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-check-a-remote-pc.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-check-a-remote-pc, 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

Your tool works on the PC in front of you today, and on every PC in an office tomorrow, because it checks the door before it knocks and says plainly which PC it couldn't reach.

Key terms

Related