Powershell as a problem solver

Last year I watched an absolutely fantastic two day training session on Powershell now available here:

http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start

Among the many things I learned from that training, one quote from one of the trainers really stuck out with me. Jeffrey Snover, the inventor of Powershell said “Powershell is designed to solve business problems.”

The take away for me from that quote is that Powershell can be an incredible vehicle for building complex automation tools that can be shared with your entire company and beyond. But also, sometimes you have business problems that are much simpler and “one-off” in scope. It turns out Powershell shines brightly here too. I’d like to give you a quick example of how I used Powershell to quickly solve a personal “business” problem in what otherwise would have been very frustrating.

I am starting to revisit the idea of finally learning Korean – enough to be actually properly conversational. With that goal in mind, I found a Korean aptitude test on the Internet. It was 70 questions and increased in difficulty as you worked your way through the list. I thought this was a great place to sink my teeth into the language again. However, I decided that I would much prefer to have these questions inside a “Visual CertExam” or VCE file. This works with an application called the VCE Designer and VCE Manager to present electronic multiple choice exams. “Wouldn’t it be cool if I could somehow import this exam into a VCE file so I could quiz myself? The question is, how could I do that? Let’s start with a sample of what the source material looks like:

Source: http://www.languagetrainers.com/language_tests/korean_level_test.php

1.
이름은
지윤 ______ . [ nai ee-reum-eun ji-yoon ________.]
 이에요[ee-ae-yo] 
 있어요[it-sau-yo] 
 했어요[hat-sau-yo]
 이었어요[ee-ut-sau-yo]
 I don’t know

That repeats like that for 70 questions. A few things immediately jump out at me. First, I didn’t like the Romanization in each question as I just find it distracting so I’d like to remove that. Second, each question has “I don’t know” for an answer. For my exam, I don’t need that so I need to remove that entry as well. Next I had a look at VCE Designer and noted there is an import tool. I reviewed the documentation and the template they provided to demonstrate what their importer was looking for had the following structure:

1. Text of the first question.

A. First choice text.
B. Second choice text.
C. Third choice text.
D. Fourth choice text.

Answer: A

Correct answer explanation and reference.

 

Based on this, we now have or criteria defined. I need to create a text file that:

  • Removes “I don’t know” from each answer
  • Removes the Romanization from each question
  • Adds a letter code to each multiple choice answer

This is where Powershell comes in. First I copied all 70 questions in plain text into a text file. (Note: This must be saved with UTF-8 encoding as ANSi doesn’t support the Hangul character set) Once that was done, I ran this very simple Powershell script against the file:

 

# LOAD IN THE TEXT FILE INTO A VARIABLE
$Input = get-content c:\Temp\Korean\SourceExam.txt

# SET THE LINE COUNTER TO 1 TO BEGIN
$CurrentLine = 1

ForEach($LineofText in $Input)
{
    if ($CurrentLine -eq 1) { Write-Host $LineofText.Split("[")[0] } # IF THE FIRST LINE, IT'S THE QUESTION, SPLIT THE TEXT AT THE OPEN BRACKET AND ONLY GRAB THE TEXT BEFORE THAT
	if ($CurrentLine -eq 3) { Write-Host "A." $LineofText.Split("[")[0] }
	if ($CurrentLine -eq 4) { Write-Host "B." $LineofText.Split("[")[0] }
	if ($CurrentLine -eq 5) { Write-Host "C." $LineofText.Split("[")[0] }
	if ($CurrentLine -eq 6) { Write-Host "D." $LineofText.Split("[")[0] }
	#if ($i -eq 8) { DO NOTHING SINCE WE DON'T WANT THIS "I DON'T KNOW" LINE }
	$CurrentLine++ # INCREMENT THE CURRENT POSITION TO THE NEXT LINE IN THE TEXT FILE
	if ($CurrentLine -gt 8) { $CurrentLine = 1 }  # EACH QUESTION HAS A MAXIMUM OF 8 LINES SO ONCE WE GET THIS FAR RESET FOR THE NEXT QUESTION
}

 

The results looked like this:

1. 내 이름은 지윤 ______ .

A. 이에요

B. 있어요

C. 했어요

D. 이었어요

2. 그 남자는 25살 _______ .

A. 있다

B. 이다

C. 했다

D. 한다


 

Much better! I was then able to save the results to a file and import that file into VCE. Since the source material doesn’t provide answers, I have to provide my own. But in this case that’s part of the studying so I’m more than happy with that. So what was the result of this effort?

ORIGINAL WEBPAGE:

MY CONVERSION:

Sure you could have done with this a batch file or a thousand other ways. But Powershell made this quick and painless and now I’m actually motivated to fill out all 70 questions. Thank you Powershell!

 

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.