The Chairman realizes that many IT Pros who take on his challenges are experienced and advanced PowerShell users. But everyone was a beginner once. Everyone had that first challenge of writing a PowerShell function. Today’s challenge is for those of you in the early stages of learning PowerShell. Now, it is time for you to begin scripting. For this beginner challenge, the Chairman would like you to write 2 very simple functions.
You should write a function to convert a Fahrenheit temperate to Celsius and a second function to do the opposite. The functions can be very simple with a single parameter for the value to convert. You don’t need to worry about passing values from the pipeline, error handling or anything advanced. The function can simply write the converted value to the pipeline as a result. If you are feeling a bit more confident, have your function write an object to the pipeline with the original and converted values.
Even though this is a basic and beginner challenge, you should still follow PowerShell scripting best practices such as using a proper verb-noun naming convention. If you feel a bit more skilled, feel free to include error handling, parameter validation and pipeline input.
To help you along, you can use these formulas to convert temperatures. To convert from Celsius to Fahrenheit: (C*1.8)+32. To convert from Fahrenheit to Celsius is a matter of reversing the process: (F-32)/1.8. As points of reference, freezing is 32F and 0C. Boiling is 212F and 100C.
As always, please don’t submit any code solutions as comments. But you are encouraged to share links to your work.
5 Replies to “A Beginner PowerShell Function Challenge”
Used this challenge as an exercise when trying to get a colleague into POSH, just to teach him the patterns. I didn’t convert it into an object with input and output temps for the pipeline (although it’s a minor task), but this script will accept pipeline input at least.
And -40 is -40!
Here is my solution, figured out most of the puzzle other then the pipeline.. Anyone want to step in and help? 🙂
This challenge triggered a trip down memory lane as I vaguely recall writing a similar conversion program, probably in Commodore Basic, during the ’80s.
I provided simple, intermediate, and advanced functions. The advanced functions includes a parameter to assist the user with decimal precision.
Just for the learning chance, I also provided a Temperature custom class. The gist has a nice table of the class methods.
Please feel free to post any comments on the gist. Sample output from all functions and some class methods are posted as a comment to the gist.
My solution includes the advance features and rounds the output to 4 decimal places.
Comments are closed.