PowerShell 102 – Practice scripts

In the last PowerShell 101 I discussed the basics for PowerShell. Since I totally dislike learning stuff from theory only, this PowerShell 102 is going to be all practice. I’ve got 2 small labs lined up for you. I’ve also directly provided you with answer script files. It’s up to you to choose whether you want try for yourself or go the easy way and directly download the answers ;-). I’ve included extensive commenting in the script about what I’m doing.

That brings me to the only thing I’d like to discuss before going on to the labs. People tend to discuss how scripts should be written a lot. The coding style should be in a certain way, everything should be written as short as possible, on the least amount of lines and conserving the most space. In my opinion if the script works you’re good. By the time you get more experience with PowerShell you’ll develop a coding style. But there is no such thing as “the correct coding style”. The same goes for writing everything as short as possible. Yes it conserves disk space and yes your script will be shorter. It does however make things more complex which in turn makes the script less readable. Since those are 2 things you’d rather not have as a rookie I’m going to advise you to just do whatever you find easy to use.

Each lab will consist of a scenario with some requirements and will require you to write a script.

Lab 01 – Checking network connectivity

You’ve got an intermittent connectivity issue on a set of your servers but you can’t figure out the root cause. So for troubleshooting you want to setup a couple of scripts that ping multiple locations to see if the issue occurs for all of the locations. The script should be able to:

  • Easily switch between locations
  • Set a location to log the attempts for connection
  • Output should be in csv so you can easily import it in excel for analysis
  • It should log both the time of the attempt and the result
  • The amount of time in between attempts should also be easily switchable

Answer script file

Lab 02 – Retrieving last boot time of machines

You’ve made some changes using a GPO to a set of machines. It won’t be active until they’ve been restarted. You want to check which machines still need to be rebooted. The script needs to:

  • Search Active Directory for the machines to query
  • Output the result of the query (last boot time & servername) to screen

Answer script file

 

I hope you enjoyed the challenges. Keep visiting my blog for more info or subscribe for notifications about new blogs.

 

PowerShell 1XX post series

  1. PowerShell 101 – The absolute basics
  2. PowerShell 102 – Practice Scripts (current)
  3. PowerShell 103 – Pipelines
  4. PowerShell 104 – Operators

3 thoughts on “PowerShell 102 – Practice scripts

Leave a Reply