{"id":12411,"date":"2011-10-13T00:01:00","date_gmt":"2011-10-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/13\/add-random-question-features-to-a-powershell-game\/"},"modified":"2011-10-13T00:01:00","modified_gmt":"2011-10-13T00:01:00","slug":"add-random-question-features-to-a-powershell-game","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/add-random-question-features-to-a-powershell-game\/","title":{"rendered":"Add Random Question Features to a PowerShell Game"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to add random question features to a Windows PowerShell script game.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I have really enjoyed Ottawa. The class I am teaching on Windows PowerShell has absolutely rocked! Ottawa is one of my favorite cities (there is a really cool woodworking store here), and I have been able to spend a decent amount of time with my friend George. All in all, it has seemed like vacation more than work. Next week, we are in Montreal. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Anyway, I have continued playing with my Windows PowerShell quiz script, and today I want to share my latest iteration. It provides the ability to choose a specific number of questions. The questions themselves are selected in a random manner. The script will also grade performance and provide the number of right and wrong answers, and the percentage of correct answers as feedback. <\/span><\/p>\n<blockquote>\n<p><span style=\"font-size: small\"><span><span style=\"font-family: Segoe\"><b>Note<\/b> This is the fourth part of a multipart series of articles about writing a Windows PowerShell quiz script. On the first day, I looked at <\/span><\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/10\/cool-powershell-game-teaches-cmdlet-names.aspx\"><span style=\"font-family: Segoe\">replacing random letters in a string<\/span><\/a><span style=\"font-family: Segoe\">. Next, I <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/11\/use-powershell-parameter-attributes-to-avoid-errors.aspx\"><span style=\"font-family: Segoe\">moved the code into a function and added parameter validation<\/span><\/a><span style=\"font-family: Segoe\"> to limit the values that can be supplied to the function. In this way I was able to prevent a divide by zero error that could arise depending on what someone supplied from the command line. This is actually a great way to do error handling &ndash; prevent the error from arising in the first place. Yesterday I <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/12\/create-a-powershell-quiz-script.aspx\"><span style=\"font-family: Segoe\">added the question and answer feature<\/span><\/a><span style=\"font-family: Segoe\"> for the Windows PowerShell cmdlet name game. Today I am adding the ability to choose a specific number of questions, as well as a grade feature. <\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">The complete New-CmdletPuzzleCountQuestions.ps1 script is shown here.<\/span><\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">function New-CmdletPuzzle<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">{<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>Param(<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>[Parameter(Position=0,<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>HelpMessage=&#8221;A number between 2 and 7&#8243;)]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>[alias(&#8220;Level&#8221;)]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>[ValidateRange(2,7)]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>[int]$difficulty = 4<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>) #end param<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>$array = @()<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>$hash = New-Object hashtable<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>$array = Get-Command -CommandType cmdlet | <\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>ForEach-Object { $_.name.tostring() }<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">&nbsp;<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>Foreach($cmdlet in $array)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>{<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>$rndChar = get-random -InputObject ($cmdlet.tochararray()) `<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>-count ($cmdlet.length\/$difficulty)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>foreach($l in $rndchar)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>{<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp; <\/span>$cmdletP = $cmdlet<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp; <\/span>$cmdletP = $cmdletP.Replace($l,&#8221;_&#8221;)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>}# end foreach l<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>$hash.add($cmdlet,$cmdletP)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>} #end foreach cmdlet<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>$hash<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">} #end function New-CmdletPuzzle<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">&nbsp;<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">Function New-Question<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">{<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>Param(<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>[hashtable]$Puzzle,<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>[int]$num = 10<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;<\/span>)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>$quiz = @{}<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>$right = $wrong = 0<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span>Get-Random -InputObject $($puzzle.keys) -Count $num | <\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp; <\/span>ForEach-Object { $quiz.add($_,$puzzle.item($_)) }<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp; <\/span>Foreach ($p in $quiz.KEYS)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp; <\/span>{<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$rtn = Read-host &#8220;What is the cmdlet name $($quiz.item($P))&#8221;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>If($quiz.contains($rtn))<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{ <\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;Correct $($quiz.item($P)) equals $p&#8221; <\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$right ++<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>ELSE<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;Sorry. $rtn is not right. $($quiz.item($P)) is $p&#8221; <\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$wrong ++<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end foreach $P<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>New-Object PSObject -Property @{<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;right&#8221; = $right<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;Wrong&#8221; = $wrong<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000\"><span><span style=\"font-family: Segoe\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;Percent&#8221; = &#8220;{0:N2}&#8221; -f ($right\/$num*100)<span>&nbsp; <\/span>}<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span><span style=\"color: #000000;font-family: Segoe\">&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">} #end function New-Question<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">&nbsp;<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\"># *** Entry point to script *** <\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">&nbsp;<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">$puzzle = New-CmdletPuzzle -level 5<\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"color: #000000;font-family: Segoe\">New-Question -puzzle $puzzle -num 5<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">I did not make any changes to the New-<b>CmdletPuzzle<\/b> function, so there is no need to discuss it today. I did make several changes to the <b>New-Question<\/b> function, and that is what I will discuss today. The first change I made was to add an <i>addition<\/i> parameter to allow the user to determine the number of questions to receive. I set the default value to 10, which seems like a reasonable number (it also made it easy to check the accuracy of the percentage right feature). This portion of the script is shown here:<\/span><\/p>\n<blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">Function New-Question<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">{<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"> Param(<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp; [hashtable]$Puzzle,<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp; [int]$num = 10<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"> )<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">Next I create an empty hash table that I store in the <b>$quiz<\/b> variable. This hash table creates the quiz after the specific number of random questions is chosen. I then initialize the <b>$right<\/b> and <b>$wrong<\/b> variables to the number zero. At first I used <b>$null<\/b>, but when all the questions were right or all wrong, it was not obvious because the output would be blank. The number zero makes it obvious that a field is empty. This portion of the function is shown here:<\/span><\/p>\n<blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp; $quiz = @{}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp; $right = $wrong = 0<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">The hardest part of the revised function is the code that accepts the original puzzle containing all cmdlet names and cmdlet names with obscured string. The <b>Get-Random<\/b> cmdlet will not select key value pairs from a hash table. So what I had to do was get a collection of keys, and allow the <b>Get-Random<\/b> cmdlet to choose a specific number of keys from the hash table. I pipe the keys to a <b>Foreach-object<\/b> cmdlet where I use the key to retrieve the associated value from the hash table. I then add both the key and the value to the <b>$quiz<\/b> hash table. This provides me with a hash table that contains the newly selected cmdlet and obscured cmdlet names. This portion of the script is shown here:<\/span><\/p>\n<blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp; Get-Random -InputObject $($puzzle.keys) -Count $num | <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp; ForEach-Object { $quiz.add($_,$puzzle.item($_)) }<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">Because I created a new hash table to hold the newly selected questions and answers, I had to change the name of the variables used in the <b>Foreach<\/b> loop. This is also where I added the use of <b>$right<\/b> counter variable. This portion of the script is shown here:<\/span><\/p>\n<blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">Foreach ($p in $quiz.KEYS)<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp; {<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp;&nbsp; $rtn = Read-host &#8220;What is the cmdlet name $($quiz.item($P))&#8221;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp;&nbsp; If($quiz.contains($rtn))<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;Correct $($quiz.item($P)) equals $p&#8221; <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $right ++<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">The same type of changes are made to the else portion of the <b>Foreach<\/b> loop. This portion of the script is shown here: <\/span><\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\">ELSE<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;Sorry. $rtn is not right. $($quiz.item($P)) is $p&#8221; <\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$wrong ++<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end foreach $P<\/span><\/span><\/span><\/p>\n<\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">After all of the questions are answered, it is time to evaluate the performance. I use a custom <b>psobject<\/b> to do this. I add the <b>right<\/b>, <b>wrong<\/b>, and <b>percent<\/b> properties to it. Here is the code for the <b>New-Object<\/b>. <\/span><\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\">New-Object PSObject -Property @{<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;right&#8221; = $right<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;Wrong&#8221; = $wrong<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;Percent&#8221; = &#8220;{0:N2}&#8221; -f ($right\/$num*100)<span>&nbsp; <\/span>}<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span><span style=\"font-family: Segoe\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt 0.5in\" class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"color: #000000\">} #end function New-Question<\/span><\/span><\/span><\/p>\n<\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">The entry point to the script does not really need to change. I decided to use the <i>num<\/i> parameter to change the number of questions asked from the default of 10 to 5. Here is the entry point:<\/span><\/p>\n<blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">$puzzle = New-CmdletPuzzle -level 5<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">New-Question -puzzle $puzzle -num 5<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: Segoe;font-size: small\">When the script runs, each question is evaluated as it is answered. After complete, the performance is displayed. This is illustrated in the following figure. <\/span><\/p>\n<p><span style=\"font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5700.Hsg-10-13-11-1_39BD45F6.png\"><span style=\"font-family: Segoe\"><img decoding=\"async\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" title=\"Image of quiz performance\" border=\"0\" alt=\"Image of quiz performance\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7028.Hsg-10-13-11-1_thumb_2898CC79.png\" width=\"504\" height=\"545\" \/><\/span><\/a><\/span><\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">That is about all there is for now. I think See you tomorrow when I will bring this series to an exciting conclusion. I have a bit of cleanup I am wanting to do, and I also want to add a couple more features. <\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\"><span style=\"font-family: Segoe\">Twitter<\/span><\/a><span style=\"font-family: Segoe\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-family: Segoe\">Facebook<\/span><\/a><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\"><span style=\"font-family: Segoe\">scripter@microsoft.com<\/span><\/a><span style=\"font-family: Segoe\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\"><span style=\"font-family: Segoe\">Official Scripting Guys Forum<\/span><\/a><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><\/span><\/p>\n<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to add random question features to a Windows PowerShell script game. Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I have really enjoyed Ottawa. The class I am teaching on Windows PowerShell has absolutely rocked! Ottawa is one of my favorite cities (there is a really cool woodworking store [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[292,3,4,45],"class_list":["post-12411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hashtables-and-dictionary-objects","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to add random question features to a Windows PowerShell script game. Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I have really enjoyed Ottawa. The class I am teaching on Windows PowerShell has absolutely rocked! Ottawa is one of my favorite cities (there is a really cool woodworking store [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12411"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12411\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}